結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | outline |
提出日時 | 2021-03-26 23:22:34 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,833 bytes |
コンパイル時間 | 2,117 ms |
コンパイル使用メモリ | 162,296 KB |
実行使用メモリ | 816,216 KB |
最終ジャッジ日時 | 2024-11-29 01:38:18 |
合計ジャッジ時間 | 39,686 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
13,632 KB |
testcase_01 | AC | 2 ms
504,856 KB |
testcase_02 | WA | - |
testcase_03 | WA | - |
testcase_04 | WA | - |
testcase_05 | AC | 5 ms
13,636 KB |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | WA | - |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | AC | 147 ms
21,380 KB |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
testcase_21 | TLE | - |
testcase_22 | AC | 359 ms
25,932 KB |
testcase_23 | TLE | - |
testcase_24 | AC | 155 ms
21,508 KB |
testcase_25 | TLE | - |
testcase_26 | MLE | - |
ソースコード
#include <iostream> #include <vector> #include <algorithm> #include <cmath> #include <queue> #include <string> #include <map> #include <set> #include <stack> #include <tuple> #include <deque> #include <array> #include <numeric> #include <bitset> #include <iomanip> #include <cassert> #include <chrono> #include <random> #include <limits> #include <iterator> #include <functional> #include <sstream> #include <fstream> #include <complex> #include <cstring> #include <unordered_map> #include <unordered_set> using namespace std; using ll = long long; constexpr int INF = 1001001001; // constexpr int mod = 1000000007; constexpr int mod = 998244353; template<class T> inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template<class T> inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } template<typename T> struct Edge{ int to; T cost; Edge() = default; Edge(int to, T cost) : to(to), cost(cost) {} }; template<typename T> struct CostLCA{ using G = vector<vector<Edge<T>>>; const int ub_log; vector<int> depth; vector<T> costs; const G& g; vector<vector<int>> table; CostLCA() = default; CostLCA(const G& g) : g(g), depth(g.size()), costs(g.size()), ub_log(32 - __builtin_clz(g.size())){ table.assign(ub_log, vector<int>(g.size(), -1)); } void dfs(int from, int par = -1, int dep = 0, T sum = 0){ table[0][from] = par; depth[from] = dep; costs[from] = sum; for(int i = 0; i < (int)g[from].size(); ++i){ int to = g[from][i].to; T cost = g[from][i].cost; if(to != par) dfs(to, from, dep + 1, sum + cost); } } void build(int root = 0){ dfs(root); for(int k = 0; k + 1 < ub_log; ++k){ for(int i = 0; i < (int)table[k].size(); ++i){ if(table[k][i] == -1) table[k + 1][i] = -1; else table[k + 1][i] = table[k][table[k][i]]; } } } int query(int u, int v){ if(depth[u] > depth[v]) swap(u, v); v = get(v, depth[v] - depth[u]); if(u == v) return u; for(int i = ub_log - 1; i >= 0; --i){ if(table[i][u] != table[i][v]){ u = table[i][u]; v = table[i][v]; } } return table[0][u]; } int get(int v, int x){ if(x <= 0) return v; for(int i = ub_log - 1; i >= 0; --i){ if(x >> i & 1) v = table[i][v]; } return v; } int length(int u, int v){ int lca = query(u, v); return depth[u] + depth[v] - depth[lca] * 2; } T dist(int u, int v){ int lca = query(u, v); return costs[u] + costs[v] - costs[lca] * 2; } void print(){ for(int k = 0; k + 1 < ub_log; ++k){ for(int i = 0; i < (int)table[k].size(); ++i){ cerr << table[k][i] << " "; } cerr << "\n"; } } }; struct UnionFind{ int sz; // vertex number vector<int> par; vector<int> rank; UnionFind(int n) : sz(n) { par.resize(sz); rank.assign(sz, 0); for(int i = 0; i < sz; ++i){ par[i] = i; } } int find(int x){ if(par[x] == x) return x; else return par[x] = find(par[x]); } bool same(int x, int y){ return find(x) == find(y); } void unite(int x, int y){ x = find(x), y = find(y); if(x == y) return; if(rank[x] < rank[y]) par[x] = y; else{ par[y] = x; if(rank[x] == rank[y]) ++rank[x]; } } }; template<typename T> struct edge{ int u, v; T cost; edge() = default; edge(int u, int v, T cost) : u(u), v(v), cost(cost) {} bool operator<(const edge& other) const{ return cost < other.cost; } }; int N, K, Q; vector<edge<ll>> es; int M[15], P[15], X[15][100005]; vector<CostLCA<ll>> lca; vector<vector<Edge<ll>>> g[1050]; template<typename T> vector<vector<Edge<ll>>> Kruskal(vector<edge<T>> &es, int V){ vector<vector<Edge<T>>> g(N + K); sort(es.begin(), es.end()); int sz = es.size(); UnionFind tree(V); for(int i = 0; i < sz; ++i){ if(!tree.same(es[i].u, es[i].v)){ tree.unite(es[i].u, es[i].v); g[es[i].u].emplace_back(es[i].v, es[i].cost); g[es[i].v].emplace_back(es[i].u, es[i].cost); } } return g; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); cin >> N >> K; for(int i = 1; i < N; ++i){ int A, B, C; cin >> A >> B >> C; --A, --B; es.emplace_back(A, B, C); } for(int i = 0; i < K; ++i){ cin >> M[i] >> P[i]; for(int j = 0; j < M[i]; ++j){ cin >> X[i][j]; --X[i][j]; } } vector<ll> Psum(1 << K); for(int mask = 0; mask < 1 << K; ++mask){ auto es2 = es; for(int i = 0; i < K; ++i){ if(mask >> i & 1){ Psum[mask] += P[i]; for(int j = 0; j < M[i]; ++j){ es2.emplace_back(N + i, X[i][j], 0); } } } g[mask] = Kruskal<ll>(es2, N + K); lca.emplace_back(CostLCA(g[mask])); lca[mask].build(); } cin >> Q; for(int q = 0; q < Q; ++q){ int U, V; cin >> U >> V; --U, --V; ll ans = 3e+18; for(int mask = 0; mask < 1 << K; ++mask){ // cout << "dist:" << lca[mask].dist(U, V) << '\n'; chmin(ans, Psum[mask] + lca[mask].dist(U, V)); } cout << ans << '\n'; } return 0; }