結果
問題 | No.788 トラックの移動 |
ユーザー |
![]() |
提出日時 | 2019-02-08 22:59:58 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 520 ms / 2,000 ms |
コード長 | 1,455 bytes |
コンパイル時間 | 2,392 ms |
コンパイル使用メモリ | 203,500 KB |
最終ジャッジ日時 | 2025-01-06 21:02:29 |
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 14 |
ソースコード
#include <bits/stdc++.h> #define int long long using namespace std; typedef pair<int, int> P; const int INF = 1e18; int n, m, l; int t[2000]; vector<P> G[2000]; int dist[2000][2000]; struct Dijkstra{ vector<int> d; Dijkstra(int V){ d.resize(V, INF); } void calc(int s){ d[s] = 0; priority_queue<P, vector<P>, greater<P> > q; q.push(P(d[s], s)); while(!q.empty()){ P p = q.top(); q.pop(); int from = p.second; int cost = p.first; if(d[from] < cost) continue; for(auto e : G[from]){ int next = e.first; int newCost = cost + e.second; if(d[next] > newCost){ d[next] = newCost; q.push(P(newCost, next)); } } } } }; signed main(){ cin >> n >> m >> l; l--; for(int i = 0; i < n; i++) cin >> t[i]; for(int i = 0; i < m; i++){ int a, b, c; cin >> a >> b >> c; a--; b--; G[a].push_back({b, c}); G[b].push_back({a, c}); } int cnt = 0; for(int i = 0; i < n; i++) if(t[i]) cnt++; if(cnt == 1){ cout << 0 << endl; return 0; } for(int i = 0; i < n; i++){ Dijkstra dk(n); dk.calc(i); for(int j = 0; j < n; j++){ dist[i][j] = dk.d[j]; } } int ans = INF; for(int i = 0; i < n; i++){ int tmp = dist[l][i]; for(int j = 0; j < n; j++){ tmp += t[j] * dist[i][j] * 2; } int MAX = 0; for(int j = 0; j < n; j++){ if(t[j] == 0) continue; MAX = max(MAX, dist[l][i] + dist[i][j] - dist[l][j]); } tmp -= MAX; ans = min(ans, tmp); } cout << ans << endl; }