Submission #77183

#TimeUsernameProblemLanguageResultExecution timeMemory
77183VardanyanCommuter Pass (JOI18_commuter_pass)C++14
31 / 100
577 ms61068 KiB
#include <bits/stdc++.h> using namespace std; const int N = 200*1000+7; const long long INF = 100000000000000005; vector<pair<int,long long> > g[N]; long long dist[N]; long long ans = INF; int n,m; int s,t; int U,V; long long distu[N],distv[N]; long long dp1[N],dp2[N]; long long distt[N]; bool c[N]; int main(){ scanf("%d%d",&n,&m); scanf("%d%d",&s,&t); scanf("%d%d",&U,&V); for(int i = 0;i<m;i++){ int x,y; long long z; scanf("%d%d%lld",&x,&y,&z); g[x].push_back({y,z}); g[y].push_back({x,z}); } priority_queue<pair<long long,int> > pq; pq.push({0,s}); for(int i = 0;i<=n;i++) dist[i] = INF; dist[s] = 0; while(!pq.empty()){ pair<long long,int> gag = pq.top(); pq.pop(); for(int i = 0;i<g[gag.second].size();i++){ int to = g[gag.second][i].first; if(dist[to]>(-gag.first)+g[gag.second][i].second){ dist[to] = (-gag.first)+g[gag.second][i].second; pq.push({-dist[to],to}); } } } pq.push({0,t}); for(int i = 0;i<=n;i++) distt[i] = INF; distt[t] = 0; while(!pq.empty()){ pair<long long,int> gag = pq.top(); pq.pop(); for(int i = 0;i<g[gag.second].size();i++){ int to = g[gag.second][i].first; if(distt[to]>(-gag.first)+g[gag.second][i].second){ distt[to] = (-gag.first)+g[gag.second][i].second; pq.push({-distt[to],to}); } } } // dfs(s); // if(all.size() == 0) assert(0); pq.push({0,U}); for(int i = 0;i<=n;i++) distu[i] = INF; distu[U] = 0; while(!pq.empty()){ pair<long long,int> gag = pq.top(); pq.pop(); for(int i = 0;i<g[gag.second].size();i++){ int to = g[gag.second][i].first; if(distu[to]>(-gag.first)+g[gag.second][i].second){ distu[to] = (-gag.first)+g[gag.second][i].second; pq.push({-distu[to],to}); } } } pq.push({0,V}); for(int i = 0;i<=n;i++) distv[i] = INF; distv[V] = 0; while(!pq.empty()){ pair<long long,int> gag = pq.top(); pq.pop(); for(int i = 0;i<g[gag.second].size();i++){ int to = g[gag.second][i].first; if(distv[to]>(-gag.first)+g[gag.second][i].second){ distv[to] = (-gag.first)+g[gag.second][i].second; pq.push({-distv[to],to}); } } } for(int i = 0;i<=n;i++) dp1[i] = INF; dp1[s] = distv[s]; c[s] = 1; // cout<<dp1[s]<<endl; pq.push({-dp1[s],s}); while(!pq.empty()){ pair<long long,int> gag = pq.top(); pq.pop(); for(int i = 0;i<g[gag.second].size();i++){ int to = g[gag.second][i].first; if(!c[to] && dp1[to]>min(-gag.first,distv[to]) && dist[t] == dist[to]+distt[to]){ dp1[to] = min(-gag.first,distv[to]); pq.push({-dp1[to],to}); } } } for(int i = 0;i<=n;i++) dp2[i] = INF; memset(c,0,sizeof(c)); dp2[t] = distv[t]; c[t] = 1; pq.push({-dp2[t],t}); while(!pq.empty()){ pair<long long,int> gag = pq.top(); pq.pop(); for(int i = 0;i<g[gag.second].size();i++){ int to = g[gag.second][i].first; if(!c[to] && dp2[to]>min(-gag.first,distv[to]) && dist[t] == dist[to]+distt[to]){ dp2[to] = min(-gag.first,distv[to]); pq.push({-dp2[to],to}); } } } ans = min(distu[V],distv[U]); for(int i = 1;i<=n;i++){ if(dist[t] == dist[i]+distt[i]){ // cout<<i<<" "<<distu[i]<<" "<<dp1[i]<<" "<<dp2[i]<<endl; ans = min(ans,distu[i]+min(dp1[i],dp2[i])); } } /*for(int i = 0;i<all.size();i++){ long long mn = INF; long long mnn = INF; for(int j = 0;j<all[i].size();j++){ mn = min(mn,distu[all[i][j]]); mnn = min(mnn,distv[all[i][j]]); } if(mn<INF && mnn<INF) ans = min(ans,mn+mnn); }*/ cout<<ans<<endl; /*for(int i = 1;i<=n;i++){ cout<<i<<" "<<dist[i]<<endl; }*/ return 0; }

Compilation message (stderr)

commuter_pass.cpp: In function 'int main()':
commuter_pass.cpp:33:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<g[gag.second].size();i++){
                       ~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:47:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<g[gag.second].size();i++){
                       ~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:63:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<g[gag.second].size();i++){
                       ~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:77:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<g[gag.second].size();i++){
                       ~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:93:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<g[gag.second].size();i++){
                       ~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:109:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 0;i<g[gag.second].size();i++){
                       ~^~~~~~~~~~~~~~~~~~~~~
commuter_pass.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&n,&m);
     ~~~~~^~~~~~~~~~~~~~
commuter_pass.cpp:17:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&s,&t);
     ~~~~~^~~~~~~~~~~~~~
commuter_pass.cpp:18:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d%d",&U,&V);
     ~~~~~^~~~~~~~~~~~~~
commuter_pass.cpp:22:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d%d%lld",&x,&y,&z);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...