Submission #704551

#TimeUsernameProblemLanguageResultExecution timeMemory
704551089487Commuter Pass (JOI18_commuter_pass)C++14
31 / 100
2037 ms38968 KiB
#pragma GCC optimzize("Ofast,no-stack-protector") #include<bits/stdc++.h> #define int long long #define quick ios::sync_with_stdio(0);cin.tie(0); #define rep(x,a,b) for(int x=a;x<=b;x++) #define repd(x,a,b) for(int x=a;x>=b;x--) #define lowbit(x) (x&-x) #define sz(x) (int)(x.size()) #define F first #define S second #define all(x) x.begin(),x.end() #define mp make_pair #define eb emplace_back using namespace std; typedef complex<int> P; #define X real() #define Y imag() typedef pair<int,int> pii; void debug(){ cout<<"\n"; } template <class T,class ... U > void debug(T a, U ... b){ cout<<a<<" ",debug(b...); } const int N=2e5+7; const int INF=1e18; int dis[N]; int dis2[N]; vector<pii> v[N]; int in[N]; vector<pii> v2[N]; vector<int> From[N]; int D[301][301]; int p[N]; int sz[N]; void init(int n){ rep(i,1,n){ p[i]=i,sz[i]=1; } } int fp(int x){ if(x!=p[x]) p[x]=fp(p[x]); return p[x]; } void Union(int a,int b){ a=fp(a);b=fp(b); if(a==b) return ; if(sz[a]<sz[b]) swap(a,b); sz[a]+=sz[b]; p[b]=a; } vector<int> vec; int s,e,f,t,n; int ans=INF; void solve(){ vector<tuple<int,int,int> > rev; for(int i:vec){ rev.eb(make_tuple(s,i,D[s][i])); rev.eb(make_tuple(e,i,D[e][i])); D[s][i]=D[i][s]=0; D[e][i]=D[e][i]=0; } fill(dis,dis+n+1,INF); dis[f]=0; priority_queue<pii,vector<pii>,greater<pii> > pq; pq.push(mp(0,f)); while(sz(pq)){ pii now=pq.top(); pq.pop(); if(dis[now.S]<now.F) continue; rep(i,1,n){ if(dis[i]>dis[now.S]+D[i][now.S]){ pq.push(mp(dis[i]=dis[now.S]+D[i][now.S],i)); } } } ans=min(ans,dis[t]); for(auto[a,b,c]:rev){ D[a][b]=D[b][a]=c; } } bool invec[N]; void dfs(int now){ if(invec[now]) return ; vec.eb(now); invec[now]=true; if(now==s){ solve(); vec.pop_back(); invec[s]=false; return ; } for(int i=0;i<sz(From[now]);i++){ dfs(From[now][i]); } invec[now]=false; vec.pop_back(); } signed main(){ quick int m; cin>>n>>m; cin>>s>>e>>f>>t; if(n<=300){ rep(i,1,n){ rep(j,1,n) D[i][j]=INF; } } rep(i,1,m){ int a,b,c; cin>>a>>b>>c; v[a].eb(mp(c,b)); v[b].eb(mp(c,a)); if(n<=300){ D[a][b]=D[b][a]=min(D[a][b],c); } } fill(dis,dis+N,INF); fill(dis2,dis2+N,INF); dis[s]=0; priority_queue<pii,vector<pii> ,greater<pii> > pq; pq.push(mp(0,s)); while(sz(pq)){ pii now=pq.top(); pq.pop(); if(dis[now.S]<now.F) continue; for(pii p2:v[now.S]){ if(dis[p2.S]>dis[now.S]+p2.F){ pq.push(mp(dis[p2.S]=dis[now.S]+p2.F,p2.S)); } if(n<=300&&dis[p2.S]==dis[now.S]+p2.F){ From[p2.S].eb(now.S); } } } dis2[e]=0; pq.push(mp(0,e)); while(sz(pq)){ pii now=pq.top(); pq.pop(); if(dis2[now.S]<now.F) continue; for(pii p2:v[now.S]){ if(dis2[p2.S]>dis2[now.S]+p2.F){ pq.push(mp(dis2[p2.S]=dis2[now.S]+p2.F,p2.S)); } } } if(n<=300){ dfs(e); cout<<ans<<"\n"; return 0; } init(n); Union(s,e); rep(i,1,n){ if(dis[i]+dis2[i]==dis[e]){ Union(i,e); v[s].eb(mp(0,i)); v[i].eb(mp(0,s)); v[e].eb(mp(0,i)); v[i].eb(mp(0,e)); } } fill(dis,dis+N,INF); dis[f]=0; pq.push(mp(0,f)); while(sz(pq)){ pii now=pq.top(); pq.pop(); if(dis[now.S]<now.F) continue; for(pii p2:v[now.S]){ if(dis[p2.S]>dis[now.S]+p2.F){ pq.push(mp(dis[p2.S]=dis[now.S]+p2.F,p2.S)); } } } cout<<dis[t]<<"\n"; return 0; }

Compilation message (stderr)

commuter_pass.cpp:1: warning: ignoring '#pragma GCC optimzize' [-Wunknown-pragmas]
    1 | #pragma GCC optimzize("Ofast,no-stack-protector")
      | 
commuter_pass.cpp: In function 'void solve()':
commuter_pass.cpp:62:10: warning: operation on 'D[e][i]' may be undefined [-Wsequence-point]
   62 |   D[e][i]=D[e][i]=0;
      |   ~~~~~~~^~~~~~~~~~
commuter_pass.cpp:79:10: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   79 |  for(auto[a,b,c]:rev){
      |          ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...