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;
//#include<bits/extc++.h>
//__gnu_pbds
const int N = 1e5+5;
vector<pair<int,int> > side[N];
int n,m;
void calc(int s,vector<ll> &dis){
fill(dis.begin(),dis.end(),1e18);
dis[s]=0;
priority_queue<pair<ll,int>,vector<pair<ll,int> >,greater<pair<ll,int> >> pq;
pq.push({0,s});
while(pq.size()){
auto cur = pq.top();
pq.pop();
if(dis[cur.second]!=cur.first) continue;
for(auto i : side[cur.second]){
if(dis[i.first]>dis[cur.second]+i.second){
dis[i.first]=dis[cur.second]+i.second;
pq.push({dis[i.first],i.first});
}
}
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin>>n>>m;
int s,t;cin>>s>>t;
int u,v;cin>>u>>v;
for(int i=0;i<m;i++){
int a,b,c;cin>>a>>b>>c;
side[a].push_back({b,c});
side[b].push_back({a,c});
}
vector<ll> diss(n+1);
calc(s,diss);
vector<ll> disu(n+1);
calc(u,disu);
vector<ll> dist(n+1);
calc(t,dist);
vector<ll> disv(n+1);
calc(v,disv);
vector<int> topo(n);
for(int i=0;i<n;i++) topo[i]=i+1;
sort(topo.begin(),topo.end(),[&](const int a,const int b){return diss[a]<diss[b];});
ll ans = disu[v];
vector<ll> ogdisu = disu;
for(auto i:topo){
if(diss[i]+dist[i]!=diss[t]) continue;
for(auto e : side[i]){
if(diss[e.first]+e.second==diss[i]){
// cout<<e.first<<" "<<i<<"\n";
disu[i] =min(disu[i],disu[e.first]);
}
}
ans = min(ans,disu[i]+disv[i]);
}
//for(int i=1;i<=n;i++) cout<<disu[i]<<"\n";
for(auto i:topo){
if(diss[i]+dist[i]!=diss[t]) continue;
for(auto e : side[i]){
if(diss[e.first]+e.second==diss[i]){
disv[i] =min(disv[i],disv[e.first]);
}
}
ans = min(ans,disv[i]+ogdisu[i]);
}
cout<<ans<<"\n";
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... |