Submission #417283

#TimeUsernameProblemLanguageResultExecution timeMemory
417283AmineWeslatiOlympic Bus (JOI20_ho_t4)C++14
37 / 100
1071 ms4972 KiB
#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef vector<int>vi; #define pb push_back #define sz(x) (int)x.size() #define all(x) begin(x),end(x) typedef pair<int,int>pi; #define fi first #define se second #define FOR(i,a,b) for(int i=a; i<b; i++) #define ROF(i,a,b) for(int i=b-1; i>=a; i--) void IO() { #ifdef LOCAL freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout); #endif } //-------------------------------- void ckmin(ll &x, ll y){x=min(x,y);} const ll INF=1e18; const int MX=5e4+10; int N,M,U[MX],V[MX],W[MX],C[MX]; vector<pair<pi,int>>adj[MX]; void build(){ FOR(i,1,N+1) adj[i].clear(); FOR(i,0,M){ int u=U[i],v=V[i],w=W[i]; adj[u].pb({{v,w},i}); } } ll dist[MX]; int par[MX],par_idx[MX]; void dijkstra(int s){ FOR(i,1,N+1) dist[i]=INF; dist[s]=0; priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>q; q.push({0,s}); while(sz(q)){ int u=q.top().se; ll d=q.top().fi; q.pop(); if(dist[u]<d) continue; for(auto it: adj[u]){ int v=it.fi.fi,w=it.fi.se,idx=it.se; if(dist[v]>d+w){ dist[v]=d+w; par[v]=u; par_idx[v]=idx; q.push({dist[v],v}); } } } } vi chosen(MX,0); ll to1[MX],toN[MX],from1[MX],fromN[MX]; void precompute(){ build(); dijkstra(1); FOR(i,1,N+1) from1[i]=dist[i]; //FOR(i,1,N+1) cout << dist[i] << ' '; cout << endl; int u=N; while(dist[N]!=INF && u!=1){ chosen[par_idx[u]]=1; u=par[u]; } dijkstra(N); FOR(i,1,N+1) fromN[i]=dist[i]; u=1; while(dist[1]!=INF && u!=N){ chosen[par_idx[u]]=1; u=par[u]; } FOR(i,0,M) swap(U[i],V[i]); build(); FOR(i,0,M) swap(U[i],V[i]); dijkstra(1); FOR(i,1,N+1) to1[i]=dist[i]; dijkstra(N); FOR(i,1,N+1) toN[i]=dist[i]; } int main(){ IO(); cin>>N>>M; FOR(i,0,M) cin>>U[i]>>V[i]>>W[i]>>C[i]; precompute(); ll ans=toN[1]+to1[N]; FOR(i,0,M){ if(chosen[i]){ swap(U[i],V[i]); build(); swap(U[i],V[i]); ll x=C[i]; dijkstra(1); x+=dist[N]; dijkstra(N); x+=dist[1]; ckmin(ans,x); } else{ int u=U[i],v=V[i]; swap(u,v); ckmin(ans,from1[u]+W[i]+C[i]+toN[v]+fromN[1]); ckmin(ans,from1[N]+fromN[u]+C[i]+W[i]+to1[v]); ckmin(ans,from1[u]+W[i]+C[i]+toN[v] +fromN[u]+W[i]+to1[v]); } } if(ans>=INF) ans=-1; cout << ans << 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...