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(int i=0;i<n;i++)
#define REP1(i,n) for(int 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],x,y,ans,ck=1;
vector<pll> v[N],rv[N];
ll dis[2][N],rdis[2][N],from[2][N];
ll dist[2][N],rdist[2][N];
priority_queue<pll,vector<pll> ,greater<pll> > pq;
void dij(ll id,ll no){
REP1(i,n)dist[id][i]=rdist[id][i]=INF;
x=(id?n:1);
pq.push(mp(dist[id][x]=0,x));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(y>dist[id][x])continue;
for(auto i:v[x]){
if(i.Y==no)continue;
if(dist[id][i.X]>dist[id][x]+c[i.Y]){
pq.push(mp(dist[id][i.X]=dist[id][x]+c[i.Y],i.X));
}
}
}
x=(!id?n:1);
pq.push(mp(rdist[id][x]=0,x));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(y>rdist[id][x])continue;
for(auto i:rv[x]){
if(i.Y==no)continue;
if(rdist[id][i.X]>rdist[id][x]+c[i.Y]){
pq.push(mp(rdist[id][i.X]=rdist[id][x]+c[i.Y],i.X));
}
}
}
}
void path(ll id){
REP1(i,n)dis[id][i]=rdis[id][i]=INF;
x=(id?n:1);
pq.push(mp(dis[id][x]=0,x));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(y>dis[id][x])continue;
for(auto i:v[x]){
if(dis[id][i.X]>dis[id][x]+c[i.Y]){
from[id][i.X]=i.Y;
pq.push(mp(dis[id][i.X]=dis[id][x]+c[i.Y],i.X));
}
}
}
x=(!id?n:1);
pq.push(mp(rdis[id][x]=0,x));
while(SZ(pq)){
x=pq.top().Y;y=pq.top().X;pq.pop();
if(y>rdis[id][x])continue;
for(auto i:rv[x]){
if(rdis[id][i.X]>rdis[id][x]+c[i.Y]){
pq.push(mp(rdis[id][i.X]=rdis[id][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));
}
path(0);
path(1);
ans=dis[0][n]+dis[1][1];
/*REP1(i,n){
cout<<"1::"<<dis[0][i]<<" "<<rdis[0][i]<<"\n";
cout<<"N::"<<dis[1][i]<<" "<<rdis[1][i]<<"\n";
}*/
REP1(i,m){
ll res=d[i];
if(from[0][ed[i].Y]==i){
dij(0,i);
res+=min(dist[0][n],dist[0][ed[i].Y]+c[i]+rdist[0][ed[i].X]);
}else{
res+=min(dis[0][n],dis[0][ed[i].Y]+c[i]+rdis[0][ed[i].X]);
}
if(from[1][ed[i].Y]==i){
dij(1,i);
res+=min(dist[1][1],dist[1][ed[i].Y]+c[i]+rdist[1][ed[i].X]);
}else{
res+=min(dis[1][1],dis[1][ed[i].Y]+c[i]+rdis[1][ed[i].X]);
}
ans=min(ans,res);
}
cout<<(ans>=INF?-1:ans)<<"\n";
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |