제출 #851713

#제출 시각아이디문제언어결과실행 시간메모리
851713dwuyCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
276 ms29908 KiB
#include <bits/stdc++.h> #define fi first #define se second #define int long long using namespace std; typedef pair<int, int> pii; const long long OO = 1e18; const int INF = 1e9; const int MX = 200005; int n, m, S, T, U, V; vector<pii> G[MX]; void nhap(){ cin >> n >> m; cin >> S >> T; cin >> U >> V; for(int i=1; i<=m; i++){ int u, v, c; cin >> u >> v >> c; G[u].push_back({v, c}); G[v].push_back({u, c}); } } void dijkstra(int source, vector<int> &d){ d[source] = 0; priority_queue<pair<int, int>> Q; Q.push({0, source}); while(Q.size()){ int du, u; tie(du, u) = Q.top(); du = -du; Q.pop(); if(du!=d[u]) continue; for(pii &tmp: G[u]){ int v, c; tie(v, c) = tmp; if(d[v]>du+c){ d[v] = du + c; Q.push({-d[v], v}); } } } } void sub1(){ vector<int> dS(n+5, OO); vector<int> dT(n+5, OO); vector<int> dV(n+5, OO); dijkstra(S, dS); dijkstra(T, dT); dijkstra(V, dV); int ans = dS[V]; for(int i=1; i<=n; i++){ if(dS[i] + dT[i] == dS[T]){ ans = min(ans, dV[i]); } } cout << ans; } int dsub2[305][305]; void sub2(){ memset(dsub2, 0x3f, sizeof(dsub2)); for(int i=1; i<=n; i++){ dsub2[i][i] = 0; for(pii &tmp: G[i]){ dsub2[i][tmp.fi] = tmp.se; } } for(int k=1; k<=n; k++){ for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ dsub2[i][j] = min(dsub2[i][j], dsub2[i][k] + dsub2[k][j]); } } } int ans = dsub2[U][V]; for(int i=1; i<=n; i++){ for(int j=1; j<=n; j++){ if(dsub2[S][i] + dsub2[i][j] + dsub2[j][T] == dsub2[S][T]){ ans = min(ans, min(dsub2[U][i] + dsub2[j][V], dsub2[V][i] + dsub2[j][U])); } } } cout << ans; } vector<int> rG[MX]; void sub3(){ vector<int> dS(n+5, OO); vector<int> dT(n+5, OO); vector<int> dU(n+5, OO); vector<int> dV(n+5, OO); /// thieu S dijkstra(T, dT); dijkstra(U, dU); dijkstra(V, dV); dS[S] = 0; priority_queue<pair<int, int>> Q; Q.push({0, S}); while(Q.size()){ int du, u; tie(du, u) = Q.top(); du = -du; Q.pop(); if(du!=dS[u]) continue; for(pii &tmp: G[u]){ int v, c; tie(v, c) = tmp; if(dS[v]>du+c){ dS[v] = du + c; Q.push({-dS[v], v}); rG[v].clear(); rG[v].push_back(u); } else if(dS[v]==du+c) rG[v].push_back(u); } } int ans = dU[V]; vector<int> d(n+5, OO); d[T] = dU[T]; Q.push({-d[T], T}); while(Q.size()){ int du, u; tie(du, u) = Q.top(); du = -du; Q.pop(); if(du!=d[u]) continue; ans = min(ans, du + dV[u]); for(int v: rG[u]){ if(d[v]>min(du, dU[v])){ d[v] = min(du, dU[v]); Q.push({-d[v], v}); } } } d = vector<int>(n+5, OO); d[T] = dV[T]; Q.push({-d[T], T}); while(Q.size()){ int du, u; tie(du, u) = Q.top(); du = -du; Q.pop(); if(du!=d[u]) continue; ans = min(ans, du + dU[u]); for(int v: rG[u]){ if(d[v]>min(du, dV[v])){ d[v] = min(du, dV[v]); Q.push({-d[v], v}); } } } cout << ans; } void solve(){ if(S==U){ sub1(); return; } if(n<=303){ sub2(); return; } sub3(); } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); nhap(); solve(); return 0; } /** 11 12 1 8 11 2 1 2 9 1 4 8 2 3 7 3 10 3 3 11 3 4 5 5 4 6 1 4 10 5 5 7 1 7 11 0 7 8 5 7 9 8 6 6 1 6 1 4 1 2 1 2 3 1 3 5 1 2 4 3 4 5 2 5 6 1 **/
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...