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 dis(int u,int u2,int n){
vector <int> d(n+1,1e10);
d[u]=0;
priority_queue <pair <int,int> > pq;
pq.push({0,u});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
for(auto to : g[v]){
if(d[to.ff]>d[v]+to.ss){
d[to.ff]=d[v]+to.ss;
pq.push({d[to.ff],to.ff});
}
}
}
return d[u2];
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
int n,m;
cin>>n>>m;
vector <vector <int> > dist(n+1,vector <int> (n+1,1e10));
vector <int> a(m),b(m),c(m),d(m);
for(int i=0;i<m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
g[a[i]].insert({b[i],c[i]});
dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
}
for(int i=1;i<=n;i++)dist[i][i]=0;
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=1e10;
mn=min(mn,dist[1][n]+dist[n][1]);
for(int i=1;i<=m;i++){
if(min(dist[1][n],dist[1][b[i]]+dist[a[i]][n]+c[i])+min(dist[n][1],dist[n][b[i]]+dist[a[i]][1]+c[i])+d[i]>=mn)continue;
g[a[i]].erase(g[a[i]].find({b[i],c[i]}));
g[b[i]].insert({a[i],c[i]});
int x=dis(1,n,n);
int y=dis(n,1,n);
mn=min(mn,x+y+d[i]);
g[b[i]].erase(g[b[i]].find({a[i],c[i]}));
g[a[i]].insert({b[i],c[i]});
}
if(mn==1e10)mn=-1;
cout<<mn<<"\n";
}
/*
*/
# | 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... |