#include <bits/stdc++.h>
using namespace std;
typedef long long llong;
const llong inf = 1e18;
int n, m;
int U[50001], V[50001], C[50001];
bool used[201];
void dijkstra(vector<int> edge[], llong D[], int P[], int s) {
for (int i = 1; i <= n; ++i) D[i] = inf, used[i] = 0;
D[s] = 0;
while (1) {
int x = 0;
llong d = inf;
for (int i = 1; i <= n; ++i) {
if (!used[i] && D[i] < d) x = i, d = D[i];
}
if (!x) break;
used[x] = 1;
for (int it : edge[x]) {
llong nd = d + C[it];
int i = U[it] ^ V[it] ^ x;
if (nd < D[i]) {
D[i] = nd;
P[i] = it;
}
}
}
}
llong ans[50001];
llong dist1[201], dist2[201], dist3[201];
int prev1[201], prev2[201], prev3[201];
vector<int> pedge[201], redge[201], edge[201];
bool R[50001];
void solve(int a, int b) {
dijkstra(pedge, dist1, prev1, a);
ans[0] += dist1[b];
dijkstra(redge, dist2, prev2, b);
memset(R, 0, sizeof(R));
for (int i = 1; i <= n; ++i) {
if (i != a && dist1[i] < inf) R[prev1[i]] = 1;
if (i != b && dist2[i] < inf) R[prev2[i]] = 1;
}
for (int i = 1; i <= m; ++i) {
if (dist1[V[i]] >= inf || dist2[U[i]] >= inf) {
ans[i] += inf;
continue;
}
if (!R[i]) {
ans[i] += min(dist1[b], dist1[V[i]] + C[i] + dist2[U[i]]);
continue;
}
for (int j = 1; j <= n; ++j) edge[j].clear();
for (int j = 1; j <= m; ++j) {
edge[i != j ? U[j] : V[j]].push_back(j);
}
dijkstra(edge, dist3, prev3, a);
ans[i] += dist3[b];
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; ++i) {
cin >> U[i] >> V[i] >> C[i] >> ans[i];
pedge[U[i]].push_back(i);
redge[V[i]].push_back(i);
}
solve(1, n); solve(n, 1);
llong ret = inf;
for (int i = 0; i <= m; ++i) ret = min(ret, ans[i]);
printf("%lld\n", ret < inf ? ret : -1);
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
77 ms |
512 KB |
Output is correct |
4 |
Correct |
84 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
48 ms |
512 KB |
Output is correct |
11 |
Correct |
46 ms |
512 KB |
Output is correct |
12 |
Correct |
46 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
45 ms |
512 KB |
Output is correct |
15 |
Correct |
40 ms |
512 KB |
Output is correct |
16 |
Correct |
46 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
452 ms |
2296 KB |
Output is correct |
2 |
Correct |
462 ms |
2304 KB |
Output is correct |
3 |
Correct |
454 ms |
2424 KB |
Output is correct |
4 |
Correct |
86 ms |
512 KB |
Output is correct |
5 |
Correct |
59 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
0 ms |
384 KB |
Output is correct |
9 |
Correct |
27 ms |
3200 KB |
Output is correct |
10 |
Correct |
26 ms |
3200 KB |
Output is correct |
11 |
Correct |
248 ms |
3576 KB |
Output is correct |
12 |
Correct |
306 ms |
3448 KB |
Output is correct |
13 |
Correct |
244 ms |
3456 KB |
Output is correct |
14 |
Correct |
240 ms |
3704 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
512 KB |
Output is correct |
2 |
Correct |
6 ms |
384 KB |
Output is correct |
3 |
Correct |
375 ms |
1900 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
474 ms |
2968 KB |
Output is correct |
6 |
Correct |
0 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
22 ms |
2720 KB |
Output is correct |
9 |
Correct |
21 ms |
2680 KB |
Output is correct |
10 |
Correct |
254 ms |
2944 KB |
Output is correct |
11 |
Correct |
249 ms |
3064 KB |
Output is correct |
12 |
Correct |
252 ms |
2944 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
0 ms |
384 KB |
Output is correct |
16 |
Correct |
1 ms |
384 KB |
Output is correct |
17 |
Correct |
1 ms |
384 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
264 ms |
2944 KB |
Output is correct |
20 |
Correct |
260 ms |
2944 KB |
Output is correct |
21 |
Correct |
243 ms |
2936 KB |
Output is correct |
22 |
Correct |
245 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
512 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
77 ms |
512 KB |
Output is correct |
4 |
Correct |
84 ms |
512 KB |
Output is correct |
5 |
Correct |
3 ms |
512 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
640 KB |
Output is correct |
9 |
Correct |
1 ms |
512 KB |
Output is correct |
10 |
Correct |
48 ms |
512 KB |
Output is correct |
11 |
Correct |
46 ms |
512 KB |
Output is correct |
12 |
Correct |
46 ms |
512 KB |
Output is correct |
13 |
Correct |
2 ms |
512 KB |
Output is correct |
14 |
Correct |
45 ms |
512 KB |
Output is correct |
15 |
Correct |
40 ms |
512 KB |
Output is correct |
16 |
Correct |
46 ms |
640 KB |
Output is correct |
17 |
Correct |
452 ms |
2296 KB |
Output is correct |
18 |
Correct |
462 ms |
2304 KB |
Output is correct |
19 |
Correct |
454 ms |
2424 KB |
Output is correct |
20 |
Correct |
86 ms |
512 KB |
Output is correct |
21 |
Correct |
59 ms |
512 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
384 KB |
Output is correct |
24 |
Correct |
0 ms |
384 KB |
Output is correct |
25 |
Correct |
27 ms |
3200 KB |
Output is correct |
26 |
Correct |
26 ms |
3200 KB |
Output is correct |
27 |
Correct |
248 ms |
3576 KB |
Output is correct |
28 |
Correct |
306 ms |
3448 KB |
Output is correct |
29 |
Correct |
244 ms |
3456 KB |
Output is correct |
30 |
Correct |
240 ms |
3704 KB |
Output is correct |
31 |
Correct |
51 ms |
512 KB |
Output is correct |
32 |
Correct |
6 ms |
384 KB |
Output is correct |
33 |
Correct |
375 ms |
1900 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
474 ms |
2968 KB |
Output is correct |
36 |
Correct |
0 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
22 ms |
2720 KB |
Output is correct |
39 |
Correct |
21 ms |
2680 KB |
Output is correct |
40 |
Correct |
254 ms |
2944 KB |
Output is correct |
41 |
Correct |
249 ms |
3064 KB |
Output is correct |
42 |
Correct |
252 ms |
2944 KB |
Output is correct |
43 |
Correct |
0 ms |
384 KB |
Output is correct |
44 |
Correct |
0 ms |
384 KB |
Output is correct |
45 |
Correct |
0 ms |
384 KB |
Output is correct |
46 |
Correct |
1 ms |
384 KB |
Output is correct |
47 |
Correct |
1 ms |
384 KB |
Output is correct |
48 |
Correct |
0 ms |
384 KB |
Output is correct |
49 |
Correct |
264 ms |
2944 KB |
Output is correct |
50 |
Correct |
260 ms |
2944 KB |
Output is correct |
51 |
Correct |
243 ms |
2936 KB |
Output is correct |
52 |
Correct |
245 ms |
2944 KB |
Output is correct |
53 |
Correct |
450 ms |
3192 KB |
Output is correct |
54 |
Correct |
467 ms |
3288 KB |
Output is correct |
55 |
Correct |
468 ms |
3320 KB |
Output is correct |
56 |
Correct |
79 ms |
512 KB |
Output is correct |
57 |
Correct |
77 ms |
540 KB |
Output is correct |
58 |
Correct |
236 ms |
2836 KB |
Output is correct |
59 |
Correct |
230 ms |
2680 KB |
Output is correct |
60 |
Correct |
233 ms |
2728 KB |
Output is correct |
61 |
Correct |
232 ms |
2808 KB |
Output is correct |
62 |
Correct |
235 ms |
2808 KB |
Output is correct |
63 |
Correct |
233 ms |
2688 KB |
Output is correct |
64 |
Correct |
309 ms |
2808 KB |
Output is correct |
65 |
Correct |
291 ms |
2680 KB |
Output is correct |
66 |
Correct |
280 ms |
2688 KB |
Output is correct |
67 |
Correct |
16 ms |
2812 KB |
Output is correct |
68 |
Correct |
27 ms |
3200 KB |
Output is correct |
69 |
Correct |
25 ms |
3200 KB |
Output is correct |
70 |
Correct |
244 ms |
3448 KB |
Output is correct |
71 |
Correct |
246 ms |
3500 KB |
Output is correct |
72 |
Correct |
241 ms |
3448 KB |
Output is correct |
73 |
Correct |
242 ms |
3456 KB |
Output is correct |
74 |
Correct |
245 ms |
3448 KB |
Output is correct |