# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
321554 | YJU | Olympic Bus (JOI20_ho_t4) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=2e2+5;
const ll M=5e5+5;
const ld pi=3.14159265359;
const ll INF=(1LL<<59);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
pll ed[M];
ll n,m,c[M],d[M],from[N];
vector<pll> v[N],rv[N];
ll dist[2][2][N],x,y,dis[2][2][N];
priority_queue<pll,vector<pll> ,greater<pll> > pq;
void dij(ll start,ll D[][],ll id){
REP(i,2)REP1(j,n)D[i][j]=INF;
pq.push(mp(D[0][1]=0,start));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(y>D[0][x])continue;
for(pll i:v[x]){
if(i.Y==id)continue;
if(D[0][i.X]>D[0][x]+c[i.Y]){
pq.push(mp(D[0][i.X]=D[0][x]+c[i.Y],i.X));
}
}
}
pq.push(mp(D[1][n]=0,n+1-start));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(y>D[1][x])continue;
for(pll i:rv[x]){
if(i.Y==id)continue;
if(D[1][i.X]>D[1][x]+c[i.Y]){
pq.push(mp(D[1][i.X]=D[1][x]+c[i.Y],i.X));
}
}
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>m;
REP1(i,m){
cin>>ed[i].X>>ed[i].Y>>c[i]>>d[i];
v[ed[i].X].pb(mp(ed[i].Y,i));
rv[ed[i].Y].pb(mp(ed[i].X,i));
}
dij(1,dis[0],0);
dij(n,dis[1],0);
ans=dis[0][0][n]+dis[1][0][1];
REP1(i,m){
if(from[ed[i].Y]==i){
dij(1,dist[0],i);
dij(n,dist[1],i);
ans=min(ans,d[i]+min(dist[0][0][n],dist[0][0][ed[i].Y]+c[i]+dist[0][1][ed[i].X])+min(dist[1][0][1],dist[1][0][ed[i].Y]+c[i]+dist[1][1][ed[i].X]));
}else{
ans=min(ans,d[i]+min(dis[0][0][n],dis[0][0][ed[i].Y]+c[i]+dis[0][1][ed[i].X])+min(dis[1][0][1],dis[1][0][ed[i].Y]+c[i]+dis[1][1][ed[i].X]));
}
}
cout<<(ans>=INF?-1:ans)<<"\n";
return 0;
}