#include <bits/stdc++.h>
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
#define eb emplace_back
#define pb push_back
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;
const int MN = 205, MM = 5e4 + 5;
int N, M, a[MM], b[MM]; ll c[MM], d[MM];
vi g[MN], rg[MN];
ll dis[2][MN], rdis[2][MN];
int from[2][MN];
bool vis[MN];
bool used[MM];
void dijk(int t, int st) {
for (int i = 0; i <= N; i++) dis[t][i] = 1e18, vis[i] = 0;
dis[t][st] = 0;
for (int i = 0; i < N; i++) {
int u = 0;
for (int v = 1; v <= N; v++) {
if (!vis[v] && dis[t][v] < dis[t][u]) {
u = v;
}
}
if (!u) break;
vis[u] = 1;
for (int e : g[u]) {
if (u != a[e]) continue;
int v = b[e], w = c[e];
if (dis[t][v] > dis[t][u] + w) {
dis[t][v] = dis[t][u] + w;
from[t][v] = e;
}
}
}
}
void rdijk(int t, int st) {
for (int i = 0; i <= N; i++) rdis[t][i] = 1e18, vis[i] = 0;
rdis[t][st] = 0;
for (int i = 0; i < N; i++) {
int u = 0;
for (int v = 1; v <= N; v++) {
if (!vis[v] && rdis[t][v] < rdis[t][u]) {
u = v;
}
}
if (!u) break;
vis[u] = 1;
for (int e : rg[u]) {
int v = a[e], w = c[e];
if (rdis[t][v] > rdis[t][u] + w) {
rdis[t][v] = rdis[t][u] + w;
}
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> N >> M;
for (int i = 1; i <= M; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
g[a[i]].pb(i), rg[b[i]].pb(i);
}
dijk(0, 1), dijk(1, N);
rdijk(0, N), rdijk(1, 1);
ll ans = dis[0][N] + dis[1][1];
int cur = N;
while (from[0][cur] && cur != 1) {
used[from[0][cur]] = 1;
cur = a[from[0][cur]];
}
cur = 1;
while (from[1][cur] && cur != N) {
used[from[1][cur]] = 1;
cur = a[from[1][cur]];
}
for (int i = 1; i <= M; i++) {
int u = a[i], v = b[i], w = c[i];
if (!used[i]) {
ll mn = dis[0][v] + w + rdis[0][u] + dis[1][v] + w + rdis[1][u];
mn = min(mn, dis[1][1] + dis[0][v] + w + rdis[0][u]);
mn = min(mn, dis[0][N] + dis[1][v] + w + rdis[1][u]);
ans = min(ans, mn + d[i]);
}
}
for (int i = 1; i <= M; i++) {
int u = a[i], v = b[i], w = c[i];
if (used[i]) {
swap(a[i], b[i]);
g[v].pb(i);
dijk(0, 1), dijk(1, N);
g[v].pop_back();
swap(a[i], b[i]);
ans = min(ans, dis[0][N] + dis[1][1] + d[i]);
}
}
if (ans >= 1e18) ans = -1;
cout << ans << '\n';
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:92:13: warning: unused variable 'u' [-Wunused-variable]
92 | int u = a[i], v = b[i], w = c[i];
| ^
ho_t4.cpp:92:33: warning: unused variable 'w' [-Wunused-variable]
92 | int u = a[i], v = b[i], w = c[i];
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
5 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
53 ms |
364 KB |
Output is correct |
11 |
Correct |
74 ms |
492 KB |
Output is correct |
12 |
Correct |
76 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
4 ms |
492 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
492 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
2284 KB |
Output is correct |
2 |
Correct |
27 ms |
2308 KB |
Output is correct |
3 |
Correct |
28 ms |
3372 KB |
Output is correct |
4 |
Correct |
4 ms |
492 KB |
Output is correct |
5 |
Correct |
6 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
26 ms |
3436 KB |
Output is correct |
10 |
Correct |
22 ms |
3436 KB |
Output is correct |
11 |
Correct |
32 ms |
3436 KB |
Output is correct |
12 |
Correct |
26 ms |
3564 KB |
Output is correct |
13 |
Correct |
29 ms |
3456 KB |
Output is correct |
14 |
Correct |
27 ms |
3436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
364 KB |
Output is correct |
2 |
Correct |
2 ms |
364 KB |
Output is correct |
3 |
Correct |
19 ms |
1772 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
25 ms |
2156 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
0 ms |
364 KB |
Output is correct |
8 |
Correct |
20 ms |
2156 KB |
Output is correct |
9 |
Correct |
22 ms |
3180 KB |
Output is correct |
10 |
Correct |
22 ms |
3180 KB |
Output is correct |
11 |
Correct |
28 ms |
3180 KB |
Output is correct |
12 |
Correct |
23 ms |
3180 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
416 KB |
Output is correct |
18 |
Correct |
1 ms |
364 KB |
Output is correct |
19 |
Correct |
22 ms |
3180 KB |
Output is correct |
20 |
Correct |
23 ms |
3180 KB |
Output is correct |
21 |
Correct |
23 ms |
3308 KB |
Output is correct |
22 |
Correct |
28 ms |
3180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
4 ms |
364 KB |
Output is correct |
4 |
Correct |
5 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
364 KB |
Output is correct |
10 |
Correct |
53 ms |
364 KB |
Output is correct |
11 |
Correct |
74 ms |
492 KB |
Output is correct |
12 |
Correct |
76 ms |
620 KB |
Output is correct |
13 |
Correct |
2 ms |
364 KB |
Output is correct |
14 |
Correct |
4 ms |
492 KB |
Output is correct |
15 |
Correct |
3 ms |
512 KB |
Output is correct |
16 |
Correct |
3 ms |
492 KB |
Output is correct |
17 |
Correct |
28 ms |
2284 KB |
Output is correct |
18 |
Correct |
27 ms |
2308 KB |
Output is correct |
19 |
Correct |
28 ms |
3372 KB |
Output is correct |
20 |
Correct |
4 ms |
492 KB |
Output is correct |
21 |
Correct |
6 ms |
492 KB |
Output is correct |
22 |
Correct |
1 ms |
364 KB |
Output is correct |
23 |
Correct |
1 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
26 ms |
3436 KB |
Output is correct |
26 |
Correct |
22 ms |
3436 KB |
Output is correct |
27 |
Correct |
32 ms |
3436 KB |
Output is correct |
28 |
Correct |
26 ms |
3564 KB |
Output is correct |
29 |
Correct |
29 ms |
3456 KB |
Output is correct |
30 |
Correct |
27 ms |
3436 KB |
Output is correct |
31 |
Correct |
5 ms |
364 KB |
Output is correct |
32 |
Correct |
2 ms |
364 KB |
Output is correct |
33 |
Correct |
19 ms |
1772 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
25 ms |
2156 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
0 ms |
364 KB |
Output is correct |
38 |
Correct |
20 ms |
2156 KB |
Output is correct |
39 |
Correct |
22 ms |
3180 KB |
Output is correct |
40 |
Correct |
22 ms |
3180 KB |
Output is correct |
41 |
Correct |
28 ms |
3180 KB |
Output is correct |
42 |
Correct |
23 ms |
3180 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
416 KB |
Output is correct |
48 |
Correct |
1 ms |
364 KB |
Output is correct |
49 |
Correct |
22 ms |
3180 KB |
Output is correct |
50 |
Correct |
23 ms |
3180 KB |
Output is correct |
51 |
Correct |
23 ms |
3308 KB |
Output is correct |
52 |
Correct |
28 ms |
3180 KB |
Output is correct |
53 |
Correct |
26 ms |
3180 KB |
Output is correct |
54 |
Correct |
30 ms |
3180 KB |
Output is correct |
55 |
Correct |
31 ms |
3180 KB |
Output is correct |
56 |
Correct |
4 ms |
492 KB |
Output is correct |
57 |
Correct |
3 ms |
492 KB |
Output is correct |
58 |
Correct |
143 ms |
2688 KB |
Output is correct |
59 |
Correct |
146 ms |
2668 KB |
Output is correct |
60 |
Correct |
199 ms |
2712 KB |
Output is correct |
61 |
Correct |
132 ms |
2668 KB |
Output is correct |
62 |
Correct |
144 ms |
2668 KB |
Output is correct |
63 |
Correct |
189 ms |
2668 KB |
Output is correct |
64 |
Correct |
205 ms |
2924 KB |
Output is correct |
65 |
Correct |
227 ms |
2796 KB |
Output is correct |
66 |
Correct |
274 ms |
2796 KB |
Output is correct |
67 |
Correct |
17 ms |
2664 KB |
Output is correct |
68 |
Correct |
23 ms |
3436 KB |
Output is correct |
69 |
Correct |
22 ms |
3436 KB |
Output is correct |
70 |
Correct |
28 ms |
3436 KB |
Output is correct |
71 |
Correct |
27 ms |
3436 KB |
Output is correct |
72 |
Correct |
28 ms |
3436 KB |
Output is correct |
73 |
Correct |
27 ms |
3436 KB |
Output is correct |
74 |
Correct |
38 ms |
3460 KB |
Output is correct |