#include <bits/stdc++.h>
using namespace std;
#ifdef ngu
#include "debug.h"
#else
#define debug(...) 42
#endif
#define int long long
const int N = 205;
const int M = 50005;
const int INF = (int) 1e17;
int n, m;
int D[N], d[N][N];
int from[N];
int c[M], cost[M];
bool on_tree[M];
vector<pair<int, int>> g[N];
pair<int, int> edge[M];
void dijkstra(int s, int d[], vector<pair<int, int>> g[N]) {
fill(d + 1, d + n + 1, INF);
using T = pair<int, int>;
priority_queue<T, vector<T>, greater<T>> pq;
d[s] = 0;
pq.emplace(0, s);
while (pq.size()) {
auto [x, u] = pq.top();
pq.pop();
if (d[u] == x) {
for (auto [v, id] : g[u]) {
if (d[v] > x + c[id]) {
from[v] = id;
pq.emplace(d[v] = x + c[id], v);
}
}
}
}
for (int i = 1; i <= n; ++i) {
on_tree[from[i]] = true;
}
}
int get_dist(int s, int t) {
fill(D + 1, D + n + 1, INF);
using T = pair<int, int>;
priority_queue<T, vector<T>, greater<T>> pq;
D[s] = 0;
pq.emplace(0, s);
while (pq.size()) {
auto [x, u] = pq.top();
pq.pop();
if (D[u] == x) {
for (auto [v, id] : g[u]) {
if (D[v] > x + c[id]) {
pq.emplace(D[v] = x + c[id], v);
}
}
}
}
return D[t];
}
void add(int u, int v, int id) {
g[u].emplace_back(v, id);
}
void del(int u, int v, int id) {
for (auto &x : g[u]) {
if (x == make_pair(v, id)) {
swap(x, g[u].back());
g[u].pop_back();
break;
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
#ifdef ngu
freopen("test.inp", "r", stdin);
freopen("test.out", "w", stdout);
#endif
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
if (i != j) {
d[i][j] = INF;
}
}
}
for (int i = 1; i <= m; ++i) {
int u, v;
cin >> u >> v >> c[i] >> cost[i];
edge[i] = {u, v};
d[u][v] = min(d[u][v], c[i]);
add(u, v, i);
}
dijkstra(1, D, g);
dijkstra(n, D, g);
for (int mid = 1; mid <= n; ++mid) {
for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) {
d[i][j] = min(d[i][j], d[i][mid] + d[mid][j]);
}
}
}
int res = 3 * INF;
if (d[1][n] < INF && d[n][1] < INF) {
res = d[1][n] + d[n][1];
}
for (int i = 1; i <= m; ++i) {
auto [u, v] = edge[i];
if (on_tree[i]) {
del(u, v, i);
add(v, u, i);
int A = get_dist(1, n);
int B = get_dist(n, 1);
if (A < INF && B < INF) {
res = min(res, A + B + cost[i]);
}
del(v, u, i);
add(u, v, i);
} else {
int A = min(d[1][n], d[1][v] + c[i] + d[u][n]);
int B = min(d[n][1], d[n][v] + c[i] + d[u][1]);
if (A < INF && B < INF) {
res = min(res, A + B + cost[i]);
}
}
}
cout << (res == 3 * INF ? -1 : res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
19 ms |
856 KB |
Output is correct |
4 |
Correct |
20 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
21 ms |
860 KB |
Output is correct |
11 |
Correct |
24 ms |
856 KB |
Output is correct |
12 |
Correct |
24 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
860 KB |
Output is correct |
14 |
Correct |
14 ms |
884 KB |
Output is correct |
15 |
Correct |
13 ms |
880 KB |
Output is correct |
16 |
Correct |
14 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
3764 KB |
Output is correct |
2 |
Correct |
118 ms |
3672 KB |
Output is correct |
3 |
Correct |
110 ms |
3760 KB |
Output is correct |
4 |
Correct |
16 ms |
856 KB |
Output is correct |
5 |
Correct |
11 ms |
856 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
8 ms |
840 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
40 ms |
4848 KB |
Output is correct |
10 |
Correct |
35 ms |
4700 KB |
Output is correct |
11 |
Correct |
78 ms |
5104 KB |
Output is correct |
12 |
Correct |
76 ms |
4948 KB |
Output is correct |
13 |
Correct |
81 ms |
4956 KB |
Output is correct |
14 |
Correct |
62 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
69 ms |
2908 KB |
Output is correct |
4 |
Correct |
8 ms |
604 KB |
Output is correct |
5 |
Correct |
84 ms |
3688 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
600 KB |
Output is correct |
8 |
Correct |
33 ms |
3672 KB |
Output is correct |
9 |
Correct |
37 ms |
3676 KB |
Output is correct |
10 |
Correct |
61 ms |
3672 KB |
Output is correct |
11 |
Correct |
61 ms |
3476 KB |
Output is correct |
12 |
Correct |
59 ms |
3672 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 |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
59 ms |
4736 KB |
Output is correct |
20 |
Correct |
53 ms |
4440 KB |
Output is correct |
21 |
Correct |
62 ms |
4444 KB |
Output is correct |
22 |
Correct |
60 ms |
4632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
860 KB |
Output is correct |
2 |
Correct |
8 ms |
604 KB |
Output is correct |
3 |
Correct |
19 ms |
856 KB |
Output is correct |
4 |
Correct |
20 ms |
860 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
8 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
21 ms |
860 KB |
Output is correct |
11 |
Correct |
24 ms |
856 KB |
Output is correct |
12 |
Correct |
24 ms |
860 KB |
Output is correct |
13 |
Correct |
10 ms |
860 KB |
Output is correct |
14 |
Correct |
14 ms |
884 KB |
Output is correct |
15 |
Correct |
13 ms |
880 KB |
Output is correct |
16 |
Correct |
14 ms |
640 KB |
Output is correct |
17 |
Correct |
112 ms |
3764 KB |
Output is correct |
18 |
Correct |
118 ms |
3672 KB |
Output is correct |
19 |
Correct |
110 ms |
3760 KB |
Output is correct |
20 |
Correct |
16 ms |
856 KB |
Output is correct |
21 |
Correct |
11 ms |
856 KB |
Output is correct |
22 |
Correct |
8 ms |
604 KB |
Output is correct |
23 |
Correct |
8 ms |
840 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
40 ms |
4848 KB |
Output is correct |
26 |
Correct |
35 ms |
4700 KB |
Output is correct |
27 |
Correct |
78 ms |
5104 KB |
Output is correct |
28 |
Correct |
76 ms |
4948 KB |
Output is correct |
29 |
Correct |
81 ms |
4956 KB |
Output is correct |
30 |
Correct |
62 ms |
4952 KB |
Output is correct |
31 |
Correct |
12 ms |
860 KB |
Output is correct |
32 |
Correct |
8 ms |
604 KB |
Output is correct |
33 |
Correct |
69 ms |
2908 KB |
Output is correct |
34 |
Correct |
8 ms |
604 KB |
Output is correct |
35 |
Correct |
84 ms |
3688 KB |
Output is correct |
36 |
Correct |
0 ms |
344 KB |
Output is correct |
37 |
Correct |
0 ms |
600 KB |
Output is correct |
38 |
Correct |
33 ms |
3672 KB |
Output is correct |
39 |
Correct |
37 ms |
3676 KB |
Output is correct |
40 |
Correct |
61 ms |
3672 KB |
Output is correct |
41 |
Correct |
61 ms |
3476 KB |
Output is correct |
42 |
Correct |
59 ms |
3672 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 |
1 ms |
344 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
59 ms |
4736 KB |
Output is correct |
50 |
Correct |
53 ms |
4440 KB |
Output is correct |
51 |
Correct |
62 ms |
4444 KB |
Output is correct |
52 |
Correct |
60 ms |
4632 KB |
Output is correct |
53 |
Correct |
117 ms |
4696 KB |
Output is correct |
54 |
Correct |
125 ms |
4508 KB |
Output is correct |
55 |
Correct |
120 ms |
4700 KB |
Output is correct |
56 |
Correct |
23 ms |
772 KB |
Output is correct |
57 |
Correct |
19 ms |
860 KB |
Output is correct |
58 |
Correct |
78 ms |
3928 KB |
Output is correct |
59 |
Correct |
95 ms |
3920 KB |
Output is correct |
60 |
Correct |
123 ms |
3672 KB |
Output is correct |
61 |
Correct |
74 ms |
3928 KB |
Output is correct |
62 |
Correct |
87 ms |
3724 KB |
Output is correct |
63 |
Correct |
127 ms |
3672 KB |
Output is correct |
64 |
Correct |
586 ms |
4652 KB |
Output is correct |
65 |
Correct |
605 ms |
4376 KB |
Output is correct |
66 |
Correct |
795 ms |
4416 KB |
Output is correct |
67 |
Correct |
11 ms |
3292 KB |
Output is correct |
68 |
Correct |
40 ms |
4700 KB |
Output is correct |
69 |
Correct |
39 ms |
4700 KB |
Output is correct |
70 |
Correct |
84 ms |
4904 KB |
Output is correct |
71 |
Correct |
87 ms |
4696 KB |
Output is correct |
72 |
Correct |
85 ms |
4700 KB |
Output is correct |
73 |
Correct |
88 ms |
4952 KB |
Output is correct |
74 |
Correct |
84 ms |
4892 KB |
Output is correct |