#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 |
1 |
Correct |
7 ms |
856 KB |
Output is correct |
2 |
Correct |
6 ms |
816 KB |
Output is correct |
3 |
Correct |
6 ms |
880 KB |
Output is correct |
4 |
Correct |
6 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
103 ms |
860 KB |
Output is correct |
11 |
Correct |
500 ms |
864 KB |
Output is correct |
12 |
Correct |
22 ms |
856 KB |
Output is correct |
13 |
Correct |
10 ms |
880 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
9 ms |
880 KB |
Output is correct |
16 |
Correct |
10 ms |
860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
28 ms |
5456 KB |
Output is correct |
2 |
Correct |
271 ms |
5468 KB |
Output is correct |
3 |
Correct |
24 ms |
5468 KB |
Output is correct |
4 |
Correct |
11 ms |
860 KB |
Output is correct |
5 |
Correct |
9 ms |
860 KB |
Output is correct |
6 |
Correct |
6 ms |
604 KB |
Output is correct |
7 |
Correct |
6 ms |
824 KB |
Output is correct |
8 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
9 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
860 KB |
Output is correct |
2 |
Correct |
6 ms |
604 KB |
Output is correct |
3 |
Correct |
22 ms |
4444 KB |
Output is correct |
4 |
Correct |
9 ms |
604 KB |
Output is correct |
5 |
Correct |
28 ms |
5468 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
54 ms |
5468 KB |
Output is correct |
9 |
Correct |
64 ms |
5460 KB |
Output is correct |
10 |
Correct |
29 ms |
5460 KB |
Output is correct |
11 |
Correct |
31 ms |
5468 KB |
Output is correct |
12 |
Correct |
27 ms |
5468 KB |
Output is correct |
13 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
856 KB |
Output is correct |
2 |
Correct |
6 ms |
816 KB |
Output is correct |
3 |
Correct |
6 ms |
880 KB |
Output is correct |
4 |
Correct |
6 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
6 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
103 ms |
860 KB |
Output is correct |
11 |
Correct |
500 ms |
864 KB |
Output is correct |
12 |
Correct |
22 ms |
856 KB |
Output is correct |
13 |
Correct |
10 ms |
880 KB |
Output is correct |
14 |
Correct |
7 ms |
860 KB |
Output is correct |
15 |
Correct |
9 ms |
880 KB |
Output is correct |
16 |
Correct |
10 ms |
860 KB |
Output is correct |
17 |
Correct |
28 ms |
5456 KB |
Output is correct |
18 |
Correct |
271 ms |
5468 KB |
Output is correct |
19 |
Correct |
24 ms |
5468 KB |
Output is correct |
20 |
Correct |
11 ms |
860 KB |
Output is correct |
21 |
Correct |
9 ms |
860 KB |
Output is correct |
22 |
Correct |
6 ms |
604 KB |
Output is correct |
23 |
Correct |
6 ms |
824 KB |
Output is correct |
24 |
Runtime error |
1 ms |
604 KB |
Execution killed with signal 11 |
25 |
Halted |
0 ms |
0 KB |
- |