This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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];
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();
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);
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])
Q.push(i);
}
cout<<Ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |