#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;
const int N=5e4+5;
vector <pair <pair <int,int> ,int> > g[205];
int dist[205][205];
int a[N],b[N],c[N],d[N];
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;
}
for(int i=1;i<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
dist[a[i]][b[i]]=min(dist[a[i]][b[i]],c[i]);
g[a[i]].pb({{b[i],c[i]},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;
vector <int> dis(n+1,1e9);
dis[1]=0;
pq.push({0,1});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(v==b[i]){
if(dis[a[i]]>dis[b[i]]+c[i]){
dis[a[i]]=dis[b[i]]+c[i];
pq.push({dis[a[i]],a[i]});
}
}
for(auto to : g[v]){
if(to.ss==i)continue;
if(dis[to.ff.ff]>dis[v]+to.ff.ss){
dis[to.ff.ff]=dis[v]+to.ff.ss;
pq.push({dis[to.ff.ff],to.ff.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();
if(v==b[i]){
if(dis[a[i]]>dis[b[i]]+c[i]){
dis[a[i]]=dis[b[i]]+c[i];
pq.push({dis[a[i]],a[i]});
}
}
for(auto to : g[v]){
if(to.ss==i)continue;
if(dis[to.ff.ff]>dis[v]+to.ff.ss){
dis[to.ff.ff]=dis[v]+to.ff.ss;
pq.push({dis[to.ff.ff],to.ff.ff});
}
}
}
if(dis[1]<1e9 && d[i]<1e9)mn=min(mn,ans+dis[1]+d[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 |
1 |
Correct |
9 ms |
708 KB |
Output is correct |
2 |
Correct |
8 ms |
600 KB |
Output is correct |
3 |
Correct |
8 ms |
604 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
78 ms |
604 KB |
Output is correct |
11 |
Correct |
341 ms |
684 KB |
Output is correct |
12 |
Correct |
28 ms |
600 KB |
Output is correct |
13 |
Correct |
10 ms |
712 KB |
Output is correct |
14 |
Correct |
8 ms |
604 KB |
Output is correct |
15 |
Correct |
13 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
3444 KB |
Output is correct |
2 |
Correct |
26 ms |
3164 KB |
Output is correct |
3 |
Correct |
25 ms |
3596 KB |
Output is correct |
4 |
Correct |
11 ms |
600 KB |
Output is correct |
5 |
Correct |
10 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
9 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
80 ms |
3552 KB |
Output is correct |
10 |
Correct |
41 ms |
3416 KB |
Output is correct |
11 |
Correct |
42 ms |
3564 KB |
Output is correct |
12 |
Correct |
21 ms |
3416 KB |
Output is correct |
13 |
Correct |
21 ms |
3420 KB |
Output is correct |
14 |
Correct |
24 ms |
3676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
604 KB |
Output is correct |
2 |
Correct |
8 ms |
600 KB |
Output is correct |
3 |
Correct |
16 ms |
2652 KB |
Output is correct |
4 |
Correct |
8 ms |
600 KB |
Output is correct |
5 |
Correct |
18 ms |
3264 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
20 ms |
3304 KB |
Output is correct |
9 |
Correct |
20 ms |
3164 KB |
Output is correct |
10 |
Correct |
21 ms |
3164 KB |
Output is correct |
11 |
Correct |
18 ms |
3164 KB |
Output is correct |
12 |
Correct |
22 ms |
3160 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
18 ms |
3336 KB |
Output is correct |
20 |
Correct |
18 ms |
3112 KB |
Output is correct |
21 |
Correct |
18 ms |
3128 KB |
Output is correct |
22 |
Correct |
18 ms |
3160 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
708 KB |
Output is correct |
2 |
Correct |
8 ms |
600 KB |
Output is correct |
3 |
Correct |
8 ms |
604 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
0 ms |
472 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
78 ms |
604 KB |
Output is correct |
11 |
Correct |
341 ms |
684 KB |
Output is correct |
12 |
Correct |
28 ms |
600 KB |
Output is correct |
13 |
Correct |
10 ms |
712 KB |
Output is correct |
14 |
Correct |
8 ms |
604 KB |
Output is correct |
15 |
Correct |
13 ms |
604 KB |
Output is correct |
16 |
Correct |
10 ms |
604 KB |
Output is correct |
17 |
Correct |
19 ms |
3444 KB |
Output is correct |
18 |
Correct |
26 ms |
3164 KB |
Output is correct |
19 |
Correct |
25 ms |
3596 KB |
Output is correct |
20 |
Correct |
11 ms |
600 KB |
Output is correct |
21 |
Correct |
10 ms |
604 KB |
Output is correct |
22 |
Correct |
8 ms |
604 KB |
Output is correct |
23 |
Correct |
9 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
80 ms |
3552 KB |
Output is correct |
26 |
Correct |
41 ms |
3416 KB |
Output is correct |
27 |
Correct |
42 ms |
3564 KB |
Output is correct |
28 |
Correct |
21 ms |
3416 KB |
Output is correct |
29 |
Correct |
21 ms |
3420 KB |
Output is correct |
30 |
Correct |
24 ms |
3676 KB |
Output is correct |
31 |
Correct |
8 ms |
604 KB |
Output is correct |
32 |
Correct |
8 ms |
600 KB |
Output is correct |
33 |
Correct |
16 ms |
2652 KB |
Output is correct |
34 |
Correct |
8 ms |
600 KB |
Output is correct |
35 |
Correct |
18 ms |
3264 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
20 ms |
3304 KB |
Output is correct |
39 |
Correct |
20 ms |
3164 KB |
Output is correct |
40 |
Correct |
21 ms |
3164 KB |
Output is correct |
41 |
Correct |
18 ms |
3164 KB |
Output is correct |
42 |
Correct |
22 ms |
3160 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
18 ms |
3336 KB |
Output is correct |
50 |
Correct |
18 ms |
3112 KB |
Output is correct |
51 |
Correct |
18 ms |
3128 KB |
Output is correct |
52 |
Correct |
18 ms |
3160 KB |
Output is correct |
53 |
Correct |
21 ms |
3164 KB |
Output is correct |
54 |
Correct |
24 ms |
3164 KB |
Output is correct |
55 |
Correct |
20 ms |
3164 KB |
Output is correct |
56 |
Correct |
8 ms |
708 KB |
Output is correct |
57 |
Correct |
8 ms |
604 KB |
Output is correct |
58 |
Correct |
24 ms |
2572 KB |
Output is correct |
59 |
Correct |
30 ms |
2572 KB |
Output is correct |
60 |
Correct |
43 ms |
2652 KB |
Output is correct |
61 |
Correct |
26 ms |
2812 KB |
Output is correct |
62 |
Correct |
197 ms |
2652 KB |
Output is correct |
63 |
Correct |
347 ms |
2648 KB |
Output is correct |
64 |
Correct |
202 ms |
2680 KB |
Output is correct |
65 |
Correct |
685 ms |
2648 KB |
Output is correct |
66 |
Correct |
765 ms |
2904 KB |
Output is correct |
67 |
Correct |
9 ms |
2648 KB |
Output is correct |
68 |
Correct |
36 ms |
3568 KB |
Output is correct |
69 |
Correct |
34 ms |
3420 KB |
Output is correct |
70 |
Correct |
19 ms |
3500 KB |
Output is correct |
71 |
Correct |
23 ms |
3420 KB |
Output is correct |
72 |
Correct |
22 ms |
3408 KB |
Output is correct |
73 |
Correct |
20 ms |
3420 KB |
Output is correct |
74 |
Correct |
24 ms |
3420 KB |
Output is correct |