結果
問題 | No.1308 ジャンプビーコン |
ユーザー | 00 Sakuda |
提出日時 | 2024-04-01 15:51:53 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 1,311 ms / 4,000 ms |
コード長 | 2,613 bytes |
コンパイル時間 | 2,340 ms |
コンパイル使用メモリ | 217,008 KB |
実行使用メモリ | 100,096 KB |
最終ジャッジ日時 | 2024-09-30 21:51:56 |
合計ジャッジ時間 | 24,160 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge1 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 77 ms
99,200 KB |
testcase_01 | AC | 77 ms
99,200 KB |
testcase_02 | AC | 73 ms
99,200 KB |
testcase_03 | AC | 76 ms
99,200 KB |
testcase_04 | AC | 76 ms
99,200 KB |
testcase_05 | AC | 77 ms
99,072 KB |
testcase_06 | AC | 77 ms
99,072 KB |
testcase_07 | AC | 76 ms
99,072 KB |
testcase_08 | AC | 75 ms
99,328 KB |
testcase_09 | AC | 81 ms
99,200 KB |
testcase_10 | AC | 78 ms
99,200 KB |
testcase_11 | AC | 77 ms
99,328 KB |
testcase_12 | AC | 77 ms
99,072 KB |
testcase_13 | AC | 97 ms
99,456 KB |
testcase_14 | AC | 95 ms
99,456 KB |
testcase_15 | AC | 97 ms
99,456 KB |
testcase_16 | AC | 97 ms
99,328 KB |
testcase_17 | AC | 96 ms
99,328 KB |
testcase_18 | AC | 1,063 ms
99,712 KB |
testcase_19 | AC | 1,080 ms
99,712 KB |
testcase_20 | AC | 1,067 ms
99,712 KB |
testcase_21 | AC | 1,082 ms
99,712 KB |
testcase_22 | AC | 1,161 ms
99,584 KB |
testcase_23 | AC | 78 ms
99,200 KB |
testcase_24 | AC | 78 ms
99,200 KB |
testcase_25 | AC | 78 ms
99,200 KB |
testcase_26 | AC | 728 ms
99,712 KB |
testcase_27 | AC | 742 ms
99,584 KB |
testcase_28 | AC | 730 ms
99,584 KB |
testcase_29 | AC | 991 ms
99,840 KB |
testcase_30 | AC | 955 ms
99,840 KB |
testcase_31 | AC | 804 ms
99,968 KB |
testcase_32 | AC | 1,133 ms
100,096 KB |
testcase_33 | AC | 1,289 ms
99,712 KB |
testcase_34 | AC | 734 ms
99,712 KB |
testcase_35 | AC | 737 ms
99,584 KB |
testcase_36 | AC | 802 ms
99,712 KB |
testcase_37 | AC | 798 ms
99,712 KB |
testcase_38 | AC | 1,294 ms
99,712 KB |
testcase_39 | AC | 1,311 ms
99,712 KB |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; int MN = 3500; vector<ll> T(MN, 0); ll INF = (1LL << 60); struct ED { int to; ll cost; }; struct TP { ll m1 = INF;ll m2 = INF; void add(ll x) { if (x < m1) { m2 = m1;m1 = x; } else { m2 = min(m2, x); } } ll get(ll x) { return (x != m1 ? m1 : m2); } }; vector<int> PAR(MN, 0); vector<vector<ED>> G(MN); vector<vector<ll>> D(MN, vector<ll> (MN, -1)); vector<ll> dp1, dp2; void dfs(int v, int par) { dp1[v] = T[v]; PAR[v] = par; for (auto nv : G[v]) { if (nv.to == par) continue; dfs(nv.to, v); dp1[v] = min(dp1[v], min(nv.cost + T[nv.to], nv.cost + dp1[nv.to])); } } void dfs2(int v, int par) { TP tp; for (auto nv : G[v]) { if (nv.to == par) { tp.add(min(nv.cost + T[nv.to], nv.cost + dp2[v])); } else { tp.add(min(nv.cost + T[nv.to], nv.cost + dp1[nv.to])); } } for (auto nv : G[v]) { if (nv.to == par) continue; dp2[nv.to] = T[v]; ll value = min(nv.cost + T[nv.to], nv.cost + dp1[nv.to]); dp2[nv.to] = min(dp2[nv.to], tp.get(value)); dfs2(nv.to, v); } } vector<ll> solve(int N) { dp1.assign(N, INF); dp2.assign(N, INF); dfs(0, 0); dfs2(0, 0); vector<ll> ans(N, INF); for (int v = 0;v < N;v++) { ans[v] = T[v]; for (auto nv : G[v]) { ans[v] = min(ans[v], nv.cost + T[nv.to]); if (nv.to == PAR[v]) { ans[v] = min(ans[v], nv.cost + dp2[v]); } else { ans[v] = min(ans[v], nv.cost + dp1[nv.to]); } } } return ans; } int main() { int N, Q;ll C;cin >> N >> Q >> C; for (int i = 0;i < N - 1;i++) { int u, v;ll l;cin >> u >> v >> l; u--;v--; G[u].push_back({v, l}); G[v].push_back({u, l}); } vector<int> X(Q+1, -1); for (int i = 1;i <= Q;i++) cin >> X[i],X[i]--; for (int v = 0;v < N;v++) { D[v][v] = 0; queue<int> pq; pq.push(v); while (!pq.empty()) { int t = pq.front();pq.pop(); for (auto nv : G[t]) { if (D[v][nv.to] == -1) { D[v][nv.to] = D[v][t] + nv.cost; pq.push(nv.to); } } } } vector<ll> dp(N, INF); for (int v = 0;v < N;v++) { int s = X[1];int t = X[2]; dp[v] = D[s][v] + D[v][t]; } for (int i = 3;i <= Q;i++) { int s = X[i - 1]; int t = X[i]; vector<ll> ndp(N, INF); ll mins = INF; for (int v = 0;v < N;v++) { T[v] = dp[v]; mins = min(mins, dp[v]); } for (int v = 0;v < N;v++) ndp[v] = D[s][v] + D[v][t] + mins; auto res = solve(N); for (int v = 0;v < N;v++) { ndp[v] = min(ndp[v], res[v] + D[v][t] + C); ndp[v] = min(ndp[v], dp[v] + D[s][t]); } swap(dp, ndp); } ll ans = INF; for (int v = 0;v < N;v++) ans = min(ans, dp[v]); cout << ans << endl; }