結果
問題 | No.1812 Uribo Road |
ユーザー | vjudge1 |
提出日時 | 2024-09-09 01:21:00 |
言語 | C++17 (gcc 12.3.0 + boost 1.83.0) |
結果 |
AC
|
実行時間 | 104 ms / 5,000 ms |
コード長 | 2,830 bytes |
コンパイル時間 | 2,951 ms |
コンパイル使用メモリ | 234,084 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-09 01:21:05 |
合計ジャッジ時間 | 5,137 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 2 ms
6,812 KB |
testcase_01 | AC | 2 ms
6,940 KB |
testcase_02 | AC | 2 ms
6,940 KB |
testcase_03 | AC | 4 ms
6,940 KB |
testcase_04 | AC | 3 ms
6,944 KB |
testcase_05 | AC | 2 ms
6,940 KB |
testcase_06 | AC | 3 ms
6,944 KB |
testcase_07 | AC | 2 ms
6,940 KB |
testcase_08 | AC | 3 ms
6,940 KB |
testcase_09 | AC | 5 ms
6,940 KB |
testcase_10 | AC | 3 ms
6,940 KB |
testcase_11 | AC | 3 ms
6,940 KB |
testcase_12 | AC | 42 ms
6,940 KB |
testcase_13 | AC | 19 ms
6,944 KB |
testcase_14 | AC | 18 ms
6,944 KB |
testcase_15 | AC | 35 ms
6,940 KB |
testcase_16 | AC | 28 ms
6,940 KB |
testcase_17 | AC | 88 ms
6,944 KB |
testcase_18 | AC | 16 ms
6,940 KB |
testcase_19 | AC | 104 ms
6,940 KB |
testcase_20 | AC | 104 ms
6,940 KB |
testcase_21 | AC | 78 ms
6,944 KB |
testcase_22 | AC | 62 ms
6,944 KB |
testcase_23 | AC | 4 ms
6,944 KB |
testcase_24 | AC | 2 ms
6,940 KB |
testcase_25 | AC | 15 ms
6,940 KB |
testcase_26 | AC | 3 ms
6,944 KB |
testcase_27 | AC | 13 ms
6,940 KB |
testcase_28 | AC | 27 ms
6,940 KB |
testcase_29 | AC | 2 ms
6,940 KB |
testcase_30 | AC | 4 ms
6,944 KB |
testcase_31 | AC | 52 ms
6,944 KB |
testcase_32 | AC | 3 ms
6,940 KB |
testcase_33 | AC | 21 ms
6,944 KB |
ソースコード
#include <bits/stdc++.h> #define int long long #define endl '\n' using namespace std; const int INF = 1e18; struct Edge { int u, d; }; vector<vector<Edge>> gr(10001, vector<Edge>()); vector<int> dijkstra(int start) { vector<int> dist(gr.size(), INF); dist[start] = 0; priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; q.push({0, start}); while (!q.empty()) { int v = q.top().second, d = q.top().first; q.pop(); if (dist[v] < d) continue; for (auto [u, d] : gr[v]) { if (dist[v] + d < dist[u]) { dist[u] = dist[v] + d; q.push({dist[u], u}); } } } return dist; } int dist[30][1 << 12]; struct Road { int v, u, d; }; int solve() { int n, m; cin >> n >> m; int r; cin >> r; vector<int> vr; for (int i = 0; i < r; i++) { int c; cin >> c; c--; vr.push_back(c); } sort(vr.begin(), vr.end()); vector<Road> main_r; for (int i = 0; i < m; i++) { int v, u, d; cin >> v >> u >> d; v--; u--; gr[v].push_back({u, d}); gr[u].push_back({v, d}); if (vr.size() && i == vr.front()) { main_r.push_back({v, u, d}); vr.erase(vr.begin()); } } set<int> s_main_c; for (int i = 0; i < r; i++) { s_main_c.insert(main_r[i].u); s_main_c.insert(main_r[i].v); } s_main_c.insert(0); s_main_c.insert(n - 1); vector<int> main_c(s_main_c.begin(), s_main_c.end()); int k = main_c.size(); int mat[k][k]; map<int, int> mp; for (int i = 0; i < k; i++) { mp[main_c[i]] = i; vector<int> dist = dijkstra(main_c[i]); for (int j = 0; j < k; j++) { mat[i][j] = dist[main_c[j]]; } } for (int i = 0; i < k; i++) { for (int j = 0; j < (1 << r); j++) { dist[i][j] = INF; } dist[i][0] = mat[0][i]; } for (int s = 1; s < (1 << r); s++) { for (int i = 0; i < r; i++) { if (s & (1 << i)) { int v = mp[main_r[i].v], u = mp[main_r[i].u], d = main_r[i].d; dist[v][s] = min(dist[v][s], dist[u][s ^ (1 << i)] + d); dist[u][s] = min(dist[u][s], dist[v][s ^ (1 << i)] + d); for (int j = 0; j < k; j++) { dist[j][s] = min(dist[j][s], dist[v][s] + mat[v][j]); dist[j][s] = min(dist[j][s], dist[u][s] + mat[u][j]); } } } } cout << dist[k - 1][(1 << r) - 1] << endl; return 1; } int32_t main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 1; // cin >> t; while (t--) { solve(); } return 0; }