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>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const long long inf=1e18;
struct edg{
int u,v;
long long c,d;
}b[50009];
#define pil pair<int,long long>
vector<pil>a[209];
struct cus{
int u;
long long val;
bool operator < (const cus &p)const {
return val>p.val;
}
};
int p1[209],p2[209],rp1[209],rp2[209];
bool use[50009];
long long d[209][209];
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
int n,m;
cin>>n>>m;
for1(i,1,m)cin>>b[i].u>>b[i].v>>b[i].c>>b[i].d;
for1(i,1,n){
for1(j,1,n){
d[i][j]=inf;
}
d[i][i]=0;
}
for1(i,1,m){
d[b[i].u][b[i].v]=min(d[b[i].u][b[i].v],b[i].c+1);
}
for1(i,1,n){
for1(j,1,n){
for1(k,1,n){
d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
}
}
}
auto dijkstra=[&](int s,int t){
vector<bool>vis(n+1);
vector<long long>d1(n+1);
for1(i,1,n)d1[i]=inf;
d1[s]=0;
priority_queue<cus>t1;
t1.push({s,0});
while (!t1.empty()){
auto u=t1.top();
t1.pop();
if (vis[u.u])continue;
vis[u.u]=1;
for (auto v:a[u.u]){
long long val=u.val+v.se;
if (val<d1[v.fi]){
d1[v.fi]=val;
t1.push({v.fi,d1[v.fi]});
}
}
}
return d1[t];
};
for1(i,1,n)a[i].clear();
for1(i,1,m){
if (d[1][b[i].u]+b[i].c+1==d[1][b[i].v]&&!p1[b[i].v]){
p1[b[i].v]=b[i].u;
use[i]=1;
}
if (d[n][b[i].u]+b[i].c+1==d[n][b[i].v]&&!p2[b[i].v]){
p2[b[i].v]=b[i].u;
use[i]=1;
}
if (d[b[i].v][n]+b[i].c+1==d[b[i].u][n]&&!rp1[b[i].u]){
rp1[b[i].u]=b[i].v;
use[i]=1;
}
if (d[b[i].v][1]+b[i].c+1==d[b[i].u][1]&&!rp2[b[i].u]){
rp2[b[i].u]=b[i].v;
use[i]=1;
}
}
for1(i,1,n){
for1(j,1,n){
d[i][j]=inf;
}
d[i][i]=0;
}
for1(i,1,m){
d[b[i].u][b[i].v]=min(d[b[i].u][b[i].v],b[i].c);
}
for1(i,1,n){
for1(j,1,n){
for1(k,1,n){
d[j][k]=min(d[j][k],d[j][i]+d[i][k]);
}
}
}
long long ans=d[1][n]+d[n][1];
auto invert=[&](int id){
if (use[id]){
for1(i,1,n)a[i].clear();
swap(b[id].u,b[id].v);
for1(i,1,m){
a[b[i].u].pb({b[i].v,b[i].c});
}
swap(b[id].u,b[id].v);
return b[id].d+dijkstra(1,n)+dijkstra(n,1);
}
return b[id].d+min(d[1][n],d[1][b[id].v]+b[id].c+d[b[id].u][n])+min(d[n][1],d[n][b[id].v]+b[id].c+d[b[id].u][1]);
};
for1(i,1,m){
ans=min(ans,invert(i));
}
if (ans>=inf)ans=-1;
cout<<ans;
}
# | 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... |