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;
#define ll long long
vector<pair<ll,ll> >adj[200005];
ll dist[200005];
ll fromu[200005],fromv[200005],cop[200005];
bool visited[200005];
priority_queue<pair<ll,ll> >q;
ll ans[200005],ans2[200005];
ll stdist,uvdist;
void D(ll s){
dist[s]=0;
q.push({0,s});
while(!q.empty()){
pair<ll,ll>lol=q.top();
q.pop();
if(visited[lol.second]){
continue;
}
visited[lol.second]=true;
for(auto u: adj[lol.second]){
if(dist[lol.second]+u.second<dist[u.first]){
dist[u.first]=dist[lol.second]+u.second;
q.push({-dist[u.first],u.first});
}
}
}
}
void D2(ll s){
dist[s]=0;
q.push({0,s});
while(!q.empty()){
pair<ll,ll>lol=q.top();
q.pop();
if(visited[lol.second]){
continue;
}
visited[lol.second]=true;
ans[lol.second]=min(ans[lol.second],fromu[lol.second]);
for(auto u: adj[lol.second]){
if(dist[lol.second]+u.second==dist[u.first]){
ans[u.first]=min(ans[u.first],ans[lol.second]);
}
else if(dist[lol.second]+u.second<dist[u.first]){
dist[u.first]=dist[lol.second]+u.second;
ans[u.first]=ans[lol.second];
q.push({-dist[u.first],u.first});
}
}
}
}
void D3(ll s){
dist[s]=0;
q.push({0,s});
while(!q.empty()){
pair<ll,ll>lol=q.top();
q.pop();
if(visited[lol.second]){
continue;
}
visited[lol.second]=true;
ans2[lol.second]=min(ans2[lol.second],fromu[lol.second]);
for(auto u: adj[lol.second]){
if(dist[lol.second]+u.second==dist[u.first]){
ans2[u.first]=min(ans2[u.first],ans2[lol.second]);
}
else if(dist[lol.second]+u.second<dist[u.first]){
dist[u.first]=dist[lol.second]+u.second;
ans2[u.first]=ans2[lol.second];
q.push({-dist[u.first],u.first});
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
ll n,m,s,t,u,v;
cin>>n>>m>>s>>t>>u>>v;
ll a[m+5],b[m+5],c[m+5];
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i];
adj[a[i]].push_back({b[i],c[i]});
adj[b[i]].push_back({a[i],c[i]});
}
for(int i=1;i<=n;i++){
dist[i]=1e17;
visited[i]=false;
}
D(u);
uvdist=dist[v];
for(int i=1;i<=n;i++){
fromu[i]=dist[i];
// cout<<dist[i]<<" ";
dist[i]=1e17;
visited[i]=false;
}
// cout<<'\n';
D(v);
for(int i=1;i<=n;i++){
fromv[i]=dist[i];
// cout<<dist[i]<<" ";
dist[i]=1e17;
visited[i]=false;
}
for(int i=1;i<=n;i++){
ans[i]=1e17;
ans2[i]=1e17;
// cout<<ans[i]<<" ";
}
D2(s);
stdist=dist[t];
for(int i=1;i<=n;i++){
cop[i]=dist[i];
dist[i]=1e17;
visited[i]=false;
}
D3(t);
// cout<<uvdist<<'\n';
for(int i=1;i<=n;i++){
if(cop[i]+dist[i]==stdist){
// cout<<i<<" "<<cop[i]<<" "<<dist[i]<<" ";
// cout<<ans[i]<<" "<<fromv[i]<<'\n';
uvdist=min(uvdist,min(ans[i],ans2[i])+fromv[i]);
// cout<<i<<" ";
}
}
// for(int i=1;i<=n;i++){
// cout<<ans[i]<<" "<<ans2[i]<<"\n";
// }
cout<<uvdist<<'\n';
}
# | 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... |