#include <algorithm>
#include <array>
#include <iostream>
#include <queue>
#include <vector>
int main() {
int n, m;
std::cin >> n >> m;
constexpr int kInf = 1'000'000'000;
std::vector<std::vector<int>> dist(n, std::vector<int>(n, kInf));
for (int i = 0; i < n; ++i) dist[i][i] = 0;
std::vector<std::array<int, 4>> ed(m);
std::vector<std::vector<std::pair<int, int>>> g(n);
for (int i = 0; i < m; ++i) {
int u, v, c, d;
std::cin >> u >> v >> c >> d;
u--, v--;
dist[u][v] = std::min(dist[u][v], c);
ed[i] = {u, v, c, d};
g[u].emplace_back(v, i);
}
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
dist[i][j] = std::min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
int64_t ans = dist[0][n - 1] + dist[n - 1][0];
auto Dijkstra = [&](int s, int e, int v) {
std::vector<int> dist(n, kInf), used(n);
std::priority_queue<std::pair<int, int>> pq;
pq.emplace(0, s);
dist[s] = 0;
while (!pq.empty()) {
int x = pq.top().second;
pq.pop();
if (x == e) break;
if (used[x]++) continue;
for (auto i : g[x]) {
int u = i.first, z = i.second;
if (z == v) continue;
if (dist[u] > dist[x] + ed[z][2]) {
dist[u] = dist[x] + ed[z][2];
pq.emplace(-dist[u], u);
}
}
if (x == ed[v][1]) {
if (dist[ed[v][0]] > dist[x] + ed[v][2]) {
dist[ed[v][0]] = dist[x] + ed[v][2];
pq.emplace(-dist[ed[v][0]], ed[v][0]);
}
}
}
return dist[e];
};
for (int i = 0; i < m; ++i) {
int u = ed[i][0], v = ed[i][1], c = ed[i][2], d = ed[i][3];
int64_t a = std::min(dist[0][n - 1], dist[0][v] + dist[u][n - 1] + c);
int64_t b = std::min(dist[n - 1][0], dist[n - 1][v] + dist[u][0] + c);
if (a + b + d < ans) {
int64_t x = Dijkstra(0, n - 1, i);
int64_t y = Dijkstra(n - 1, 0, i);
ans = std::min(ans, x + y + d);
}
}
if (ans >= kInf) ans = -1;
std::cout << ans << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
504 KB |
Output is correct |
2 |
Correct |
14 ms |
504 KB |
Output is correct |
3 |
Correct |
16 ms |
508 KB |
Output is correct |
4 |
Correct |
19 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
14 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
18 ms |
504 KB |
Output is correct |
11 |
Correct |
27 ms |
504 KB |
Output is correct |
12 |
Correct |
16 ms |
632 KB |
Output is correct |
13 |
Correct |
16 ms |
504 KB |
Output is correct |
14 |
Correct |
15 ms |
504 KB |
Output is correct |
15 |
Correct |
16 ms |
504 KB |
Output is correct |
16 |
Correct |
16 ms |
504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
1912 KB |
Output is correct |
2 |
Correct |
90 ms |
1912 KB |
Output is correct |
3 |
Correct |
100 ms |
1916 KB |
Output is correct |
4 |
Correct |
17 ms |
504 KB |
Output is correct |
5 |
Correct |
15 ms |
504 KB |
Output is correct |
6 |
Correct |
15 ms |
508 KB |
Output is correct |
7 |
Correct |
16 ms |
504 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
105 ms |
2040 KB |
Output is correct |
10 |
Correct |
102 ms |
1912 KB |
Output is correct |
11 |
Correct |
100 ms |
1912 KB |
Output is correct |
12 |
Correct |
98 ms |
1912 KB |
Output is correct |
13 |
Correct |
99 ms |
1960 KB |
Output is correct |
14 |
Correct |
103 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
508 KB |
Output is correct |
2 |
Correct |
14 ms |
504 KB |
Output is correct |
3 |
Correct |
72 ms |
1528 KB |
Output is correct |
4 |
Correct |
14 ms |
504 KB |
Output is correct |
5 |
Correct |
87 ms |
1912 KB |
Output is correct |
6 |
Correct |
5 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
94 ms |
2040 KB |
Output is correct |
9 |
Correct |
91 ms |
1912 KB |
Output is correct |
10 |
Correct |
87 ms |
1912 KB |
Output is correct |
11 |
Correct |
87 ms |
1912 KB |
Output is correct |
12 |
Correct |
91 ms |
1912 KB |
Output is correct |
13 |
Correct |
5 ms |
376 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
5 ms |
256 KB |
Output is correct |
16 |
Correct |
5 ms |
256 KB |
Output is correct |
17 |
Correct |
5 ms |
256 KB |
Output is correct |
18 |
Correct |
4 ms |
256 KB |
Output is correct |
19 |
Correct |
87 ms |
2040 KB |
Output is correct |
20 |
Correct |
88 ms |
1912 KB |
Output is correct |
21 |
Correct |
88 ms |
1912 KB |
Output is correct |
22 |
Correct |
90 ms |
1912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
504 KB |
Output is correct |
2 |
Correct |
14 ms |
504 KB |
Output is correct |
3 |
Correct |
16 ms |
508 KB |
Output is correct |
4 |
Correct |
19 ms |
504 KB |
Output is correct |
5 |
Correct |
6 ms |
376 KB |
Output is correct |
6 |
Correct |
14 ms |
504 KB |
Output is correct |
7 |
Correct |
5 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
6 ms |
376 KB |
Output is correct |
10 |
Correct |
18 ms |
504 KB |
Output is correct |
11 |
Correct |
27 ms |
504 KB |
Output is correct |
12 |
Correct |
16 ms |
632 KB |
Output is correct |
13 |
Correct |
16 ms |
504 KB |
Output is correct |
14 |
Correct |
15 ms |
504 KB |
Output is correct |
15 |
Correct |
16 ms |
504 KB |
Output is correct |
16 |
Correct |
16 ms |
504 KB |
Output is correct |
17 |
Correct |
90 ms |
1912 KB |
Output is correct |
18 |
Correct |
90 ms |
1912 KB |
Output is correct |
19 |
Correct |
100 ms |
1916 KB |
Output is correct |
20 |
Correct |
17 ms |
504 KB |
Output is correct |
21 |
Correct |
15 ms |
504 KB |
Output is correct |
22 |
Correct |
15 ms |
508 KB |
Output is correct |
23 |
Correct |
16 ms |
504 KB |
Output is correct |
24 |
Correct |
5 ms |
256 KB |
Output is correct |
25 |
Correct |
105 ms |
2040 KB |
Output is correct |
26 |
Correct |
102 ms |
1912 KB |
Output is correct |
27 |
Correct |
100 ms |
1912 KB |
Output is correct |
28 |
Correct |
98 ms |
1912 KB |
Output is correct |
29 |
Correct |
99 ms |
1960 KB |
Output is correct |
30 |
Correct |
103 ms |
2168 KB |
Output is correct |
31 |
Correct |
15 ms |
508 KB |
Output is correct |
32 |
Correct |
14 ms |
504 KB |
Output is correct |
33 |
Correct |
72 ms |
1528 KB |
Output is correct |
34 |
Correct |
14 ms |
504 KB |
Output is correct |
35 |
Correct |
87 ms |
1912 KB |
Output is correct |
36 |
Correct |
5 ms |
256 KB |
Output is correct |
37 |
Correct |
4 ms |
256 KB |
Output is correct |
38 |
Correct |
94 ms |
2040 KB |
Output is correct |
39 |
Correct |
91 ms |
1912 KB |
Output is correct |
40 |
Correct |
87 ms |
1912 KB |
Output is correct |
41 |
Correct |
87 ms |
1912 KB |
Output is correct |
42 |
Correct |
91 ms |
1912 KB |
Output is correct |
43 |
Correct |
5 ms |
376 KB |
Output is correct |
44 |
Correct |
5 ms |
256 KB |
Output is correct |
45 |
Correct |
5 ms |
256 KB |
Output is correct |
46 |
Correct |
5 ms |
256 KB |
Output is correct |
47 |
Correct |
5 ms |
256 KB |
Output is correct |
48 |
Correct |
4 ms |
256 KB |
Output is correct |
49 |
Correct |
87 ms |
2040 KB |
Output is correct |
50 |
Correct |
88 ms |
1912 KB |
Output is correct |
51 |
Correct |
88 ms |
1912 KB |
Output is correct |
52 |
Correct |
90 ms |
1912 KB |
Output is correct |
53 |
Correct |
94 ms |
1912 KB |
Output is correct |
54 |
Correct |
90 ms |
1912 KB |
Output is correct |
55 |
Correct |
87 ms |
1912 KB |
Output is correct |
56 |
Correct |
16 ms |
504 KB |
Output is correct |
57 |
Correct |
16 ms |
504 KB |
Output is correct |
58 |
Correct |
82 ms |
1528 KB |
Output is correct |
59 |
Correct |
78 ms |
1552 KB |
Output is correct |
60 |
Correct |
80 ms |
1656 KB |
Output is correct |
61 |
Correct |
80 ms |
1528 KB |
Output is correct |
62 |
Correct |
115 ms |
1656 KB |
Output is correct |
63 |
Correct |
165 ms |
1528 KB |
Output is correct |
64 |
Correct |
133 ms |
1908 KB |
Output is correct |
65 |
Correct |
253 ms |
1892 KB |
Output is correct |
66 |
Correct |
418 ms |
1892 KB |
Output is correct |
67 |
Correct |
68 ms |
1652 KB |
Output is correct |
68 |
Correct |
103 ms |
2048 KB |
Output is correct |
69 |
Correct |
103 ms |
1912 KB |
Output is correct |
70 |
Correct |
101 ms |
1912 KB |
Output is correct |
71 |
Correct |
100 ms |
2040 KB |
Output is correct |
72 |
Correct |
99 ms |
1912 KB |
Output is correct |
73 |
Correct |
100 ms |
1912 KB |
Output is correct |
74 |
Correct |
104 ms |
2072 KB |
Output is correct |