제출 #605635

#제출 시각아이디문제언어결과실행 시간메모리
605635Ronin13Commuter Pass (JOI18_commuter_pass)C++14
100 / 100
531 ms35264 KiB
#include<bits/stdc++.h> #define ll long long #define ull unsigned ll #define f first #define s second #define pii pair<int,int> #define pll pair<ll,ll> #define pb push_back #define epb emplace_back using namespace std; const int NMAX = 1e5 + 1; vector <vector <pll> > g(NMAX); ll d[NMAX][4]; vector <vector <int> > p(NMAX); void Dijkstra(int n, int s, int ind){ d[s][ind] = 0; priority_queue <pll> q; q.push({0, s}); vector <bool> used(n + 1); while(!q.empty()){ int v = q.top().s; q.pop(); if(used[v]) continue; used[v] = true; for(auto x : g[v]){ int to = x.f; ll l = x.s; if(d[to][ind] > d[v][ind] + l){ d[to][ind] = d[v][ind] + l; q.push({-d[to][ind], to}); if(!ind){ p[to].clear(); p[to].pb(v); } } else{ if(d[to][ind] == d[v][ind] + l){ if(!ind) p[to].pb(v); } } } } } vector <vector <int> > G(NMAX); stack <int> st, st1; vector <bool> used(NMAX, false); void dfs(int v){ used[v] = true; for(int to : G[v]){ if(used[to]) continue; dfs(to); } st.push(v); st1.push(v); } int main(){ int n, m; cin >> n >> m; int s, t; cin >> s >> t; int x, y; cin >> x >> y; for(int i = 1; i <= m; i++){ int u, v; ll c; cin >> u >> v >> c; g[u].pb({v, c}); g[v].pb({u, c}); } for(int i = 1; i <= n; i++){ for(int j = 0; j < 4; j++) d[i][j] = 1e18; } Dijkstra(n, s, 0); Dijkstra(n, t, 1); Dijkstra(n, x, 2); Dijkstra(n, y, 3); for(int i = 1; i <= n; i++){ for(int to : p[i]) G[to].pb(i); } dfs(s); ll ans[n + 1]; for(int i = 1; i <= n; i++){ ans[i] = 1e18; } ll res = d[x][3]; while(!st.empty()){ int v = st.top(); st.pop(); if(d[t][0] != d[v][0] + d[v][1]) continue; ans[v] = min(d[v][2], ans[v]); res = min(res, ans[v] + d[v][3]); for(int to : G[v]){ ans[to] = min(ans[to], ans[v]); } } for(int i = 1; i <= n; i++){ ans[i] = 1e18; } while(!st1.empty()){ int v = st1.top(); st1.pop(); if(d[t][0] != d[v][0] + d[v][1]) continue; ans[v] = min(d[v][3], ans[v]); res = min(res, ans[v] + d[v][2]); for(int to : G[v]){ ans[to] = min(ans[to], ans[v]); } } cout << res; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...