結果
問題 | No.1812 Uribo Road |
ユーザー |
![]() |
提出日時 | 2022-01-09 16:01:47 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 237 ms / 5,000 ms |
コード長 | 2,714 bytes |
コンパイル時間 | 4,729 ms |
コンパイル使用メモリ | 265,816 KB |
最終ジャッジ日時 | 2025-01-27 10:10:32 |
ジャッジサーバーID (参考情報) |
judge5 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 30 |
ソースコード
#include <math.h> #include <bits/stdc++.h> #include <atcoder/all> using namespace std; using namespace atcoder; #define rep(i, l, n) for (int i = (l); i < (n); i++) #define ite(i, a) for(auto& i : a) #define all(x) x.begin(), x.end() #define lb lower_bound #define ub upper_bound #define lbi(A, x) (ll)(lb(all(A), x) - A.begin()) #define ubi(A, x) (ll)(ub(all(A), x) - A.begin()) using ll = long long; using Pair = pair<int, int>; template <class T> using V = vector<T>; template <class T> using VV = V<V<T> >; const ll INF = 1000000000000000001; const int inf = 2000000000; const ll mod = 1000000007; const ll MOD = 998244353; template <class T> inline V<T> getList(int n) { V<T> res(n); rep(i, 0, n) { cin >> res[i]; }return res; } template <class T> inline VV<T> getGrid(int m, int n) { VV<T> res(m, V<T>(n)); rep(i, 0, m) { res[i] = getList<T>(n); }return res; } inline V<int> dtois(string& s) { V<int> vec = {}; for (auto& e : s) { vec.push_back(e - '0'); } return vec; } V<int> dijkstra(int s, int n, VV<Pair>& route) { V<int> dist(n, -1); priority_queue<Pair, V<Pair>, greater<Pair> > pq; pq.push({ 0,s }); int cnt = 0; while (pq.size() and cnt < n) { Pair p = pq.top(); pq.pop(); int d = p.first; int v = p.second; if (dist[v] == -1) { dist[v] = d; cnt++; rep(j, 0, route[v].size()) { Pair p = { d + route[v][j].second,route[v][j].first }; pq.push(p); } } } return dist; } int main(void) { int n, m, k; cin >> n >> m >> k; V<int> r(k); rep(i, 0, k) { cin >> r[i]; r[i]--; } VV<Pair> route(n, V<Pair>(0)); VV<int> edge(m, V<int>(3)); rep(i, 0, m) { rep(j, 0, 3) { cin >> edge[i][j]; } edge[i][0]--; edge[i][1]--; route[edge[i][0]].push_back({ edge[i][1],edge[i][2] }); route[edge[i][1]].push_back({ edge[i][0],edge[i][2] }); } VV<int> dist(n, V<int>(0)); set<int> st = { 0,n - 1 }; ite(i, r) { st.insert(edge[i][0]); st.insert(edge[i][1]); } for (auto itr = st.begin(); itr != st.end(); itr++) { auto i = *itr; dist[i] = dijkstra(i, n, route); } V<VV<int> > dp(1 << k, VV<int>(k, V<int>(2, inf))); rep(i, 0, k) { rep(j, 0, 2) { dp[1 << i][i][j] = dist[0][edge[r[i]][j ^ 1]] + edge[r[i]][2]; } } rep(bit, 1, 1 << k) { rep(s, 0, k) { if (((bit >> s) & 1) == 0) { continue; } rep(t, 0, k) { if ((bit >> t) & 1) { continue; } rep(x, 0, 2) { rep(y, 0, 2) { int b = bit | (1 << t); dp[b][t][y] = min(dp[b][t][y], dp[bit][s][x] + dist[edge[r[s]][x]][edge[r[t]][y ^ 1]] + edge[r[t]][2]); } } } } } int ans = inf; rep(i, 0, k) { rep(j, 0, 2) { ans = min(ans, dp[(1 << k) - 1][i][j] + dist[edge[r[i]][j]][n - 1]); } } cout << ans << endl; return 0; }