Submission #945934

#TimeUsernameProblemLanguageResultExecution timeMemory
945934ezzzayCommuter Pass (JOI18_commuter_pass)C++14
55 / 100
313 ms34800 KiB
#include<bits/stdc++.h> using namespace std; #define int long long #define ff first #define ss second #define pb push_back const int N=3e5+5; vector<pair<int,int>>v[N]; int n,m,s,t,x,y; int dist[N]; int tmp[N]; int par[N]; int d[500][500]; bool vis[N]; vector<pair<int,int>>vc; void dfs(int a){ vis[a]=1; for(auto p:v[a]){ int b=p.ff; int c=p.ss; if(vis[b]==0 and dist[b]+c==dist[a])dfs(b); } } void sbtsk1(){ for(int i=0;i<m;i++){ int a,b,c; cin>>a>>b>>c; v[a].pb({b,c}); v[b].pb({a,c}); } for(int i=1;i<=n;i++){ dist[i]=1e16; } priority_queue<pair<int,int>>q; q.push({0,s}); par[s]=s; dist[s]=0; vis[s]=1; while(!q.empty()){ int w=-q.top().ff; int a=q.top().ss; q.pop(); if(dist[a]<w)continue; for(auto p:v[a]){ int b=p.ff; int c=p.ss; if(dist[a]+c<dist[b]){ dist[b]=dist[a]+c; par[b]=a; q.push({-dist[b],b}); } } } dfs(t); for(int i=1;i<=n;i++){ dist[i]=1e16; } s=y; q.push({0,s}); dist[s]=0; while(!q.empty()){ int w=-q.top().ff; int a=q.top().ss; q.pop(); if(dist[a]<w)continue; for(auto p:v[a]){ int b=p.ff; int c=p.ss; if(dist[a]+c<dist[b]){ dist[b]=dist[a]+c; par[b]=a; q.push({-dist[b],b}); } } } int ans=1e16; for(int i=1;i<=n;i++){ if(vis[i]){ ans=min(ans,dist[i]); } } cout<<ans; } void sbtsk2(){ for(int i=0;i<m;i++){ int a,b,c; cin>>a>>b>>c; v[a].pb({b,c}); v[b].pb({a,c}); } for(int i=1;i<=n;i++){ dist[i]=1e16; } priority_queue<pair<int,int>>q; q.push({0,s}); par[s]=s; dist[s]=0; while(!q.empty()){ int w=-q.top().ff; int a=q.top().ss; q.pop(); if(dist[a]<w)continue; for(auto p:v[a]){ int b=p.ff; int c=p.ss; if(dist[a]+c<dist[b]){ dist[b]=dist[a]+c; par[b]=a; q.push({-dist[b],b}); } } } while(t!=s){ v[t].pb({par[t],0}); v[par[t]].pb({t,0}); t=par[t]; } for(int i=1;i<=n;i++){ dist[i]=1e16; } s=x; q.push({0,s}); par[s]=s; dist[s]=0; while(!q.empty()){ int w=-q.top().ff; int a=q.top().ss; q.pop(); if(dist[a]<w)continue; for(auto p:v[a]){ int b=p.ff; int c=p.ss; if(dist[a]+c<dist[b]){ dist[b]=dist[a]+c; par[b]=a; q.push({-dist[b],b}); } } } cout<<dist[y]; } void subtsk3(){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(i==j){ d[i][j]=0; continue; } d[i][j]=1e14; } } for(int i=0;i<m;i++){ int a,b,c; cin>>a>>b>>c; d[a][b]=min(d[a][b],c); d[b][a]=min(d[b][a],c); } for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(d[i][j]> d[i][k]+d[k][j]){ d[i][j]=d[i][k]+d[k][j]; } } } } int ans=d[x][y]; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ if(d[s][i]+d[i][j]+d[j][t]==d[s][t]){ ans=min(ans,d[x][i]+d[j][y]); ans=min(ans,d[x][j]+d[i][y]); } } } cout<<ans; } signed main(){ cin>>n>>m>>s>>t>>x>>y; if(n<=300){ subtsk3(); return 0; } if(s==x){ sbtsk1(); return 0; } sbtsk2(); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...