#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
const int N = 205, M = 5e4 + 5;
const int inf = 1e9;
int n, m;
int pr[N], w[M], d[2][N], rd[2][N], f[2][N];
bool spec[M];
vector<array<int, 2>> g[N], rg[N];
array<int, 3> edges[M];
void dijkstra(int s, int *d, vector<array<int, 2>> *g) {
using T = array<int, 2>;
priority_queue<T, vector<T>, greater<T>> pq;
fill(d + 1, d + n + 1, inf);
fill(pr + 1, pr + n + 1, 0);
pq.push({d[s] = 0, s});
while (pq.size()) {
auto [c, u] = pq.top(); pq.pop();
if (c != d[u]) {
continue;
}
for (auto [v, id] : g[u]) {
if (d[v] > d[u] + w[id]) {
pr[v] = id;
pq.push({d[v] = d[u] + w[id], v});
}
}
}
}
void mark() {
for (int i = 1; i <= n; ++i) {
spec[pr[i]] = 1;
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
int u, v, c; cin >> u >> v >> w[i] >> c;
edges[i] = {u, v, c};
g[u].push_back({v, i});
rg[v].push_back({u, i});
}
dijkstra(1, d[0], g);
mark();
dijkstra(n, d[1], g);
mark();
dijkstra(1, rd[0], rg);
dijkstra(n, rd[1], rg);
auto del = [&](int u, array<int, 2> x) {
for (auto &v : g[u]) {
if (v == x) {
swap(v, g[u].back());
g[u].pop_back();
break;
}
}
};
auto add = [&](int u, array<int, 2> x) {
g[u].push_back(x);
};
int res = 2 * inf;
if (d[0][n] != inf && d[1][1] != inf) {
res = min(res, d[0][n] + d[1][1]);
}
for (int i = 1; i <= m; ++i) {
auto [u, v, c] = edges[i];
if (spec[i]) {
del(u, {v, i});
add(v, {u, i});
dijkstra(1, f[0], g);
dijkstra(n, f[1], g);
if (f[0][n] != inf && f[1][1] != inf) {
res = min(res, f[0][n] + f[1][1] + c);
}
del(v, {u, i});
add(u, {v, i});
} else {
int a = min(d[0][n], d[0][v] + rd[1][u] + w[i]);
int b = min(d[1][1], d[1][v] + rd[0][u] + w[i]);
if (a != inf && b != inf) {
res = min(res, a + b + c);
}
}
}
cout << (res != 2 * inf ? res : -1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
572 KB |
Output is correct |
4 |
Correct |
11 ms |
568 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 |
484 KB |
Output is correct |
8 |
Correct |
0 ms |
484 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
12 ms |
568 KB |
Output is correct |
11 |
Correct |
14 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
568 KB |
Output is correct |
14 |
Correct |
6 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
568 KB |
Output is correct |
16 |
Correct |
5 ms |
544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
3672 KB |
Output is correct |
2 |
Correct |
79 ms |
3676 KB |
Output is correct |
3 |
Correct |
69 ms |
3672 KB |
Output is correct |
4 |
Correct |
6 ms |
604 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
0 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 |
22 ms |
3608 KB |
Output is correct |
10 |
Correct |
22 ms |
3768 KB |
Output is correct |
11 |
Correct |
45 ms |
3760 KB |
Output is correct |
12 |
Correct |
45 ms |
3820 KB |
Output is correct |
13 |
Correct |
42 ms |
3676 KB |
Output is correct |
14 |
Correct |
37 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
43 ms |
2652 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
50 ms |
3548 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
21 ms |
3420 KB |
Output is correct |
9 |
Correct |
23 ms |
3420 KB |
Output is correct |
10 |
Correct |
36 ms |
3420 KB |
Output is correct |
11 |
Correct |
49 ms |
3420 KB |
Output is correct |
12 |
Correct |
37 ms |
3420 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 |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
44 ms |
3396 KB |
Output is correct |
20 |
Correct |
38 ms |
3420 KB |
Output is correct |
21 |
Correct |
36 ms |
3416 KB |
Output is correct |
22 |
Correct |
36 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
9 ms |
572 KB |
Output is correct |
4 |
Correct |
11 ms |
568 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 |
484 KB |
Output is correct |
8 |
Correct |
0 ms |
484 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
12 ms |
568 KB |
Output is correct |
11 |
Correct |
14 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
348 KB |
Output is correct |
13 |
Correct |
3 ms |
568 KB |
Output is correct |
14 |
Correct |
6 ms |
348 KB |
Output is correct |
15 |
Correct |
5 ms |
568 KB |
Output is correct |
16 |
Correct |
5 ms |
544 KB |
Output is correct |
17 |
Correct |
73 ms |
3672 KB |
Output is correct |
18 |
Correct |
79 ms |
3676 KB |
Output is correct |
19 |
Correct |
69 ms |
3672 KB |
Output is correct |
20 |
Correct |
6 ms |
604 KB |
Output is correct |
21 |
Correct |
3 ms |
348 KB |
Output is correct |
22 |
Correct |
0 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 |
22 ms |
3608 KB |
Output is correct |
26 |
Correct |
22 ms |
3768 KB |
Output is correct |
27 |
Correct |
45 ms |
3760 KB |
Output is correct |
28 |
Correct |
45 ms |
3820 KB |
Output is correct |
29 |
Correct |
42 ms |
3676 KB |
Output is correct |
30 |
Correct |
37 ms |
3932 KB |
Output is correct |
31 |
Correct |
6 ms |
348 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
43 ms |
2652 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
50 ms |
3548 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
21 ms |
3420 KB |
Output is correct |
39 |
Correct |
23 ms |
3420 KB |
Output is correct |
40 |
Correct |
36 ms |
3420 KB |
Output is correct |
41 |
Correct |
49 ms |
3420 KB |
Output is correct |
42 |
Correct |
37 ms |
3420 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 |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
44 ms |
3396 KB |
Output is correct |
50 |
Correct |
38 ms |
3420 KB |
Output is correct |
51 |
Correct |
36 ms |
3416 KB |
Output is correct |
52 |
Correct |
36 ms |
3420 KB |
Output is correct |
53 |
Correct |
93 ms |
3604 KB |
Output is correct |
54 |
Correct |
92 ms |
3580 KB |
Output is correct |
55 |
Correct |
82 ms |
3420 KB |
Output is correct |
56 |
Correct |
11 ms |
344 KB |
Output is correct |
57 |
Correct |
9 ms |
348 KB |
Output is correct |
58 |
Correct |
59 ms |
2900 KB |
Output is correct |
59 |
Correct |
63 ms |
2876 KB |
Output is correct |
60 |
Correct |
85 ms |
2692 KB |
Output is correct |
61 |
Correct |
54 ms |
2652 KB |
Output is correct |
62 |
Correct |
64 ms |
2864 KB |
Output is correct |
63 |
Correct |
85 ms |
2908 KB |
Output is correct |
64 |
Correct |
450 ms |
3224 KB |
Output is correct |
65 |
Correct |
494 ms |
3296 KB |
Output is correct |
66 |
Correct |
625 ms |
3248 KB |
Output is correct |
67 |
Correct |
9 ms |
2628 KB |
Output is correct |
68 |
Correct |
28 ms |
3780 KB |
Output is correct |
69 |
Correct |
25 ms |
3676 KB |
Output is correct |
70 |
Correct |
56 ms |
3668 KB |
Output is correct |
71 |
Correct |
57 ms |
3676 KB |
Output is correct |
72 |
Correct |
55 ms |
3676 KB |
Output is correct |
73 |
Correct |
58 ms |
3828 KB |
Output is correct |
74 |
Correct |
52 ms |
3676 KB |
Output is correct |