#include <bits/stdc++.h>
#define long long long
#define pii pair<int, int>
#define x first
#define y second
using namespace std;
const int N = 205;
const int M = 5e4+5;
int n, m;
vector<pii> g[N];
array<int, 4> E[M];
long dp[N];
priority_queue<pii, vector<pii>, greater<pii> > Q;
long dijkstra(int s, int e, int i) {
fill_n(dp, N, 1e18);
Q.emplace(dp[s] = 0, s);
while(!Q.empty()) {
pii u = Q.top(); Q.pop();
if(dp[u.y] != u.x) continue;
for(pii v : g[u.y]) {
if(v.y == i) continue;
if(u.x + E[v.y][2] < dp[v.x])
Q.emplace(dp[v.x] = u.x + E[v.y][2], v.x);
}
if(u.y == E[i][1] && u.x + E[i][2] < dp[E[i][0]])
Q.emplace(dp[E[i][0]] = u.x + E[i][2], E[i][0]);
}
return dp[e];
}
long ap[N][N];
int main() {
fill_n(ap[0], N * N, 1e18);
scanf("%d %d", &n, &m);
for(int i = 1; i <= n; i++) ap[i][i] = 0;
for(int i = 1, a, b, c, d; i <= m; i++) {
scanf("%d %d %d %d", &a, &b, &c, &d);
E[i] = {a, b, c, d};
g[a].emplace_back(b, i);
ap[a][b] = min(ap[a][b], (long)c);
}
for(int k = 1; k <= n; k++) for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++)
ap[i][j] = min(ap[i][j], ap[i][k] + ap[k][j]);
long ans = ap[1][n] + ap[n][1];
for(int i = 1; i <= m; i++) {
long d1 = min(ap[1][n], ap[1][E[i][1]] + E[i][2] + ap[E[i][0]][n]);
long d2 = min(ap[n][1], ap[n][E[i][1]] + E[i][2] + ap[E[i][0]][1]);
if(d1 + d2 + E[i][3] < ans)
ans = min(ans, dijkstra(1, n, i) + dijkstra(n, 1, i) + E[i][3]);
}
if(ans >= 1e18) printf("-1\n");
else printf("%lld\n", ans);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:42:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:45:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d", &a, &b, &c, &d);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
640 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
13 ms |
768 KB |
Output is correct |
4 |
Correct |
14 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
13 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
17 ms |
512 KB |
Output is correct |
11 |
Correct |
27 ms |
640 KB |
Output is correct |
12 |
Correct |
16 ms |
640 KB |
Output is correct |
13 |
Correct |
14 ms |
640 KB |
Output is correct |
14 |
Correct |
14 ms |
640 KB |
Output is correct |
15 |
Correct |
14 ms |
640 KB |
Output is correct |
16 |
Correct |
14 ms |
688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
2168 KB |
Output is correct |
2 |
Correct |
38 ms |
2048 KB |
Output is correct |
3 |
Correct |
42 ms |
2040 KB |
Output is correct |
4 |
Correct |
14 ms |
768 KB |
Output is correct |
5 |
Correct |
14 ms |
640 KB |
Output is correct |
6 |
Correct |
14 ms |
640 KB |
Output is correct |
7 |
Correct |
14 ms |
640 KB |
Output is correct |
8 |
Correct |
5 ms |
640 KB |
Output is correct |
9 |
Correct |
44 ms |
2040 KB |
Output is correct |
10 |
Correct |
45 ms |
2168 KB |
Output is correct |
11 |
Correct |
40 ms |
2048 KB |
Output is correct |
12 |
Correct |
41 ms |
2048 KB |
Output is correct |
13 |
Correct |
41 ms |
2040 KB |
Output is correct |
14 |
Correct |
41 ms |
2168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
640 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
32 ms |
1664 KB |
Output is correct |
4 |
Correct |
15 ms |
768 KB |
Output is correct |
5 |
Correct |
38 ms |
2936 KB |
Output is correct |
6 |
Correct |
5 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
41 ms |
2936 KB |
Output is correct |
9 |
Correct |
42 ms |
2936 KB |
Output is correct |
10 |
Correct |
38 ms |
2944 KB |
Output is correct |
11 |
Correct |
37 ms |
2944 KB |
Output is correct |
12 |
Correct |
38 ms |
3072 KB |
Output is correct |
13 |
Correct |
5 ms |
640 KB |
Output is correct |
14 |
Correct |
5 ms |
640 KB |
Output is correct |
15 |
Correct |
5 ms |
640 KB |
Output is correct |
16 |
Correct |
5 ms |
640 KB |
Output is correct |
17 |
Correct |
5 ms |
640 KB |
Output is correct |
18 |
Correct |
5 ms |
640 KB |
Output is correct |
19 |
Correct |
39 ms |
2936 KB |
Output is correct |
20 |
Correct |
37 ms |
2944 KB |
Output is correct |
21 |
Correct |
38 ms |
2936 KB |
Output is correct |
22 |
Correct |
37 ms |
2944 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
640 KB |
Output is correct |
2 |
Correct |
13 ms |
640 KB |
Output is correct |
3 |
Correct |
13 ms |
768 KB |
Output is correct |
4 |
Correct |
14 ms |
640 KB |
Output is correct |
5 |
Correct |
5 ms |
640 KB |
Output is correct |
6 |
Correct |
13 ms |
640 KB |
Output is correct |
7 |
Correct |
5 ms |
640 KB |
Output is correct |
8 |
Correct |
4 ms |
640 KB |
Output is correct |
9 |
Correct |
5 ms |
768 KB |
Output is correct |
10 |
Correct |
17 ms |
512 KB |
Output is correct |
11 |
Correct |
27 ms |
640 KB |
Output is correct |
12 |
Correct |
16 ms |
640 KB |
Output is correct |
13 |
Correct |
14 ms |
640 KB |
Output is correct |
14 |
Correct |
14 ms |
640 KB |
Output is correct |
15 |
Correct |
14 ms |
640 KB |
Output is correct |
16 |
Correct |
14 ms |
688 KB |
Output is correct |
17 |
Correct |
46 ms |
2168 KB |
Output is correct |
18 |
Correct |
38 ms |
2048 KB |
Output is correct |
19 |
Correct |
42 ms |
2040 KB |
Output is correct |
20 |
Correct |
14 ms |
768 KB |
Output is correct |
21 |
Correct |
14 ms |
640 KB |
Output is correct |
22 |
Correct |
14 ms |
640 KB |
Output is correct |
23 |
Correct |
14 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
25 |
Correct |
44 ms |
2040 KB |
Output is correct |
26 |
Correct |
45 ms |
2168 KB |
Output is correct |
27 |
Correct |
40 ms |
2048 KB |
Output is correct |
28 |
Correct |
41 ms |
2048 KB |
Output is correct |
29 |
Correct |
41 ms |
2040 KB |
Output is correct |
30 |
Correct |
41 ms |
2168 KB |
Output is correct |
31 |
Correct |
14 ms |
640 KB |
Output is correct |
32 |
Correct |
13 ms |
640 KB |
Output is correct |
33 |
Correct |
32 ms |
1664 KB |
Output is correct |
34 |
Correct |
15 ms |
768 KB |
Output is correct |
35 |
Correct |
38 ms |
2936 KB |
Output is correct |
36 |
Correct |
5 ms |
640 KB |
Output is correct |
37 |
Correct |
5 ms |
640 KB |
Output is correct |
38 |
Correct |
41 ms |
2936 KB |
Output is correct |
39 |
Correct |
42 ms |
2936 KB |
Output is correct |
40 |
Correct |
38 ms |
2944 KB |
Output is correct |
41 |
Correct |
37 ms |
2944 KB |
Output is correct |
42 |
Correct |
38 ms |
3072 KB |
Output is correct |
43 |
Correct |
5 ms |
640 KB |
Output is correct |
44 |
Correct |
5 ms |
640 KB |
Output is correct |
45 |
Correct |
5 ms |
640 KB |
Output is correct |
46 |
Correct |
5 ms |
640 KB |
Output is correct |
47 |
Correct |
5 ms |
640 KB |
Output is correct |
48 |
Correct |
5 ms |
640 KB |
Output is correct |
49 |
Correct |
39 ms |
2936 KB |
Output is correct |
50 |
Correct |
37 ms |
2944 KB |
Output is correct |
51 |
Correct |
38 ms |
2936 KB |
Output is correct |
52 |
Correct |
37 ms |
2944 KB |
Output is correct |
53 |
Correct |
38 ms |
3072 KB |
Output is correct |
54 |
Correct |
38 ms |
3064 KB |
Output is correct |
55 |
Correct |
40 ms |
2936 KB |
Output is correct |
56 |
Correct |
14 ms |
768 KB |
Output is correct |
57 |
Correct |
14 ms |
768 KB |
Output is correct |
58 |
Correct |
36 ms |
2560 KB |
Output is correct |
59 |
Correct |
36 ms |
2560 KB |
Output is correct |
60 |
Correct |
36 ms |
2552 KB |
Output is correct |
61 |
Correct |
35 ms |
2560 KB |
Output is correct |
62 |
Correct |
65 ms |
2552 KB |
Output is correct |
63 |
Correct |
115 ms |
2680 KB |
Output is correct |
64 |
Correct |
88 ms |
2812 KB |
Output is correct |
65 |
Correct |
199 ms |
2816 KB |
Output is correct |
66 |
Correct |
358 ms |
2936 KB |
Output is correct |
67 |
Correct |
25 ms |
2684 KB |
Output is correct |
68 |
Correct |
44 ms |
3192 KB |
Output is correct |
69 |
Correct |
43 ms |
3192 KB |
Output is correct |
70 |
Correct |
41 ms |
3192 KB |
Output is correct |
71 |
Correct |
41 ms |
3200 KB |
Output is correct |
72 |
Correct |
41 ms |
3200 KB |
Output is correct |
73 |
Correct |
41 ms |
3320 KB |
Output is correct |
74 |
Correct |
41 ms |
3192 KB |
Output is correct |