제출 #563129

#제출 시각아이디문제언어결과실행 시간메모리
563129jk410Commuter Pass (JOI18_commuter_pass)C++17
100 / 100
345 ms31200 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; const ll INF=4e18; struct edge{ int v; ll cost; bool operator<(const edge &tmp)const{ return cost>tmp.cost; } }; int N,M,S,T,U,V; vector<edge> Edge[100001]; vector<int> To[100001],From[100001]; ll Dist[100001],Dist_U[100001],Dist_V[100001],Min_U[100001],Min_V[100001]; int In_deg[100001]; bool Visited[100001]; queue<int> Q; ll Ans; void dijkstra(int st,ll *dist){ for (int i=1; i<=N; i++) dist[i]=INF; dist[st]=0; priority_queue<edge> q; q.push({st,0}); while (!q.empty()){ edge t=q.top(); q.pop(); if (dist[t.v]<t.cost) continue; for (edge i:Edge[t.v]){ if (dist[i.v]>t.cost+i.cost){ dist[i.v]=t.cost+i.cost; q.push({i.v,dist[i.v]}); } } } } int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>N>>M>>S>>T>>U>>V; while (M--){ int a,b,c; cin>>a>>b>>c; Edge[a].push_back({b,c}); Edge[b].push_back({a,c}); } dijkstra(S,Dist); dijkstra(U,Dist_U); dijkstra(V,Dist_V); Q.push(T); while (!Q.empty()){ int v=Q.front(); Q.pop(); if (Visited[v]) continue; Visited[v]=true; for (edge i:Edge[v]){ if (Dist[v]==Dist[i.v]+i.cost){ To[i.v].push_back(v); From[v].push_back(i.v); In_deg[v]++; Q.push(i.v); } } } Min_U[0]=Min_V[0]=Dist_U[0]=Dist_V[0]=INF; From[S].push_back(0); Ans=Dist_U[V]; Q.push(S); while (!Q.empty()){ int v=Q.front(); Q.pop(); Min_U[v]=Min_V[v]=INF; for (int i:From[v]){ Min_U[v]=min({Min_U[v],Min_U[i],Dist_U[i]}); Min_V[v]=min({Min_V[v],Min_V[i],Dist_V[i]}); } Ans=min({Ans,Min_U[v]+Dist_V[v],Min_V[v]+Dist_U[v]}); for (int i:To[v]){ if (--In_deg[i]==0) Q.push(i); } } cout<<Ans; 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...