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;
typedef pair<LL,LL> pii;
const LL MAXN=100005,INF=1e18;
vector<pii> adj[MAXN];
map<LL,LL> dist[MAXN];
LL s,t,u,v,dp[MAXN][2];
LL DP(LL now,LL stat){
LL &ret=dp[now][stat];
if(ret!=-1)return ret;
ret=dist[v][now];
for(auto nxt : adj[now]){
if(stat==0){
if(dist[s][now]+nxt.second+dist[t][nxt.first]==dist[s][t])ret=min(ret,DP(nxt.first,stat));
}
else{
if(dist[t][now]+nxt.second+dist[s][nxt.first]==dist[t][s])ret=min(ret,DP(nxt.first,stat));
}
}
return ret;
}
int main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
LL n,m,U,V,w;
cin >> n >> m >> s >> t >> u >> v;
for(LL i=1;i<=m;i++){
cin >> U >> V >> w;
adj[U].push_back({V,w});
adj[V].push_back({U,w});
}
for(LL i=1;i<=n;i++){
if(i!=s && i!=t && i!=u && i!=v)continue;
for(LL j=1;j<=n;j++)dist[i][j]=INF;
dist[i][i]=0;
priority_queue<pii,vector<pii>,greater<pii> > PQ;
PQ.push({0,i});
while(!PQ.empty()){
pii now=PQ.top();
PQ.pop();
if(dist[i][now.second]<now.first)continue;
for(auto nxt : adj[now.second]){
if(dist[i][now.second]+nxt.second<dist[i][nxt.first]){
dist[i][nxt.first]=dist[i][now.second]+nxt.second;
PQ.push({dist[i][nxt.first],nxt.first});
}
}
}
}
memset(dp,-1,sizeof(dp));
LL ans=dist[u][v];
for(LL i=1;i<=n;i++){
if(dist[s][i]+dist[t][i]==dist[s][t])ans=min(ans,dist[u][i]+min(DP(i,0),DP(i,1)));
}
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... |