結果
問題 | No.1442 I-wate Shortest Path Problem |
ユーザー | tnakao0123 |
提出日時 | 2021-03-29 20:16:25 |
言語 | C++14 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 388 ms / 3,000 ms |
コード長 | 3,247 bytes |
コンパイル時間 | 4,157 ms |
コンパイル使用メモリ | 104,332 KB |
実行使用メモリ | 38,652 KB |
最終ジャッジ日時 | 2024-05-07 01:26:12 |
合計ジャッジ時間 | 11,066 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
5,888 KB |
testcase_01 | AC | 3 ms
5,888 KB |
testcase_02 | AC | 7 ms
6,912 KB |
testcase_03 | AC | 38 ms
6,656 KB |
testcase_04 | AC | 8 ms
6,656 KB |
testcase_05 | AC | 5 ms
6,528 KB |
testcase_06 | AC | 41 ms
6,656 KB |
testcase_07 | AC | 6 ms
6,272 KB |
testcase_08 | AC | 37 ms
6,400 KB |
testcase_09 | AC | 13 ms
7,680 KB |
testcase_10 | AC | 42 ms
7,040 KB |
testcase_11 | AC | 40 ms
7,040 KB |
testcase_12 | AC | 266 ms
36,320 KB |
testcase_13 | AC | 125 ms
29,696 KB |
testcase_14 | AC | 198 ms
31,724 KB |
testcase_15 | AC | 170 ms
31,912 KB |
testcase_16 | AC | 244 ms
34,408 KB |
testcase_17 | AC | 366 ms
38,596 KB |
testcase_18 | AC | 383 ms
38,536 KB |
testcase_19 | AC | 291 ms
35,316 KB |
testcase_20 | AC | 388 ms
38,464 KB |
testcase_21 | AC | 379 ms
38,652 KB |
testcase_22 | AC | 160 ms
29,696 KB |
testcase_23 | AC | 299 ms
35,796 KB |
testcase_24 | AC | 120 ms
30,204 KB |
testcase_25 | AC | 246 ms
34,748 KB |
testcase_26 | AC | 125 ms
31,176 KB |
ソースコード
/* -*- coding: utf-8 -*- * * 1442.cc: No.1442 I-wate Shortest Path Problem - yukicoder */ #include<cstdio> #include<cstdlib> #include<cstring> #include<cmath> #include<iostream> #include<string> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<deque> #include<algorithm> #include<numeric> #include<utility> #include<complex> #include<functional> using namespace std; /* constant */ const int MAX_N = 100000; const int MAX_K = 10; const int BN = 17; // 2^17 = 131072 const long long LINF = 1LL << 62; /* typedef */ typedef long long ll; typedef pair<int,int> pii; typedef pair<ll,int> pli; typedef vector<pii> vpii; /* global variables */ vpii nbrs[MAX_N + MAX_K]; int fcs[MAX_K]; int ps[MAX_N][BN], ds[MAX_N], cis[MAX_N]; ll pcs[MAX_N][BN], fds[MAX_K][MAX_N + MAX_K]; /* subroutines */ void dijkstra(int n, int st, ll ds[]) { fill(ds, ds + n, LINF); ds[st] = 0; priority_queue<pli> q; q.push(pli(0, st)); while (! q.empty()) { pli u = q.top(); q.pop(); ll ud = -u.first; int ui = u.second; if (ds[ui] != ud) continue; vpii &nbru = nbrs[ui]; for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) { int vi = vit->first; ll vd = ud + vit->second; if (ds[vi] > vd) { ds[vi] = vd; q.push(pli(-vd, vi)); } } } } ll pathcost(int u, int v) { if (ds[u] > ds[v]) swap(u, v); ll c = 0; for (int i = BN - 1; i >= 0; i--) if (((ds[v] - ds[u]) >> i) & 1) c += pcs[v][i], v = ps[v][i]; if (u == v) return c; for (int i = BN - 1; i >= 0; i--) if (ps[u][i] != ps[v][i]) { c += pcs[u][i] + pcs[v][i]; u = ps[u][i], v = ps[v][i]; } return c + pcs[u][0] + pcs[v][0]; } /* main */ int main() { int n, k; scanf("%d%d", &n, &k); for (int i = 1; i < n; i++) { int a, b, c; scanf("%d%d%d", &a, &b, &c); a--, b--; nbrs[a].push_back(pii(b, c)); nbrs[b].push_back(pii(a, c)); } for (int i = 0; i < k; i++) { int mi; scanf("%d%d", &mi, fcs + i); for (int j = 0; j < mi; j++) { int xj; scanf("%d", &xj); xj--; nbrs[n + i].push_back(pii(xj, 0)); nbrs[xj].push_back(pii(n + i, fcs[i])); } } for (int i = 0; i < k; i++) dijkstra(n + k, n + i, fds[i]); ps[0][0] = -1; pcs[0][0] = 0; ds[0] = 0; for (int u = 0; u >= 0;) { vpii &nbru = nbrs[u]; int up = ps[u][0]; if (cis[u] == 0) { for (int i = 0; i < BN - 1; i++) { if (ps[u][i] >= 0) { ps[u][i + 1] = ps[ps[u][i]][i]; pcs[u][i + 1] = pcs[u][i] + pcs[ps[u][i]][i]; } else { ps[u][i + 1] = -1; pcs[u][i + 1] = 0; } } } if (cis[u] < nbru.size()) { pii vv = nbru[cis[u]++]; int v = vv.first; if (v < n && v != up) { ps[v][0] = u; pcs[v][0] = vv.second; ds[v] = ds[u] + 1; u = v; } } else u = up; } int qn; scanf("%d", &qn); while (qn--) { int u, v; scanf("%d%d", &u, &v); u--, v--; ll minc = pathcost(u, v); //printf("pathcost(%d,%d) = %lld\n", u, v, minc); for (int i = 0; i < k; i++) { ll c = fds[i][u] + fds[i][v] + fcs[i]; if (minc > c) minc = c; } printf("%lld\n", minc); } return 0; }