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>
//#define int long long
#define all(x) x.begin(),x.end()
#define ff first
#define ss second
#define pb push_back
using namespace std;
multiset < pair <int,int> > g[205];
int dist[205][205];
vector <int> a,b,c,d;
priority_queue <pair <int,int> > pq;
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=1e9;
}
dist[i][i]=0;
}
a.pb(0);b.pb(0);c.pb(0);d.pb(0);
for(int i=1;i<=m;i++){
int A,B,C,D;
cin>>A>>B>>C>>D;
a.pb(A);b.pb(B);c.pb(C);d.pb(D);
dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
g[a[i]].insert({b[i],c[i]});
}
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
}
}
int mn=2e9;
if(dist[1][n]<1e9 && dist[n][1]<1e9)mn=min(dist[1][n]+dist[n][1],mn);
//cout<<mn<<"\n";
for(int i=1;i<=m;i++){
int new1n=min(dist[1][n],dist[1][b[i]]+dist[a[i]][n]+c[i]);
int newn1=min(dist[n][1],dist[n][b[i]]+dist[a[i]][1]+c[i]);
if(new1n==1e9 or newn1==1e9 or new1n+newn1+d[i]>=mn)continue;
g[a[i]].erase(g[a[i]].find({b[i],c[i]}));
g[b[i]].insert({a[i],c[i]});
vector <int> dis(n+1,1e9);
dis[1]=0;
pq.push({0,1});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
for(auto to : g[v]){
if(dis[to.ff]>dis[v]+to.ss){
dis[to.ff]=dis[v]+to.ss;
pq.push({dis[to.ff],to.ff});
}
}
}
if(dis[n]!=1e9){
int ans=dis[n];
for(int j=1;j<=n;j++){
dis[j]=1e9;
}
dis[n]=0;
pq.push({0,n});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
for(auto to : g[v]){
if(dis[to.ff]>dis[v]+to.ss){
dis[to.ff]=dis[v]+to.ss;
pq.push({dis[to.ff],to.ff});
}
}
}
if(dis[1]<1e9 && d[i]<1e9)mn=min(mn,ans+dis[1]+d[i]);
}
g[b[i]].erase(g[b[i]].find({a[i],c[i]}));
g[a[i]].insert({b[i],c[i]});
}
if(mn==2e9)mn=-1;
cout<<mn<<"\n";
}
//g[1]-r
//dis[0] 1
//dis[1] 1r
//dis[2] n
//dis[3] nr
/*
*/
# | 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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |