#include<bits/stdc++.h>
using namespace std;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
const int MAXN=207, MAXM=5e4+7, INF=2e9+7;
vector<int>V[MAXN];
pair<pair<int,int>,pair<int,int>>T[MAXM];
int odl[MAXN][4], odw[MAXM], mi[MAXN], n, m;
void dijkstra1(int v, int r, int p) {
rep(i, n) odl[i][p]=INF;
priority_queue<pair<int,pair<int,int>>>q;
q.push({0, {v, -1}});
while(!q.empty()) {
int o=-q.top().st, a=q.top().nd.st, b=q.top().nd.nd; q.pop();
if(odl[a][p]<=o) continue;
odl[a][p]=o;
if(!r && b!=-1) odw[b]=1;
for(auto i : V[a]) {
if(!r) {
if(T[i].st.st==a && odl[T[i].st.nd][p]==INF) {
q.push({-o-T[i].nd.st, {T[i].st.nd, i}});
}
} else {
if(T[i].st.nd==a && odl[T[i].st.st][p]==INF) {
q.push({-o-T[i].nd.st, {T[i].st.st, i}});
}
}
}
}
}
void dijkstra2(int v) {
rep(i, n) odl[i][0]=mi[i]=INF;
mi[v]=0;
while(true) {
int o=INF, a=-1;
rep(i, n) if(mi[i]<o && odl[i][0]==INF) {
o=mi[i];
a=i;
}
if(a==-1) break;
odl[a][0]=o;
for(auto i : V[a]) if(T[i].st.st==a) mi[T[i].st.nd]=min(mi[T[i].st.nd], o+T[i].nd.st);
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
rep(i, m) {
cin >> T[i].st.st >> T[i].st.nd >> T[i].nd.st >> T[i].nd.nd;
--T[i].st.st;
--T[i].st.nd;
V[T[i].st.st].pb(i);
V[T[i].st.nd].pb(i);
}
dijkstra1(0, 0, 0);
dijkstra1(0, 1, 1);
dijkstra1(n-1, 0, 2);
dijkstra1(n-1, 1, 3);
int ans=INF;
if(odl[n-1][0]<INF && odl[0][2]<INF) ans=odl[n-1][0]+odl[0][2];
rep(i, m) if(!odw[i]) {
int a=odl[n-1][0], b=odl[0][2];
if(odl[T[i].st.nd][0]<INF && odl[T[i].st.st][3]<INF) {
a=min(a, odl[T[i].st.nd][0]+odl[T[i].st.st][3]+T[i].nd.st);
}
if(odl[T[i].st.nd][2]<INF && odl[T[i].st.st][1]<INF) {
b=min(b, odl[T[i].st.nd][2]+odl[T[i].st.st][1]+T[i].nd.st);
}
if(a<INF && b<INF) ans=min(ans, a+b+T[i].nd.nd);
}
rep(i, m) if(odw[i]) {
swap(T[i].st.st, T[i].st.nd);
dijkstra2(0);
if(odl[n-1][0]<INF) {
int akt=odl[n-1][0];
dijkstra2(n-1);
if(odl[0][0]<INF) {
akt+=odl[0][0];
ans=min(ans, akt+T[i].nd.nd);
}
}
swap(T[i].st.st, T[i].st.nd);
}
cout << (ans==INF?-1:ans) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
50 ms |
388 KB |
Output is correct |
4 |
Correct |
55 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
41 ms |
332 KB |
Output is correct |
11 |
Correct |
52 ms |
344 KB |
Output is correct |
12 |
Correct |
47 ms |
336 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
33 ms |
332 KB |
Output is correct |
15 |
Correct |
10 ms |
332 KB |
Output is correct |
16 |
Correct |
29 ms |
388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
533 ms |
2928 KB |
Output is correct |
2 |
Correct |
573 ms |
3588 KB |
Output is correct |
3 |
Correct |
535 ms |
3760 KB |
Output is correct |
4 |
Correct |
56 ms |
460 KB |
Output is correct |
5 |
Correct |
35 ms |
388 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
308 KB |
Output is correct |
9 |
Correct |
66 ms |
3376 KB |
Output is correct |
10 |
Correct |
62 ms |
3436 KB |
Output is correct |
11 |
Correct |
152 ms |
3716 KB |
Output is correct |
12 |
Correct |
348 ms |
3776 KB |
Output is correct |
13 |
Correct |
112 ms |
3772 KB |
Output is correct |
14 |
Correct |
87 ms |
3800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
408 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
403 ms |
1944 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
509 ms |
2420 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
328 KB |
Output is correct |
8 |
Correct |
66 ms |
2880 KB |
Output is correct |
9 |
Correct |
79 ms |
2896 KB |
Output is correct |
10 |
Correct |
397 ms |
3076 KB |
Output is correct |
11 |
Correct |
136 ms |
3040 KB |
Output is correct |
12 |
Correct |
168 ms |
3040 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
407 ms |
3088 KB |
Output is correct |
20 |
Correct |
135 ms |
3108 KB |
Output is correct |
21 |
Correct |
169 ms |
3004 KB |
Output is correct |
22 |
Correct |
160 ms |
3056 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
332 KB |
Output is correct |
3 |
Correct |
50 ms |
388 KB |
Output is correct |
4 |
Correct |
55 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
0 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
41 ms |
332 KB |
Output is correct |
11 |
Correct |
52 ms |
344 KB |
Output is correct |
12 |
Correct |
47 ms |
336 KB |
Output is correct |
13 |
Correct |
7 ms |
332 KB |
Output is correct |
14 |
Correct |
33 ms |
332 KB |
Output is correct |
15 |
Correct |
10 ms |
332 KB |
Output is correct |
16 |
Correct |
29 ms |
388 KB |
Output is correct |
17 |
Correct |
533 ms |
2928 KB |
Output is correct |
18 |
Correct |
573 ms |
3588 KB |
Output is correct |
19 |
Correct |
535 ms |
3760 KB |
Output is correct |
20 |
Correct |
56 ms |
460 KB |
Output is correct |
21 |
Correct |
35 ms |
388 KB |
Output is correct |
22 |
Correct |
1 ms |
332 KB |
Output is correct |
23 |
Correct |
1 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
308 KB |
Output is correct |
25 |
Correct |
66 ms |
3376 KB |
Output is correct |
26 |
Correct |
62 ms |
3436 KB |
Output is correct |
27 |
Correct |
152 ms |
3716 KB |
Output is correct |
28 |
Correct |
348 ms |
3776 KB |
Output is correct |
29 |
Correct |
112 ms |
3772 KB |
Output is correct |
30 |
Correct |
87 ms |
3800 KB |
Output is correct |
31 |
Correct |
24 ms |
408 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
403 ms |
1944 KB |
Output is correct |
34 |
Correct |
1 ms |
332 KB |
Output is correct |
35 |
Correct |
509 ms |
2420 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
328 KB |
Output is correct |
38 |
Correct |
66 ms |
2880 KB |
Output is correct |
39 |
Correct |
79 ms |
2896 KB |
Output is correct |
40 |
Correct |
397 ms |
3076 KB |
Output is correct |
41 |
Correct |
136 ms |
3040 KB |
Output is correct |
42 |
Correct |
168 ms |
3040 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
407 ms |
3088 KB |
Output is correct |
50 |
Correct |
135 ms |
3108 KB |
Output is correct |
51 |
Correct |
169 ms |
3004 KB |
Output is correct |
52 |
Correct |
160 ms |
3056 KB |
Output is correct |
53 |
Correct |
705 ms |
3596 KB |
Output is correct |
54 |
Correct |
799 ms |
3604 KB |
Output is correct |
55 |
Correct |
760 ms |
3568 KB |
Output is correct |
56 |
Correct |
53 ms |
332 KB |
Output is correct |
57 |
Correct |
47 ms |
344 KB |
Output is correct |
58 |
Correct |
413 ms |
2788 KB |
Output is correct |
59 |
Correct |
474 ms |
2788 KB |
Output is correct |
60 |
Correct |
635 ms |
2784 KB |
Output is correct |
61 |
Correct |
420 ms |
2776 KB |
Output is correct |
62 |
Correct |
477 ms |
2780 KB |
Output is correct |
63 |
Correct |
639 ms |
2716 KB |
Output is correct |
64 |
Correct |
360 ms |
3088 KB |
Output is correct |
65 |
Correct |
403 ms |
2968 KB |
Output is correct |
66 |
Correct |
488 ms |
2984 KB |
Output is correct |
67 |
Correct |
27 ms |
3228 KB |
Output is correct |
68 |
Correct |
88 ms |
3400 KB |
Output is correct |
69 |
Correct |
78 ms |
3372 KB |
Output is correct |
70 |
Correct |
528 ms |
3632 KB |
Output is correct |
71 |
Correct |
488 ms |
3604 KB |
Output is correct |
72 |
Correct |
173 ms |
3720 KB |
Output is correct |
73 |
Correct |
207 ms |
3816 KB |
Output is correct |
74 |
Correct |
484 ms |
3772 KB |
Output is correct |