結果
問題 | No.2805 Go to School |
ユーザー |
|
提出日時 | 2024-07-12 21:31:09 |
言語 | C++17 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 256 ms / 2,000 ms |
コード長 | 2,159 bytes |
コンパイル時間 | 2,555 ms |
コンパイル使用メモリ | 209,692 KB |
最終ジャッジ日時 | 2025-02-22 03:36:51 |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 35 |
ソースコード
#include <bits/stdc++.h>using namespace std;vector<long long> dijkstra(vector<vector<pair<int,long long>>> &Graph,int start){int N = Graph.size();//O((V+E)logV) 一般最短経路魔法.long long inf = 3e18;vector<bool> used(N);vector<long long> ret(N,inf);priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> Q;ret.at(start) = 0; Q.push({0,start});while(Q.size()){auto[nowd,pos] = Q.top(); Q.pop();if(used.at(pos)) continue;used.at(pos) = true;for(auto [to,w] : Graph.at(pos)){if(ret.at(to) > nowd+w){ret.at(to) = nowd+w;Q.push({ret.at(to),to});}}}return ret;}vector<long long> dijkstra2(vector<vector<pair<int,long long>>> &Graph,int start){int N = Graph.size();//O(V^2) 密グラフ専用.long long inf = 3e18;vector<bool> used(N);vector<long long> ret(N,inf);ret.at(start) = 0;while(true){long long nowd = inf,pos = -1;for(int i=0; i<N; i++){if(used.at(i)) continue;if(nowd > ret.at(i)) nowd = ret.at(i),pos = i;}if(pos == -1) break;used.at(pos) = true;for(auto [to,w] : Graph.at(pos)){if(ret.at(to) > nowd+w) ret.at(to) = nowd+w;}}return ret;}int main() {ios_base::sync_with_stdio(false);cin.tie(nullptr);long long N,M,L,S,E; cin >> N >> M >> L >> S >> E;vector<vector<pair<int,long long>>> Graph(N+1);for(int i=0; i<M; i++){int u,v; cin >> u >> v;u--; v--;int w; cin >> w;Graph.at(u).push_back({v,w});Graph.at(v).push_back({u,w});}vector<int> T(L);for(auto &t : T) cin >> t,t--;auto dist = dijkstra(Graph,0);if(dist.at(N-1) < S+E){cout << max(S+1,dist.at(N-1)+1) << "\n"; return 0;}for(auto &t : T){if(dist.at(t) >= S+E) continue;Graph.at(N).push_back({t,max(S+1,dist.at(t)+1)});}auto dist2 = dijkstra(Graph,N);if(dist2.at(N-1) >= 1e18) cout << -1 << "\n";else cout << dist2.at(N-1) << "\n";}