結果
問題 | 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
|
実行時間 | 521 ms / 3,000 ms |
コード長 | 3,247 bytes |
コンパイル時間 | 1,118 ms |
コンパイル使用メモリ | 106,820 KB |
実行使用メモリ | 38,884 KB |
最終ジャッジ日時 | 2024-11-29 13:13:39 |
合計ジャッジ時間 | 10,408 ms |
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 4 ms
10,588 KB |
testcase_01 | AC | 4 ms
11,128 KB |
testcase_02 | AC | 9 ms
12,580 KB |
testcase_03 | AC | 44 ms
13,804 KB |
testcase_04 | AC | 9 ms
10,692 KB |
testcase_05 | AC | 7 ms
11,128 KB |
testcase_06 | AC | 43 ms
12,624 KB |
testcase_07 | AC | 8 ms
13,248 KB |
testcase_08 | AC | 41 ms
10,748 KB |
testcase_09 | AC | 15 ms
13,864 KB |
testcase_10 | AC | 47 ms
14,604 KB |
testcase_11 | AC | 46 ms
12,668 KB |
testcase_12 | AC | 332 ms
37,472 KB |
testcase_13 | AC | 149 ms
31,484 KB |
testcase_14 | AC | 251 ms
34,884 KB |
testcase_15 | AC | 221 ms
32,160 KB |
testcase_16 | AC | 302 ms
36,272 KB |
testcase_17 | AC | 518 ms
38,692 KB |
testcase_18 | AC | 516 ms
38,884 KB |
testcase_19 | AC | 377 ms
37,384 KB |
testcase_20 | AC | 513 ms
38,600 KB |
testcase_21 | AC | 521 ms
38,744 KB |
testcase_22 | AC | 195 ms
31,172 KB |
testcase_23 | AC | 402 ms
37,744 KB |
testcase_24 | AC | 129 ms
31,152 KB |
testcase_25 | AC | 320 ms
36,832 KB |
testcase_26 | AC | 136 ms
31,700 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; }