제출 #854197

#제출 시각아이디문제언어결과실행 시간메모리
854197LeVanThucCommuter Pass (JOI18_commuter_pass)C++17
100 / 100
409 ms50412 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define fi first #define se second #define p(x,y) pair<ll,ll>(x,y) #define BIT(i,x) ((x>>i)&1) #define MASK(x) (1<<x) #define ld long double #define __builtin_popcount __builtin_popcountll #define pll pair<ll,ll> template<class T1,class T2> bool maximize(T1 &x,const T2 &y) { if(x<y) { x=y; return 1; } return 0; } template<class T1,class T2> bool minimize(T1 &x,const T2 &y) { if(x>y) { x=y; return 1; } return 0; } void online() { std::ios_base::sync_with_stdio(0); cin.tie(0); #ifdef thuc freopen("input.INP","r",stdin); freopen("output.OUT","w",stdout); #endif } const ll N=2e5+10,inf=1e18; ll dists[N],distt[N],n,m,f[N][4],U[N],V[N],C[N]; vector<pll> gr[N]; void dijkstra(ll src,ll dist[]) { priority_queue<pll,vector<pll>,greater<pll> > qq; for(int i=1;i<=n;i++) dist[i]=inf; dist[src]=0; qq.emplace(0,src); while(qq.size()) { auto [d,u]=qq.top(); qq.pop(); if(dist[u]!=d) continue; for(auto [v,c]:gr[u]) { if(minimize(dist[v],d+c)) { qq.emplace(dist[v],v); } } } } struct info { ll d,u,s; info(ll _d=0,ll _u=0,ll _s=0) { d=_d; u=_u; s=_s; } bool operator <(const info &o) const { return d>o.d; } }; ll dijkstra2(ll st,ll en) { memset(f,0x3f,sizeof f); priority_queue<info> qq; qq.emplace(0,st,0); f[st][0]=0; while(qq.size()) { auto [d,u,s]=qq.top(); qq.pop(); if(d!=f[u][s]) continue; for(auto [v,c]:gr[u]) { if(c==0) { if(s==2) continue; if(minimize(f[v][s|1],f[u][s])) { qq.emplace(f[v][s|1],v,s|1); } } else { if(s==1) { if(minimize(f[v][2],f[u][s]+c)) { qq.emplace(f[v][2],v,2); } } else { if(minimize(f[v][s],f[u][s]+c)) { qq.emplace(f[v][s],v,s); } } } } } return min(min(f[en][0],f[en][1]),f[en][2]); } int main() { online(); ll s,t,u,v; cin>>n>>m; cin>>s>>t; cin>>u>>v; for(int i=1;i<=m;i++) { ll u,v,c; cin>>u>>v>>c; U[i]=u; V[i]=v; C[i]=c; gr[u].emplace_back(v,c); gr[v].emplace_back(u,c); } dijkstra(s,dists); dijkstra(t,distt); for(int i=1;i<=m;i++) { if(dists[U[i]]+C[i]+distt[V[i]]==dists[t]) { gr[U[i]].emplace_back(V[i],0); } if(dists[V[i]]+C[i]+distt[U[i]]==dists[t]) { gr[V[i]].emplace_back(U[i],0); } } cout<<min(dijkstra2(u,v),dijkstra2(v,u)); }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...