#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 200 + 2;
const int M = 5e4 + 2;
const ll linf = 1e18;
ll dist[N][N], di[N];
vector<pair<int, int>> g[N];
int u[M], v[M], c[M], d[M], is[M], prv[N], n, m;
void Dijkstra(int x) {
for (int i = 0; i < n; i++) di[i] = linf;
vector<int> was(n);
priority_queue<pair<ll, int>> pq;
di[x] = 0;
pq.push({0, x});
while (!pq.empty()) {
ll v = -pq.top().first;
int s = pq.top().second;
pq.pop();
if (was[s] == 1) continue;
was[s] = 1;
for (auto zz : g[s]) {
int u = zz.first; int i = zz.second;
if (di[u] > v + c[i]) {
di[u] = v + c[i];
pq.push({-di[u], u});
prv[u] = i;
}
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) if (i != j) dist[i][j] = linf;
for (int i = 0; i < m; i++) {
cin >> u[i] >> v[i] >> c[i] >> d[i];
u[i] -= 1; v[i] -= 1;
smin(dist[u[i]][v[i]],(ll) c[i]);
g[u[i]].push_back({v[i], i});
}
for (int k = 0; k < n; k++) for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) smin(dist[i][j], dist[i][k] + dist[k][j]);
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(0);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
for (int i = 0; i < n; i++) prv[i] = -1;
Dijkstra(n - 1);
for (int i = 0; i < n; i++) if (prv[i] != -1) is[prv[i]] = 1;
ll ans = linf;
if (dist[0][n - 1] != linf && dist[n - 1][0] != linf) ans = dist[0][n - 1] + dist[n - 1][0];
for (int i = 0; i < m; i++) {
if (is[i] == 0) {
ll d1 = min(dist[0][n - 1], dist[0][v[i]] + c[i] + dist[u[i]][n - 1]);
ll d2 = min(dist[n - 1][0], dist[n - 1][v[i]] + c[i] + dist[u[i]][0]);
if (d1 < linf && d2 < linf) smin(ans, d1 + d2 + d[i]);
continue;
}
for (int j = 0; j < n; j++) g[j].clear();
for (int j = 0; j < m; j++) {
if (i == j) g[v[j]].push_back({u[j], j});
else g[u[j]].push_back({v[j], j});
}
ll ret = d[i];
if (ans <= ret) continue;
Dijkstra(0);
ret += di[n - 1];
if (ans <= ret) continue;
Dijkstra(n - 1);
if (di[0] == linf) continue;
ret += di[0];
smin(ans, ret);
}
if (ans == linf) ans = -1;
cout << ans << en;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
676 KB |
Output is correct |
3 |
Correct |
12 ms |
596 KB |
Output is correct |
4 |
Correct |
12 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
684 KB |
Output is correct |
7 |
Correct |
0 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 |
26 ms |
596 KB |
Output is correct |
11 |
Correct |
34 ms |
596 KB |
Output is correct |
12 |
Correct |
30 ms |
596 KB |
Output is correct |
13 |
Correct |
11 ms |
708 KB |
Output is correct |
14 |
Correct |
19 ms |
716 KB |
Output is correct |
15 |
Correct |
12 ms |
596 KB |
Output is correct |
16 |
Correct |
16 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
2464 KB |
Output is correct |
2 |
Correct |
232 ms |
2468 KB |
Output is correct |
3 |
Correct |
156 ms |
2376 KB |
Output is correct |
4 |
Correct |
26 ms |
860 KB |
Output is correct |
5 |
Correct |
16 ms |
596 KB |
Output is correct |
6 |
Correct |
8 ms |
596 KB |
Output is correct |
7 |
Correct |
9 ms |
596 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
92 ms |
2248 KB |
Output is correct |
10 |
Correct |
88 ms |
2288 KB |
Output is correct |
11 |
Correct |
151 ms |
2324 KB |
Output is correct |
12 |
Correct |
155 ms |
2440 KB |
Output is correct |
13 |
Correct |
136 ms |
2344 KB |
Output is correct |
14 |
Correct |
120 ms |
2316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
596 KB |
Output is correct |
3 |
Correct |
149 ms |
1888 KB |
Output is correct |
4 |
Correct |
11 ms |
680 KB |
Output is correct |
5 |
Correct |
161 ms |
2228 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
108 ms |
2264 KB |
Output is correct |
9 |
Correct |
116 ms |
2284 KB |
Output is correct |
10 |
Correct |
182 ms |
2312 KB |
Output is correct |
11 |
Correct |
167 ms |
2364 KB |
Output is correct |
12 |
Correct |
168 ms |
2364 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
19 |
Correct |
170 ms |
2332 KB |
Output is correct |
20 |
Correct |
155 ms |
2280 KB |
Output is correct |
21 |
Correct |
154 ms |
2380 KB |
Output is correct |
22 |
Correct |
162 ms |
2212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
596 KB |
Output is correct |
2 |
Correct |
9 ms |
676 KB |
Output is correct |
3 |
Correct |
12 ms |
596 KB |
Output is correct |
4 |
Correct |
12 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
9 ms |
684 KB |
Output is correct |
7 |
Correct |
0 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 |
26 ms |
596 KB |
Output is correct |
11 |
Correct |
34 ms |
596 KB |
Output is correct |
12 |
Correct |
30 ms |
596 KB |
Output is correct |
13 |
Correct |
11 ms |
708 KB |
Output is correct |
14 |
Correct |
19 ms |
716 KB |
Output is correct |
15 |
Correct |
12 ms |
596 KB |
Output is correct |
16 |
Correct |
16 ms |
596 KB |
Output is correct |
17 |
Correct |
164 ms |
2464 KB |
Output is correct |
18 |
Correct |
232 ms |
2468 KB |
Output is correct |
19 |
Correct |
156 ms |
2376 KB |
Output is correct |
20 |
Correct |
26 ms |
860 KB |
Output is correct |
21 |
Correct |
16 ms |
596 KB |
Output is correct |
22 |
Correct |
8 ms |
596 KB |
Output is correct |
23 |
Correct |
9 ms |
596 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
92 ms |
2248 KB |
Output is correct |
26 |
Correct |
88 ms |
2288 KB |
Output is correct |
27 |
Correct |
151 ms |
2324 KB |
Output is correct |
28 |
Correct |
155 ms |
2440 KB |
Output is correct |
29 |
Correct |
136 ms |
2344 KB |
Output is correct |
30 |
Correct |
120 ms |
2316 KB |
Output is correct |
31 |
Correct |
14 ms |
596 KB |
Output is correct |
32 |
Correct |
9 ms |
596 KB |
Output is correct |
33 |
Correct |
149 ms |
1888 KB |
Output is correct |
34 |
Correct |
11 ms |
680 KB |
Output is correct |
35 |
Correct |
161 ms |
2228 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
108 ms |
2264 KB |
Output is correct |
39 |
Correct |
116 ms |
2284 KB |
Output is correct |
40 |
Correct |
182 ms |
2312 KB |
Output is correct |
41 |
Correct |
167 ms |
2364 KB |
Output is correct |
42 |
Correct |
168 ms |
2364 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
0 ms |
340 KB |
Output is correct |
45 |
Correct |
0 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
340 KB |
Output is correct |
48 |
Correct |
0 ms |
340 KB |
Output is correct |
49 |
Correct |
170 ms |
2332 KB |
Output is correct |
50 |
Correct |
155 ms |
2280 KB |
Output is correct |
51 |
Correct |
154 ms |
2380 KB |
Output is correct |
52 |
Correct |
162 ms |
2212 KB |
Output is correct |
53 |
Correct |
147 ms |
2296 KB |
Output is correct |
54 |
Correct |
183 ms |
2316 KB |
Output is correct |
55 |
Correct |
176 ms |
2324 KB |
Output is correct |
56 |
Correct |
11 ms |
596 KB |
Output is correct |
57 |
Correct |
11 ms |
596 KB |
Output is correct |
58 |
Correct |
142 ms |
1948 KB |
Output is correct |
59 |
Correct |
172 ms |
1996 KB |
Output is correct |
60 |
Correct |
235 ms |
2032 KB |
Output is correct |
61 |
Correct |
143 ms |
1960 KB |
Output is correct |
62 |
Correct |
171 ms |
1952 KB |
Output is correct |
63 |
Correct |
233 ms |
1948 KB |
Output is correct |
64 |
Correct |
708 ms |
2484 KB |
Output is correct |
65 |
Correct |
719 ms |
2556 KB |
Output is correct |
66 |
Correct |
940 ms |
2628 KB |
Output is correct |
67 |
Correct |
12 ms |
2264 KB |
Output is correct |
68 |
Correct |
91 ms |
3416 KB |
Output is correct |
69 |
Correct |
93 ms |
3400 KB |
Output is correct |
70 |
Correct |
195 ms |
3572 KB |
Output is correct |
71 |
Correct |
148 ms |
3532 KB |
Output is correct |
72 |
Correct |
146 ms |
3476 KB |
Output is correct |
73 |
Correct |
147 ms |
3532 KB |
Output is correct |
74 |
Correct |
171 ms |
3496 KB |
Output is correct |