#include <bits/stdc++.h>
using namespace std;
#define int long long
#define fs first
#define sc second
#define mp make_pair
typedef pair<int, int> pii;
struct EDGE {
int u, v, c, d, is_tree[2], id;
EDGE() {}
};
const int MXN = 205, MXM = 50005, INF = 3000000000000000000;
int n, m, dis1[MXN], dis1_inv[MXN], disn[MXN], disn_inv[MXN], p[MXN], dis[MXN];
vector<int> v[MXN][MXN];
EDGE edge[MXM];
bitset<MXN> b;
inline void DIJKSTRA(int sr, int *dis, int id, int inv) {
b.reset();
fill(dis + 1, dis + n + 1, INF);
fill(p + 1, p + n + 1, 0);
dis[sr] = 0;
for (int $ = 0; $ < n; $++) {
int x = -1;
for (int i = 1; i <= n; i++) {
if (b[i]) continue;
if (x == -1) x = i;
else if (dis[x] > dis[i]) x = i;
}
// if (x == -1) cout << "ERROR" << endl;
// cout << "x: " << x << endl;
b[x] = true;
if (id != -1) edge[p[x]].is_tree[id] = true;
for (int j = 1; j <= n; j++) for (auto i : v[x][j]) {
if (inv) {
if (i > 0) continue;
i *= -1;
} else {
if (i < 0) continue;
}
// if (b[i]) continue;
if (dis[x] + edge[i].c < dis[j]) {
dis[j] = dis[x] + edge[i].c;
p[j] = i;
}
}
}
}
inline int SOLVE_1(int id) {
if (edge[id].is_tree[0]) {
DIJKSTRA(1, dis, -1, false);
return dis[n];
}
return min(dis1[n], dis1[edge[id].u] + edge[id].c + disn_inv[edge[id].v]);
}
inline int SOLVE_n(int id) {
if (edge[id].is_tree[1]) {
DIJKSTRA(n, dis, -1, false);
return dis[1];
}
return min(disn[1], disn[edge[id].u] + edge[id].c + dis1_inv[edge[id].v]);
}
int32_t main() {
cin.tie(0) -> sync_with_stdio(false);
cin >> n >> m;
for (int i = 1; i <= m; i++) {
cin >> edge[i].u >> edge[i].v >> edge[i].c >> edge[i].d;
edge[i].id = v[edge[i].u][edge[i].v].size();
v[edge[i].u][edge[i].v].push_back(i);
v[edge[i].v][edge[i].u].push_back(-i);
}
DIJKSTRA(1, dis1, 0, false);
DIJKSTRA(1, dis1_inv, -1, true);
DIJKSTRA(n, disn, 1, false);
DIJKSTRA(n, disn_inv, -1, true);
// for (int i = 1; i <= m; i++) cout << edge[i].is_tree[1];
// cout << endl;
// return 0;
int x = dis1[n] + disn[1];
for (int i = 1; i <= m; i++) {
swap(edge[i].u, edge[i].v);
v[edge[i].u][edge[i].v][edge[i].id] *= -1;
v[edge[i].v][edge[i].u][edge[i].id] *= -1;
x = min(x, edge[i].d + SOLVE_1(i) + SOLVE_n(i));
swap(edge[i].u, edge[i].v);
v[edge[i].u][edge[i].v][edge[i].id] *= -1;
v[edge[i].v][edge[i].u][edge[i].id] *= -1;
}
cout << (x >= INF ? -1LL : x) << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
1372 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
3 |
Correct |
57 ms |
1548 KB |
Output is correct |
4 |
Correct |
58 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
58 ms |
1372 KB |
Output is correct |
11 |
Correct |
58 ms |
1372 KB |
Output is correct |
12 |
Correct |
62 ms |
1368 KB |
Output is correct |
13 |
Correct |
27 ms |
1368 KB |
Output is correct |
14 |
Correct |
43 ms |
1624 KB |
Output is correct |
15 |
Correct |
41 ms |
1372 KB |
Output is correct |
16 |
Correct |
42 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
347 ms |
5464 KB |
Output is correct |
2 |
Correct |
353 ms |
5484 KB |
Output is correct |
3 |
Correct |
349 ms |
5460 KB |
Output is correct |
4 |
Correct |
58 ms |
1628 KB |
Output is correct |
5 |
Correct |
44 ms |
1372 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
5 ms |
1492 KB |
Output is correct |
8 |
Correct |
1 ms |
1624 KB |
Output is correct |
9 |
Correct |
172 ms |
5468 KB |
Output is correct |
10 |
Correct |
162 ms |
5464 KB |
Output is correct |
11 |
Correct |
274 ms |
5480 KB |
Output is correct |
12 |
Correct |
268 ms |
5480 KB |
Output is correct |
13 |
Correct |
258 ms |
5468 KB |
Output is correct |
14 |
Correct |
243 ms |
5488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
1368 KB |
Output is correct |
2 |
Correct |
11 ms |
1368 KB |
Output is correct |
3 |
Correct |
387 ms |
5056 KB |
Output is correct |
4 |
Correct |
6 ms |
1372 KB |
Output is correct |
5 |
Correct |
448 ms |
5724 KB |
Output is correct |
6 |
Correct |
1 ms |
1368 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
199 ms |
5792 KB |
Output is correct |
9 |
Correct |
221 ms |
5720 KB |
Output is correct |
10 |
Correct |
334 ms |
5720 KB |
Output is correct |
11 |
Correct |
347 ms |
5672 KB |
Output is correct |
12 |
Correct |
341 ms |
5468 KB |
Output is correct |
13 |
Correct |
1 ms |
1372 KB |
Output is correct |
14 |
Correct |
1 ms |
1372 KB |
Output is correct |
15 |
Correct |
1 ms |
1372 KB |
Output is correct |
16 |
Correct |
1 ms |
1368 KB |
Output is correct |
17 |
Correct |
1 ms |
1372 KB |
Output is correct |
18 |
Correct |
1 ms |
1372 KB |
Output is correct |
19 |
Correct |
344 ms |
5668 KB |
Output is correct |
20 |
Correct |
330 ms |
5664 KB |
Output is correct |
21 |
Correct |
327 ms |
5468 KB |
Output is correct |
22 |
Correct |
336 ms |
5660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
1372 KB |
Output is correct |
2 |
Correct |
2 ms |
1372 KB |
Output is correct |
3 |
Correct |
57 ms |
1548 KB |
Output is correct |
4 |
Correct |
58 ms |
1372 KB |
Output is correct |
5 |
Correct |
2 ms |
1372 KB |
Output is correct |
6 |
Correct |
3 ms |
1372 KB |
Output is correct |
7 |
Correct |
1 ms |
1372 KB |
Output is correct |
8 |
Correct |
1 ms |
1372 KB |
Output is correct |
9 |
Correct |
1 ms |
1368 KB |
Output is correct |
10 |
Correct |
58 ms |
1372 KB |
Output is correct |
11 |
Correct |
58 ms |
1372 KB |
Output is correct |
12 |
Correct |
62 ms |
1368 KB |
Output is correct |
13 |
Correct |
27 ms |
1368 KB |
Output is correct |
14 |
Correct |
43 ms |
1624 KB |
Output is correct |
15 |
Correct |
41 ms |
1372 KB |
Output is correct |
16 |
Correct |
42 ms |
1372 KB |
Output is correct |
17 |
Correct |
347 ms |
5464 KB |
Output is correct |
18 |
Correct |
353 ms |
5484 KB |
Output is correct |
19 |
Correct |
349 ms |
5460 KB |
Output is correct |
20 |
Correct |
58 ms |
1628 KB |
Output is correct |
21 |
Correct |
44 ms |
1372 KB |
Output is correct |
22 |
Correct |
1 ms |
1368 KB |
Output is correct |
23 |
Correct |
5 ms |
1492 KB |
Output is correct |
24 |
Correct |
1 ms |
1624 KB |
Output is correct |
25 |
Correct |
172 ms |
5468 KB |
Output is correct |
26 |
Correct |
162 ms |
5464 KB |
Output is correct |
27 |
Correct |
274 ms |
5480 KB |
Output is correct |
28 |
Correct |
268 ms |
5480 KB |
Output is correct |
29 |
Correct |
258 ms |
5468 KB |
Output is correct |
30 |
Correct |
243 ms |
5488 KB |
Output is correct |
31 |
Correct |
54 ms |
1368 KB |
Output is correct |
32 |
Correct |
11 ms |
1368 KB |
Output is correct |
33 |
Correct |
387 ms |
5056 KB |
Output is correct |
34 |
Correct |
6 ms |
1372 KB |
Output is correct |
35 |
Correct |
448 ms |
5724 KB |
Output is correct |
36 |
Correct |
1 ms |
1368 KB |
Output is correct |
37 |
Correct |
1 ms |
1372 KB |
Output is correct |
38 |
Correct |
199 ms |
5792 KB |
Output is correct |
39 |
Correct |
221 ms |
5720 KB |
Output is correct |
40 |
Correct |
334 ms |
5720 KB |
Output is correct |
41 |
Correct |
347 ms |
5672 KB |
Output is correct |
42 |
Correct |
341 ms |
5468 KB |
Output is correct |
43 |
Correct |
1 ms |
1372 KB |
Output is correct |
44 |
Correct |
1 ms |
1372 KB |
Output is correct |
45 |
Correct |
1 ms |
1372 KB |
Output is correct |
46 |
Correct |
1 ms |
1368 KB |
Output is correct |
47 |
Correct |
1 ms |
1372 KB |
Output is correct |
48 |
Correct |
1 ms |
1372 KB |
Output is correct |
49 |
Correct |
344 ms |
5668 KB |
Output is correct |
50 |
Correct |
330 ms |
5664 KB |
Output is correct |
51 |
Correct |
327 ms |
5468 KB |
Output is correct |
52 |
Correct |
336 ms |
5660 KB |
Output is correct |
53 |
Correct |
453 ms |
5872 KB |
Output is correct |
54 |
Correct |
452 ms |
5868 KB |
Output is correct |
55 |
Correct |
459 ms |
5860 KB |
Output is correct |
56 |
Correct |
57 ms |
1372 KB |
Output is correct |
57 |
Correct |
58 ms |
1372 KB |
Output is correct |
58 |
Correct |
387 ms |
5084 KB |
Output is correct |
59 |
Correct |
395 ms |
4952 KB |
Output is correct |
60 |
Correct |
395 ms |
4956 KB |
Output is correct |
61 |
Correct |
393 ms |
5076 KB |
Output is correct |
62 |
Correct |
394 ms |
5072 KB |
Output is correct |
63 |
Correct |
387 ms |
4952 KB |
Output is correct |
64 |
Correct |
309 ms |
5128 KB |
Output is correct |
65 |
Correct |
294 ms |
4952 KB |
Output is correct |
66 |
Correct |
297 ms |
4956 KB |
Output is correct |
67 |
Correct |
10 ms |
4568 KB |
Output is correct |
68 |
Correct |
197 ms |
5852 KB |
Output is correct |
69 |
Correct |
199 ms |
5724 KB |
Output is correct |
70 |
Correct |
335 ms |
5468 KB |
Output is correct |
71 |
Correct |
337 ms |
5468 KB |
Output is correct |
72 |
Correct |
336 ms |
5672 KB |
Output is correct |
73 |
Correct |
345 ms |
5672 KB |
Output is correct |
74 |
Correct |
339 ms |
5468 KB |
Output is correct |