#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 200, M = 50000;
const ll infL = 3e18;
bool in_s[M], in_n[M], used[N];
ll distS[N], distN[N], distAllS[N], distAllN[N], tmp[N];
int A[M], B[M], W[M], D[M], n, m;
vector<pair<int, int>> newg[N];
void dijkstra(int s, vector<pair<int, int>> g[], ll dist[], bool need, bool in[], int blocked = -1) {
fill(dist, dist + n, infL);
dist[s] = 0;
int S = s;
vector<bool> was(n);
while (s != -1) {
was[s] = true;
for (auto [to, i]: g[s]) {
if (i != blocked && dist[to] > dist[s] + W[i]) {
dist[to] = dist[s] + W[i];
}
}
int nxt = -1;
for (int i = 0; i < n; ++i) {
if (!was[i] && dist[i] != infL && (nxt == -1 || dist[nxt] > dist[i])) {
nxt = i;
}
}
s = nxt;
}
if (need) {
for (int i = 0; i < n; ++i) {
newg[i].clear();
}
for (int i = 0; i < n; ++i) {
for (auto [to, j]: g[i]) {
if (dist[to] == dist[i] + W[j]) {
newg[i].emplace_back(to, j);
}
}
}
memset(used, 0, sizeof(used[0]) * n);
function<void(int)> dfs = [&](int v) {
used[v] = true;
for (auto [to, i]: newg[v]) {
if (!used[to]) {
in[i] = true;
dfs(to);
}
}
};
dfs(S);
}
}
vector<pair<int, int>> g[N], revg[N];
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n >> m;
for (int i = 0; i < m; ++i) {
cin >> A[i] >> B[i] >> W[i] >> D[i];
--A[i], --B[i];
g[A[i]].emplace_back(B[i], i);
revg[B[i]].emplace_back(A[i], i);
}
dijkstra(n - 1, g, distN, true, in_n);
dijkstra(0, g, distS, true, in_s);
dijkstra(0, revg, distAllS, false, used);
dijkstra(n - 1, revg, distAllN, false, used);
ll ans = infL;
ans = min(ans, distS[n - 1] + distN[0]);
for (int i = 0; i < m; ++i) {
ll valSN, valNS;
if (in_s[i]) {
dijkstra(0, g, tmp, false, nullptr, i);
valSN = tmp[n - 1];
} else {
valSN = min(distS[n - 1], distS[B[i]] + W[i] + distAllN[A[i]]);
}
if (in_n[i]) {
dijkstra(n - 1, g, tmp, false, nullptr, i);
valNS = tmp[0];
} else {
valNS = min(distN[0], distN[B[i]] + W[i] + distAllS[A[i]]);
}
ans = min(ans, D[i] + valNS + valSN);
}
cout << (ans == infL ? -1 : ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
43 ms |
440 KB |
Output is correct |
4 |
Correct |
42 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
41 ms |
340 KB |
Output is correct |
11 |
Correct |
40 ms |
340 KB |
Output is correct |
12 |
Correct |
41 ms |
340 KB |
Output is correct |
13 |
Correct |
12 ms |
376 KB |
Output is correct |
14 |
Correct |
25 ms |
348 KB |
Output is correct |
15 |
Correct |
24 ms |
444 KB |
Output is correct |
16 |
Correct |
27 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
2556 KB |
Output is correct |
2 |
Correct |
98 ms |
2944 KB |
Output is correct |
3 |
Correct |
84 ms |
2884 KB |
Output is correct |
4 |
Correct |
40 ms |
468 KB |
Output is correct |
5 |
Correct |
32 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
43 ms |
2880 KB |
Output is correct |
10 |
Correct |
40 ms |
2892 KB |
Output is correct |
11 |
Correct |
60 ms |
2828 KB |
Output is correct |
12 |
Correct |
65 ms |
2912 KB |
Output is correct |
13 |
Correct |
59 ms |
3020 KB |
Output is correct |
14 |
Correct |
55 ms |
3064 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
412 KB |
Output is correct |
2 |
Correct |
4 ms |
392 KB |
Output is correct |
3 |
Correct |
68 ms |
2776 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
87 ms |
3148 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
33 ms |
3212 KB |
Output is correct |
9 |
Correct |
34 ms |
3208 KB |
Output is correct |
10 |
Correct |
55 ms |
3404 KB |
Output is correct |
11 |
Correct |
60 ms |
3268 KB |
Output is correct |
12 |
Correct |
61 ms |
3292 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
58 ms |
3312 KB |
Output is correct |
20 |
Correct |
54 ms |
3268 KB |
Output is correct |
21 |
Correct |
62 ms |
3256 KB |
Output is correct |
22 |
Correct |
58 ms |
3276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
43 ms |
440 KB |
Output is correct |
4 |
Correct |
42 ms |
420 KB |
Output is correct |
5 |
Correct |
2 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
41 ms |
340 KB |
Output is correct |
11 |
Correct |
40 ms |
340 KB |
Output is correct |
12 |
Correct |
41 ms |
340 KB |
Output is correct |
13 |
Correct |
12 ms |
376 KB |
Output is correct |
14 |
Correct |
25 ms |
348 KB |
Output is correct |
15 |
Correct |
24 ms |
444 KB |
Output is correct |
16 |
Correct |
27 ms |
340 KB |
Output is correct |
17 |
Correct |
85 ms |
2556 KB |
Output is correct |
18 |
Correct |
98 ms |
2944 KB |
Output is correct |
19 |
Correct |
84 ms |
2884 KB |
Output is correct |
20 |
Correct |
40 ms |
468 KB |
Output is correct |
21 |
Correct |
32 ms |
364 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
43 ms |
2880 KB |
Output is correct |
26 |
Correct |
40 ms |
2892 KB |
Output is correct |
27 |
Correct |
60 ms |
2828 KB |
Output is correct |
28 |
Correct |
65 ms |
2912 KB |
Output is correct |
29 |
Correct |
59 ms |
3020 KB |
Output is correct |
30 |
Correct |
55 ms |
3064 KB |
Output is correct |
31 |
Correct |
36 ms |
412 KB |
Output is correct |
32 |
Correct |
4 ms |
392 KB |
Output is correct |
33 |
Correct |
68 ms |
2776 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
87 ms |
3148 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
33 ms |
3212 KB |
Output is correct |
39 |
Correct |
34 ms |
3208 KB |
Output is correct |
40 |
Correct |
55 ms |
3404 KB |
Output is correct |
41 |
Correct |
60 ms |
3268 KB |
Output is correct |
42 |
Correct |
61 ms |
3292 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
0 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
58 ms |
3312 KB |
Output is correct |
50 |
Correct |
54 ms |
3268 KB |
Output is correct |
51 |
Correct |
62 ms |
3256 KB |
Output is correct |
52 |
Correct |
58 ms |
3276 KB |
Output is correct |
53 |
Correct |
86 ms |
2880 KB |
Output is correct |
54 |
Correct |
86 ms |
2904 KB |
Output is correct |
55 |
Correct |
95 ms |
2892 KB |
Output is correct |
56 |
Correct |
47 ms |
428 KB |
Output is correct |
57 |
Correct |
54 ms |
424 KB |
Output is correct |
58 |
Correct |
77 ms |
2252 KB |
Output is correct |
59 |
Correct |
77 ms |
2368 KB |
Output is correct |
60 |
Correct |
78 ms |
2376 KB |
Output is correct |
61 |
Correct |
103 ms |
2252 KB |
Output is correct |
62 |
Correct |
80 ms |
2448 KB |
Output is correct |
63 |
Correct |
78 ms |
2376 KB |
Output is correct |
64 |
Correct |
118 ms |
2520 KB |
Output is correct |
65 |
Correct |
108 ms |
2556 KB |
Output is correct |
66 |
Correct |
108 ms |
2524 KB |
Output is correct |
67 |
Correct |
14 ms |
2256 KB |
Output is correct |
68 |
Correct |
43 ms |
2796 KB |
Output is correct |
69 |
Correct |
31 ms |
2852 KB |
Output is correct |
70 |
Correct |
57 ms |
2832 KB |
Output is correct |
71 |
Correct |
57 ms |
2820 KB |
Output is correct |
72 |
Correct |
58 ms |
2908 KB |
Output is correct |
73 |
Correct |
66 ms |
2952 KB |
Output is correct |
74 |
Correct |
64 ms |
2968 KB |
Output is correct |