제출 #524797

#제출 시각아이디문제언어결과실행 시간메모리
524797NetrCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
452 ms25680 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef pair<ll, ll> pi; #ifdef DEBUG #define D(x...) printf(x); #else #define D(x...) #endif const ll MAXN = 1e5 + 5; const ll INF = 1ll<<62; ll N,M,S,T,U,V,UD[MAXN],VD[MAXN],SD[MAXN],vis[MAXN],ans; pi dp[MAXN]; vector<pi> edge[MAXN]; pi dfs(int i){ D("%d: ud,vd = %d, %d\n", i, UD[i], VD[i]) if(i == S) return {UD[S],VD[S]}; if(vis[i]) return dp[i]; vis[i] = 1; ll udist = INF/2, vdist = INF/2; for(auto e : edge[i]){ if(SD[e.first] + e.second == SD[i]){ auto res = dfs(e.first); if(min(UD[i], res.first) + min(VD[i], res.second) < udist + vdist){ udist = min(UD[i], res.first); vdist = min(VD[i], res.second); } } } dp[i] = {udist, vdist}; return {udist, vdist}; } void djikstras(int src, ll *d, ll s){ priority_queue<pi, vector<pi>, greater<pi>> pq; fill(d, d+s+1, INF); pq.push({d[src] = 0, src}); while(!pq.empty()){ auto t = pq.top(); pq.pop(); if(d[t.second] != t.first) continue; for(auto e : edge[t.second]){ if(d[t.second] + e.second < d[e.first]){ pq.push({d[e.first] = d[t.second] + e.second, e.first}); } } } } int main(){ cin >> N >> M >> S >> T >> U >> V; for(int i = 0; i < M; i++){ ll ai, bi, ci; cin >> ai >> bi >> ci; edge[bi].push_back({ai, ci}); edge[ai].push_back({bi, ci}); } djikstras(S, SD, N); djikstras(U, UD, N); djikstras(V, VD, N); ans = UD[V]; auto res = dfs(T); ans = min(ans, res.first + res.second); 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...