제출 #330167

#제출 시각아이디문제언어결과실행 시간메모리
330167casperwangCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
476 ms25320 KiB
#include <bits/stdc++.h> #define int long long #define pii pair<int,int> #define ff first #define ss second #define pb push_back using namespace std; const int MAXN = 100000; const int INF = 1e18; int n, m; int s, t, u, v; int a, b, c; vector <pii> path[MAXN+1]; int dpU[MAXN+1]; int dpV[MAXN+1]; pii arr[MAXN+1]; int ans = INF; priority_queue <pii, vector<pii>, greater<pii>> nxt; class Dijkstra{ private: priority_queue <pii, vector<pii>, greater<pii>> nxt; public: int dis[MAXN+1]; void init() { for (int i = 1; i <= n; i++) dis[i] = INF; } void shortest_path(int s) { init(); dis[s] = 0; nxt.push(pii(0, s)); while (nxt.size()) { int now = nxt.top().ss, d = nxt.top().ff; nxt.pop(); for (pii e : path[now]) { if (d + e.ss < dis[e.ff]) { dis[e.ff] = d + e.ss; nxt.push(pii(dis[e.ff], e.ff)); } } } } } S, T, U, V; void solve(int s) { for (int i = 1; i <= n; i++) arr[i] = pii(S.dis[i], i); sort(arr+1, arr+1+n); for (int i = 1; i <= n; i++) { int now = arr[i].ss, d = arr[i].ff; for (pii e : path[now]) { if (d + e.ss == S.dis[e.ff] && S.dis[now] + e.ss + T.dis[e.ff] == T.dis[s]) { dpU[e.ff] = min(dpU[e.ff], dpU[now]); dpV[e.ff] = min(dpV[e.ff], dpV[now]); } } } for (int i = 1; i <= n; i++) { ans = min(ans, dpU[i] + V.dis[i]); ans = min(ans, dpV[i] + U.dis[i]); } } signed main() { ios_base::sync_with_stdio(0), cin.tie(0); cin >> n >> m; cin >> s >> t; cin >> u >> v; for (int i = 0; i < m; i++) { cin >> a >> b >> c; path[a].pb(pii(b, c)); path[b].pb(pii(a, c)); } U.shortest_path(u); V.shortest_path(v); for (int i = 1; i <= n; i++) dpU[i] = U.dis[i]; for (int i = 1; i <= n; i++) dpV[i] = V.dis[i]; S.shortest_path(s); T.shortest_path(t); solve(s); cout << ans << "\n"; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...