#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[2][205];
int dis[4][205];
int a[N],b[N],c[N],d[N];
priority_queue <pair <int,int> > pq;
void djkstra(int n,int tp,int x){
dis[x][n]=0;
pq.push({0,n});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
for(auto to : g[tp][v]){
if(dis[x][to.ff.ff]>dis[x][v]+to.ff.ss){
dis[x][to.ff.ff]=dis[x][v]+to.ff.ss;
pq.push({dis[x][to.ff.ff],to.ff.ff});
}
}
}
}
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<=m;i++){
cin>>a[i]>>b[i]>>c[i]>>d[i];
g[0][a[i]].pb({{b[i],c[i]},i});
g[1][b[i]].pb({{a[i],c[i]},i});
}
for(int i=1;i<=n;i++){
for(int j=0;j<4;j++){
dis[j][i]=1e9;
}
}
djkstra(1,0,0);
djkstra(1,1,1);
djkstra(n,0,2);
djkstra(n,1,3);
/*for(int x=0;x<4;x++){
for(int i=1;i<=n;i++){
cout<<dis[x][i]<<" ";
}
cout<<"\n";
}
cout<<"\n";*/
int mn=2e9;
if(dis[0][n]<1e9 && dis[2][1]<1e9)mn=min(dis[0][n]+dis[2][1],mn);
for(int i=1;i<=m;i++){
int new1n=min(dis[0][n],dis[0][b[i]]+dis[3][a[i]]+c[i]);
int newn1=min(dis[2][1],dis[2][b[i]]+dis[1][a[i]]+c[i]);
if(new1n==1e9 or newn1==1e9 or new1n+newn1+d[i]>=mn)continue;
else{
vector <int> dist(n+1,1e9);
dist[1]=0;
pq.push({0,1});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(v==b[i]){
if(dist[a[i]]>dist[b[i]]+c[i]){
dist[a[i]]=dist[b[i]]+c[i];
pq.push({dist[a[i]],a[i]});
}
}
for(auto to : g[0][v]){
if(to.ss==i)continue;
if(dist[to.ff.ff]>dist[v]+to.ff.ss){
dist[to.ff.ff]=dist[v]+to.ff.ss;
pq.push({dist[to.ff.ff],to.ff.ff});
}
}
}
if(dist[n]<1e9){
int ans=dist[n];
for(int j=1;j<=n;j++){
dist[j]=1e9;
}
dist[n]=0;
pq.push({0,n});
while(!pq.empty()){
int v=pq.top().ss;
pq.pop();
if(v==b[i]){
if(dist[a[i]]>dist[b[i]]+c[i]){
dist[a[i]]=dist[b[i]]+c[i];
pq.push({dist[a[i]],a[i]});
}
}
for(auto to : g[0][v]){
if(to.ss==i)continue;
if(dist[to.ff.ff]>dist[v]+to.ff.ss){
dist[to.ff.ff]=dist[v]+to.ff.ss;
pq.push({dist[to.ff.ff],to.ff.ff});
}
}
}
if(dist[1]<1e9)mn=min(mn,ans+dist[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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
548 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
73 ms |
348 KB |
Output is correct |
11 |
Correct |
301 ms |
348 KB |
Output is correct |
12 |
Correct |
14 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
556 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
3244 KB |
Output is correct |
2 |
Correct |
21 ms |
3160 KB |
Output is correct |
3 |
Correct |
17 ms |
3160 KB |
Output is correct |
4 |
Correct |
6 ms |
604 KB |
Output is correct |
5 |
Correct |
4 ms |
548 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
55 ms |
3176 KB |
Output is correct |
10 |
Correct |
31 ms |
3164 KB |
Output is correct |
11 |
Correct |
20 ms |
3272 KB |
Output is correct |
12 |
Correct |
16 ms |
3160 KB |
Output is correct |
13 |
Correct |
17 ms |
3164 KB |
Output is correct |
14 |
Correct |
25 ms |
3460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
2484 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
11 ms |
3164 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
13 ms |
3164 KB |
Output is correct |
9 |
Correct |
13 ms |
3164 KB |
Output is correct |
10 |
Correct |
11 ms |
3164 KB |
Output is correct |
11 |
Correct |
11 ms |
3200 KB |
Output is correct |
12 |
Correct |
11 ms |
3164 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 |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
11 ms |
3176 KB |
Output is correct |
20 |
Correct |
15 ms |
3372 KB |
Output is correct |
21 |
Correct |
11 ms |
3164 KB |
Output is correct |
22 |
Correct |
11 ms |
2956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
548 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
360 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
73 ms |
348 KB |
Output is correct |
11 |
Correct |
301 ms |
348 KB |
Output is correct |
12 |
Correct |
14 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
556 KB |
Output is correct |
14 |
Correct |
2 ms |
348 KB |
Output is correct |
15 |
Correct |
3 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
18 ms |
3244 KB |
Output is correct |
18 |
Correct |
21 ms |
3160 KB |
Output is correct |
19 |
Correct |
17 ms |
3160 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
4 ms |
548 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
55 ms |
3176 KB |
Output is correct |
26 |
Correct |
31 ms |
3164 KB |
Output is correct |
27 |
Correct |
20 ms |
3272 KB |
Output is correct |
28 |
Correct |
16 ms |
3160 KB |
Output is correct |
29 |
Correct |
17 ms |
3164 KB |
Output is correct |
30 |
Correct |
25 ms |
3460 KB |
Output is correct |
31 |
Correct |
1 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
9 ms |
2484 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
11 ms |
3164 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
13 ms |
3164 KB |
Output is correct |
39 |
Correct |
13 ms |
3164 KB |
Output is correct |
40 |
Correct |
11 ms |
3164 KB |
Output is correct |
41 |
Correct |
11 ms |
3200 KB |
Output is correct |
42 |
Correct |
11 ms |
3164 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 |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
11 ms |
3176 KB |
Output is correct |
50 |
Correct |
15 ms |
3372 KB |
Output is correct |
51 |
Correct |
11 ms |
3164 KB |
Output is correct |
52 |
Correct |
11 ms |
2956 KB |
Output is correct |
53 |
Correct |
16 ms |
2920 KB |
Output is correct |
54 |
Correct |
15 ms |
3028 KB |
Output is correct |
55 |
Correct |
14 ms |
2936 KB |
Output is correct |
56 |
Correct |
3 ms |
344 KB |
Output is correct |
57 |
Correct |
3 ms |
348 KB |
Output is correct |
58 |
Correct |
19 ms |
2512 KB |
Output is correct |
59 |
Correct |
26 ms |
2396 KB |
Output is correct |
60 |
Correct |
35 ms |
2276 KB |
Output is correct |
61 |
Correct |
21 ms |
2396 KB |
Output is correct |
62 |
Correct |
191 ms |
2484 KB |
Output is correct |
63 |
Correct |
359 ms |
2396 KB |
Output is correct |
64 |
Correct |
204 ms |
2648 KB |
Output is correct |
65 |
Correct |
738 ms |
2652 KB |
Output is correct |
66 |
Correct |
776 ms |
2716 KB |
Output is correct |
67 |
Correct |
13 ms |
2256 KB |
Output is correct |
68 |
Correct |
38 ms |
3060 KB |
Output is correct |
69 |
Correct |
30 ms |
3208 KB |
Output is correct |
70 |
Correct |
16 ms |
3164 KB |
Output is correct |
71 |
Correct |
18 ms |
3164 KB |
Output is correct |
72 |
Correct |
15 ms |
3208 KB |
Output is correct |
73 |
Correct |
15 ms |
3164 KB |
Output is correct |
74 |
Correct |
19 ms |
3236 KB |
Output is correct |