#include <bits/stdc++.h>
using namespace std;
const int INF = 2e9;
int n, m, ans = INF;
vector<vector<pair<int, int>>> normalgraph, revgraph, brutegraph;
vector<int> a, b, c, d, from1, to1, fromn, ton;
vector<bool> isinsp;
void dijkstra(int x, vector<int> &dist, vector<vector<pair<int, int>>> &graph, bool setspedges){
vector<bool> vis(n + 1, false);
vector<int> par(n + 1, -1);
dist.assign(n + 1, INF);
dist[x] = 0;
while(true){
int v = -1;
for(int i = 1; i <= n; i++){
if(!vis[i] && dist[i] != INF && (v == -1 || dist[i] < dist[v])){
v = i;
}
}
if(v == -1) break;
if(setspedges && par[v] != -1) isinsp[par[v]] = true;
vis[v] = true;
for(auto [u, i] : graph[v]){
if(dist[v] + c[i] < dist[u]){
dist[u] = dist[v] + c[i];
par[u] = i;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);cin.tie(NULL);
cin >> n >> m;
normalgraph = revgraph = vector(n + 1, vector<pair<int, int>>{});
a = b = c = d = vector(m + 1, 0);
isinsp = vector(m + 1, false);
for(int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
normalgraph[a[i]].push_back({b[i], i});
revgraph[b[i]].push_back({a[i], i});
}
dijkstra(1, from1, normalgraph, true);
dijkstra(1, to1, revgraph, true);
dijkstra(n, fromn, normalgraph, true);
dijkstra(n, ton, revgraph, true);
if(from1[n] != INF && fromn[1] != INF) ans = from1[n] + fromn[1];
for(int i = 1; i <= m; i++){
int useton = INF, useto1 = INF;
if(isinsp[i]){
brutegraph = vector(n + 1, vector<pair<int, int>>{});
for(int j = 1; j <= m; j++){
if(j == i) brutegraph[b[j]].push_back({a[j], j});
else brutegraph[a[j]].push_back({b[j], j});
}
vector<int> brutedist;
dijkstra(1, brutedist, brutegraph, false);
useton = brutedist[n];
dijkstra(n, brutedist, brutegraph, false);
useto1 = brutedist[1];
}
else{
useton = from1[n], useto1 = fromn[1];
if(from1[b[i]] != INF && ton[a[i]] != INF) useton = min(useton, from1[b[i]] + c[i] + ton[a[i]]);
if(fromn[b[i]] != INF && to1[a[i]] != INF) useto1 = min(useto1, fromn[b[i]] + c[i] + to1[a[i]]);
}
if(useton != INF && useto1 != INF) ans = min(ans, d[i] + useton + useto1);
}
cout << (ans == INF ? -1 : ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
520 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
90 ms |
520 KB |
Output is correct |
4 |
Correct |
94 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
404 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
46 ms |
344 KB |
Output is correct |
11 |
Correct |
60 ms |
344 KB |
Output is correct |
12 |
Correct |
55 ms |
516 KB |
Output is correct |
13 |
Correct |
28 ms |
348 KB |
Output is correct |
14 |
Correct |
57 ms |
344 KB |
Output is correct |
15 |
Correct |
57 ms |
344 KB |
Output is correct |
16 |
Correct |
59 ms |
524 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
396 ms |
3168 KB |
Output is correct |
2 |
Correct |
420 ms |
3324 KB |
Output is correct |
3 |
Correct |
399 ms |
3208 KB |
Output is correct |
4 |
Correct |
97 ms |
576 KB |
Output is correct |
5 |
Correct |
58 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
204 ms |
3184 KB |
Output is correct |
10 |
Correct |
195 ms |
3176 KB |
Output is correct |
11 |
Correct |
324 ms |
3160 KB |
Output is correct |
12 |
Correct |
306 ms |
3164 KB |
Output is correct |
13 |
Correct |
302 ms |
3148 KB |
Output is correct |
14 |
Correct |
300 ms |
3424 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
516 KB |
Output is correct |
2 |
Correct |
6 ms |
348 KB |
Output is correct |
3 |
Correct |
501 ms |
2452 KB |
Output is correct |
4 |
Correct |
3 ms |
348 KB |
Output is correct |
5 |
Correct |
622 ms |
3252 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
263 ms |
3200 KB |
Output is correct |
9 |
Correct |
275 ms |
4100 KB |
Output is correct |
10 |
Correct |
425 ms |
4044 KB |
Output is correct |
11 |
Correct |
418 ms |
4108 KB |
Output is correct |
12 |
Correct |
450 ms |
4572 KB |
Output is correct |
13 |
Correct |
1 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 |
344 KB |
Output is correct |
17 |
Correct |
0 ms |
344 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
436 ms |
4204 KB |
Output is correct |
20 |
Correct |
439 ms |
4036 KB |
Output is correct |
21 |
Correct |
443 ms |
4244 KB |
Output is correct |
22 |
Correct |
413 ms |
4004 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
520 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
90 ms |
520 KB |
Output is correct |
4 |
Correct |
94 ms |
348 KB |
Output is correct |
5 |
Correct |
2 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
404 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
46 ms |
344 KB |
Output is correct |
11 |
Correct |
60 ms |
344 KB |
Output is correct |
12 |
Correct |
55 ms |
516 KB |
Output is correct |
13 |
Correct |
28 ms |
348 KB |
Output is correct |
14 |
Correct |
57 ms |
344 KB |
Output is correct |
15 |
Correct |
57 ms |
344 KB |
Output is correct |
16 |
Correct |
59 ms |
524 KB |
Output is correct |
17 |
Correct |
396 ms |
3168 KB |
Output is correct |
18 |
Correct |
420 ms |
3324 KB |
Output is correct |
19 |
Correct |
399 ms |
3208 KB |
Output is correct |
20 |
Correct |
97 ms |
576 KB |
Output is correct |
21 |
Correct |
58 ms |
348 KB |
Output is correct |
22 |
Correct |
3 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
344 KB |
Output is correct |
25 |
Correct |
204 ms |
3184 KB |
Output is correct |
26 |
Correct |
195 ms |
3176 KB |
Output is correct |
27 |
Correct |
324 ms |
3160 KB |
Output is correct |
28 |
Correct |
306 ms |
3164 KB |
Output is correct |
29 |
Correct |
302 ms |
3148 KB |
Output is correct |
30 |
Correct |
300 ms |
3424 KB |
Output is correct |
31 |
Correct |
72 ms |
516 KB |
Output is correct |
32 |
Correct |
6 ms |
348 KB |
Output is correct |
33 |
Correct |
501 ms |
2452 KB |
Output is correct |
34 |
Correct |
3 ms |
348 KB |
Output is correct |
35 |
Correct |
622 ms |
3252 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
263 ms |
3200 KB |
Output is correct |
39 |
Correct |
275 ms |
4100 KB |
Output is correct |
40 |
Correct |
425 ms |
4044 KB |
Output is correct |
41 |
Correct |
418 ms |
4108 KB |
Output is correct |
42 |
Correct |
450 ms |
4572 KB |
Output is correct |
43 |
Correct |
1 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 |
344 KB |
Output is correct |
47 |
Correct |
0 ms |
344 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
436 ms |
4204 KB |
Output is correct |
50 |
Correct |
439 ms |
4036 KB |
Output is correct |
51 |
Correct |
443 ms |
4244 KB |
Output is correct |
52 |
Correct |
413 ms |
4004 KB |
Output is correct |
53 |
Correct |
400 ms |
4052 KB |
Output is correct |
54 |
Correct |
414 ms |
4088 KB |
Output is correct |
55 |
Correct |
394 ms |
4076 KB |
Output is correct |
56 |
Correct |
97 ms |
348 KB |
Output is correct |
57 |
Correct |
87 ms |
348 KB |
Output is correct |
58 |
Correct |
152 ms |
3172 KB |
Output is correct |
59 |
Correct |
185 ms |
3272 KB |
Output is correct |
60 |
Correct |
242 ms |
3244 KB |
Output is correct |
61 |
Correct |
158 ms |
3408 KB |
Output is correct |
62 |
Correct |
174 ms |
3164 KB |
Output is correct |
63 |
Correct |
243 ms |
3160 KB |
Output is correct |
64 |
Correct |
195 ms |
3640 KB |
Output is correct |
65 |
Correct |
213 ms |
3460 KB |
Output is correct |
66 |
Correct |
276 ms |
3660 KB |
Output is correct |
67 |
Correct |
11 ms |
3280 KB |
Output is correct |
68 |
Correct |
211 ms |
4344 KB |
Output is correct |
69 |
Correct |
207 ms |
4352 KB |
Output is correct |
70 |
Correct |
322 ms |
4264 KB |
Output is correct |
71 |
Correct |
313 ms |
4272 KB |
Output is correct |
72 |
Correct |
324 ms |
4452 KB |
Output is correct |
73 |
Correct |
321 ms |
4380 KB |
Output is correct |
74 |
Correct |
323 ms |
4568 KB |
Output is correct |