#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];
ll distS[N], distN[N], distAllS[N], distAllN[N], tmp[N];
int A[M], B[M], W[M], D[M], n, m;
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);
vector<int> par(n, -1);
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];
par[to] = 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) {
if (par[i] != -1) {
in[par[i]] = true;
}
}
}
}
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, nullptr);
dijkstra(n - 1, revg, distAllN, false, nullptr);
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;
}
Compilation message
ho_t4.cpp: In function 'void dijkstra(int, std::vector<std::pair<int, int> >*, ll*, bool, bool*, int)':
ho_t4.cpp:18:9: warning: unused variable 'S' [-Wunused-variable]
18 | int S = s;
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
43 ms |
340 KB |
Output is correct |
4 |
Correct |
41 ms |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
42 ms |
412 KB |
Output is correct |
11 |
Correct |
40 ms |
416 KB |
Output is correct |
12 |
Correct |
42 ms |
412 KB |
Output is correct |
13 |
Correct |
11 ms |
444 KB |
Output is correct |
14 |
Correct |
25 ms |
340 KB |
Output is correct |
15 |
Correct |
24 ms |
340 KB |
Output is correct |
16 |
Correct |
25 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
2552 KB |
Output is correct |
2 |
Correct |
80 ms |
2752 KB |
Output is correct |
3 |
Correct |
87 ms |
2812 KB |
Output is correct |
4 |
Correct |
39 ms |
468 KB |
Output is correct |
5 |
Correct |
34 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 |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
47 ms |
2784 KB |
Output is correct |
10 |
Correct |
31 ms |
2652 KB |
Output is correct |
11 |
Correct |
57 ms |
2764 KB |
Output is correct |
12 |
Correct |
59 ms |
2768 KB |
Output is correct |
13 |
Correct |
58 ms |
2788 KB |
Output is correct |
14 |
Correct |
57 ms |
2856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
340 KB |
Output is correct |
2 |
Correct |
4 ms |
340 KB |
Output is correct |
3 |
Correct |
66 ms |
2316 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
79 ms |
2772 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
29 ms |
2672 KB |
Output is correct |
9 |
Correct |
32 ms |
2764 KB |
Output is correct |
10 |
Correct |
50 ms |
2688 KB |
Output is correct |
11 |
Correct |
49 ms |
2764 KB |
Output is correct |
12 |
Correct |
57 ms |
2828 KB |
Output is correct |
13 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
56 ms |
2840 KB |
Output is correct |
20 |
Correct |
51 ms |
2792 KB |
Output is correct |
21 |
Correct |
65 ms |
2772 KB |
Output is correct |
22 |
Correct |
54 ms |
2676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
43 ms |
340 KB |
Output is correct |
4 |
Correct |
41 ms |
340 KB |
Output is correct |
5 |
Correct |
1 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 |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
42 ms |
412 KB |
Output is correct |
11 |
Correct |
40 ms |
416 KB |
Output is correct |
12 |
Correct |
42 ms |
412 KB |
Output is correct |
13 |
Correct |
11 ms |
444 KB |
Output is correct |
14 |
Correct |
25 ms |
340 KB |
Output is correct |
15 |
Correct |
24 ms |
340 KB |
Output is correct |
16 |
Correct |
25 ms |
344 KB |
Output is correct |
17 |
Correct |
82 ms |
2552 KB |
Output is correct |
18 |
Correct |
80 ms |
2752 KB |
Output is correct |
19 |
Correct |
87 ms |
2812 KB |
Output is correct |
20 |
Correct |
39 ms |
468 KB |
Output is correct |
21 |
Correct |
34 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
47 ms |
2784 KB |
Output is correct |
26 |
Correct |
31 ms |
2652 KB |
Output is correct |
27 |
Correct |
57 ms |
2764 KB |
Output is correct |
28 |
Correct |
59 ms |
2768 KB |
Output is correct |
29 |
Correct |
58 ms |
2788 KB |
Output is correct |
30 |
Correct |
57 ms |
2856 KB |
Output is correct |
31 |
Correct |
36 ms |
340 KB |
Output is correct |
32 |
Correct |
4 ms |
340 KB |
Output is correct |
33 |
Correct |
66 ms |
2316 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
79 ms |
2772 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
29 ms |
2672 KB |
Output is correct |
39 |
Correct |
32 ms |
2764 KB |
Output is correct |
40 |
Correct |
50 ms |
2688 KB |
Output is correct |
41 |
Correct |
49 ms |
2764 KB |
Output is correct |
42 |
Correct |
57 ms |
2828 KB |
Output is correct |
43 |
Correct |
0 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 |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
56 ms |
2840 KB |
Output is correct |
50 |
Correct |
51 ms |
2792 KB |
Output is correct |
51 |
Correct |
65 ms |
2772 KB |
Output is correct |
52 |
Correct |
54 ms |
2676 KB |
Output is correct |
53 |
Correct |
83 ms |
2700 KB |
Output is correct |
54 |
Correct |
87 ms |
2784 KB |
Output is correct |
55 |
Correct |
93 ms |
2652 KB |
Output is correct |
56 |
Correct |
39 ms |
432 KB |
Output is correct |
57 |
Correct |
39 ms |
344 KB |
Output is correct |
58 |
Correct |
74 ms |
2108 KB |
Output is correct |
59 |
Correct |
75 ms |
2208 KB |
Output is correct |
60 |
Correct |
70 ms |
2132 KB |
Output is correct |
61 |
Correct |
75 ms |
2220 KB |
Output is correct |
62 |
Correct |
78 ms |
2304 KB |
Output is correct |
63 |
Correct |
71 ms |
2236 KB |
Output is correct |
64 |
Correct |
126 ms |
2368 KB |
Output is correct |
65 |
Correct |
106 ms |
2420 KB |
Output is correct |
66 |
Correct |
99 ms |
2388 KB |
Output is correct |
67 |
Correct |
13 ms |
2012 KB |
Output is correct |
68 |
Correct |
35 ms |
2760 KB |
Output is correct |
69 |
Correct |
30 ms |
2708 KB |
Output is correct |
70 |
Correct |
57 ms |
2764 KB |
Output is correct |
71 |
Correct |
64 ms |
2784 KB |
Output is correct |
72 |
Correct |
58 ms |
2720 KB |
Output is correct |
73 |
Correct |
70 ms |
2848 KB |
Output is correct |
74 |
Correct |
56 ms |
2824 KB |
Output is correct |