Submission #376516

#TimeUsernameProblemLanguageResultExecution timeMemory
376516astoriaCommuter Pass (JOI18_commuter_pass)C++14
100 / 100
527 ms25784 KiB
#include "bits/stdc++.h" using namespace std; #define int long long const int N=100005; int n,m; int dist[4][N]; //0=s,1=t,2=u,3=v vector<pair<int,int> > adj[N]; int mnu[2][N]; void dijk(int src, int pp){ priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq; memset(dist[pp],-1,sizeof(dist[pp])); dist[pp][src] = 0; pq.push({0,src}); while(!pq.empty()){ int v=pq.top().second, d=pq.top().first; pq.pop(); if(dist[pp][v] != d) continue; for(auto ii : adj[v]){ int nv=ii.first, nd=ii.second+d; if(dist[pp][nv]==-1 || nd<dist[pp][nv]){ dist[pp][nv] = nd; pq.push({nd,nv}); } } } } void special_dijk(int src, int pp){ int dis[N]; memset(dis,-1,sizeof(dis)); priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq; pq.push({0,src}); dis[src]=0; while(!pq.empty()){ int v=pq.top().second, d=pq.top().first; pq.pop(); if(dis[v]!=d) continue; if(mnu[pp][v]==-1) mnu[pp][v] = dist[2][v]; else mnu[pp][v] = min(mnu[pp][v], dist[2][v]); for(auto ii : adj[v]){ int nv=ii.first, nd=ii.second+d; if(dis[nv]==-1 || nd<dis[nv]){ dis[nv]=nd; pq.push({nd,nv}); mnu[pp][nv] = mnu[pp][v]; } else if(nd==dis[nv]){ if(mnu[pp][nv]==-1) mnu[pp][nv] = mnu[pp][v]; else mnu[pp][nv] = min(mnu[pp][nv],mnu[pp][v]); } } } } int32_t main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int s,t,u,v; cin>>n>>m>>s>>t>>u>>v; memset(mnu,-1,sizeof(mnu)); for(int i=0; i<m; i++){ int a,b,c; cin>>a>>b>>c; adj[a].push_back({b,c}); adj[b].push_back({a,c}); } dijk(s,0); dijk(t,1); dijk(u,2); dijk(v,3); special_dijk(s,0); special_dijk(t,1); int an=dist[2][v]; for(int i=1; i<=n; i++){ if(dist[0][i] + dist[1][i] != dist[0][t]) continue; //not on shortest path int val = min(mnu[0][i],mnu[1][i])+dist[3][i]; //if(val==0) cout<<i<<endl; an=min(an,val); } cout<<an<<endl; /*for(int i=1; i<=n; i++){ cout<<mnu[0][i]<<' '<<mnu[1][i]<<' '<<dist[3][i]<<'\n'; }*/ //cout<<dist[0][4]<<' '<<dist[1][4]<<endl; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...