#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
constexpr ll INF = 1e18;
int n, m, U[50050], V[50050], C[50050], D[50050];
pair<ll, int> adj[202][202];
ll d1[202], d2[202], d3[202], d4[202], dist1[202], dist2[202];
int visited[202], chk[50050], par[202];
void dijkstra(int s, ll dist[], int *chk = 0, int e = 0){
fill(dist+1, dist+n+1, INF);
fill(visited+1, visited+n+1, 0);
dist[s] = 0;
for (int i=1;i<=n;i++){
ll mn = INF + 1, v = -1;
for (int j=1;j<=n;j++) if (!visited[j] && dist[j] < mn){
mn = dist[j];
v = j;
}
assert(v!=-1);
visited[v] = 1;
for (int j=1;j<=n;j++) if (!visited[j] && dist[j] > dist[v] + adj[v][j].first){
dist[j] = dist[v] + adj[v][j].first;
par[j] = adj[v][j].second;
}
}
if (chk && dist[e] < INF){
for (int i=e;i!=s;i=U[par[i]]){
chk[par[i]] = 1;
}
}
}
ll init(){
for (int i=1;i<=n;i++) fill(adj[i]+1, adj[i]+n+1, pair<ll, int>(INF, -1));
for (int i=1;i<=m;i++){
adj[U[i]][V[i]] = min(adj[U[i]][V[i]], pair<ll, int>(C[i], i));
}
dijkstra(1, d1, chk, n);
dijkstra(n, d3, chk, 1);
for (int i=1;i<=n;i++) fill(adj[i]+1, adj[i]+n+1, pair<ll, int>(INF, -1));
for (int i=1;i<=m;i++){
adj[V[i]][U[i]] = min(adj[V[i]][U[i]], pair<ll, int>(C[i], i));
}
dijkstra(n, d2);
dijkstra(1, d4);
return d1[n] + d3[1];
}
ll solve(int idx){
for (int i=1;i<=n;i++) fill(adj[i]+1, adj[i]+n+1, pair<ll, int>(INF, -1));
for (int i=1;i<=m;i++){
if (i!=idx) adj[U[i]][V[i]] = min(adj[U[i]][V[i]], pair<ll, int>(C[i], i));
else adj[V[i]][U[i]] = min(adj[V[i]][U[i]], pair<ll, int>(C[i], i));
}
dijkstra(1, dist1);
dijkstra(n, dist2);
return dist1[n] + dist2[1];
}
int main(){
scanf("%d %d", &n, &m);
for (int i=1;i<=m;i++) scanf("%d %d %d %d", U+i, V+i, C+i, D+i);
ll ans = init();
for (int i=1;i<=m;i++){
if (chk[i]) ans = min(ans, solve(i) + D[i]);
else ans = min(ans, min(d1[n], d1[V[i]] + C[i] + d2[U[i]]) + min(d3[1], d3[V[i]] + C[i] + d4[U[i]]) + D[i]);
}
printf("%lld\n", ans>=INF?-1:ans);
return 0;
}
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
ho_t4.cpp:73:33: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | for (int i=1;i<=m;i++) scanf("%d %d %d %d", U+i, V+i, C+i, D+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
6 ms |
980 KB |
Output is correct |
4 |
Correct |
8 ms |
1016 KB |
Output is correct |
5 |
Correct |
1 ms |
476 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
97 ms |
988 KB |
Output is correct |
11 |
Correct |
117 ms |
988 KB |
Output is correct |
12 |
Correct |
111 ms |
992 KB |
Output is correct |
13 |
Correct |
2 ms |
988 KB |
Output is correct |
14 |
Correct |
6 ms |
1016 KB |
Output is correct |
15 |
Correct |
4 ms |
980 KB |
Output is correct |
16 |
Correct |
6 ms |
964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
2636 KB |
Output is correct |
2 |
Correct |
25 ms |
2680 KB |
Output is correct |
3 |
Correct |
26 ms |
2800 KB |
Output is correct |
4 |
Correct |
5 ms |
968 KB |
Output is correct |
5 |
Correct |
8 ms |
1008 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
952 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
21 ms |
2872 KB |
Output is correct |
10 |
Correct |
20 ms |
2792 KB |
Output is correct |
11 |
Correct |
29 ms |
3016 KB |
Output is correct |
12 |
Correct |
26 ms |
2800 KB |
Output is correct |
13 |
Correct |
26 ms |
2892 KB |
Output is correct |
14 |
Correct |
30 ms |
2968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
984 KB |
Output is correct |
2 |
Correct |
3 ms |
980 KB |
Output is correct |
3 |
Correct |
17 ms |
2256 KB |
Output is correct |
4 |
Correct |
2 ms |
980 KB |
Output is correct |
5 |
Correct |
21 ms |
2560 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
18 ms |
2564 KB |
Output is correct |
9 |
Correct |
18 ms |
2672 KB |
Output is correct |
10 |
Correct |
21 ms |
2564 KB |
Output is correct |
11 |
Correct |
20 ms |
2552 KB |
Output is correct |
12 |
Correct |
20 ms |
2636 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
312 KB |
Output is correct |
19 |
Correct |
20 ms |
2624 KB |
Output is correct |
20 |
Correct |
20 ms |
2636 KB |
Output is correct |
21 |
Correct |
21 ms |
2644 KB |
Output is correct |
22 |
Correct |
20 ms |
2636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
980 KB |
Output is correct |
2 |
Correct |
1 ms |
980 KB |
Output is correct |
3 |
Correct |
6 ms |
980 KB |
Output is correct |
4 |
Correct |
8 ms |
1016 KB |
Output is correct |
5 |
Correct |
1 ms |
476 KB |
Output is correct |
6 |
Correct |
1 ms |
980 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
97 ms |
988 KB |
Output is correct |
11 |
Correct |
117 ms |
988 KB |
Output is correct |
12 |
Correct |
111 ms |
992 KB |
Output is correct |
13 |
Correct |
2 ms |
988 KB |
Output is correct |
14 |
Correct |
6 ms |
1016 KB |
Output is correct |
15 |
Correct |
4 ms |
980 KB |
Output is correct |
16 |
Correct |
6 ms |
964 KB |
Output is correct |
17 |
Correct |
25 ms |
2636 KB |
Output is correct |
18 |
Correct |
25 ms |
2680 KB |
Output is correct |
19 |
Correct |
26 ms |
2800 KB |
Output is correct |
20 |
Correct |
5 ms |
968 KB |
Output is correct |
21 |
Correct |
8 ms |
1008 KB |
Output is correct |
22 |
Correct |
1 ms |
980 KB |
Output is correct |
23 |
Correct |
1 ms |
952 KB |
Output is correct |
24 |
Correct |
0 ms |
340 KB |
Output is correct |
25 |
Correct |
21 ms |
2872 KB |
Output is correct |
26 |
Correct |
20 ms |
2792 KB |
Output is correct |
27 |
Correct |
29 ms |
3016 KB |
Output is correct |
28 |
Correct |
26 ms |
2800 KB |
Output is correct |
29 |
Correct |
26 ms |
2892 KB |
Output is correct |
30 |
Correct |
30 ms |
2968 KB |
Output is correct |
31 |
Correct |
5 ms |
984 KB |
Output is correct |
32 |
Correct |
3 ms |
980 KB |
Output is correct |
33 |
Correct |
17 ms |
2256 KB |
Output is correct |
34 |
Correct |
2 ms |
980 KB |
Output is correct |
35 |
Correct |
21 ms |
2560 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
18 ms |
2564 KB |
Output is correct |
39 |
Correct |
18 ms |
2672 KB |
Output is correct |
40 |
Correct |
21 ms |
2564 KB |
Output is correct |
41 |
Correct |
20 ms |
2552 KB |
Output is correct |
42 |
Correct |
20 ms |
2636 KB |
Output is correct |
43 |
Correct |
0 ms |
340 KB |
Output is correct |
44 |
Correct |
1 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 |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
312 KB |
Output is correct |
49 |
Correct |
20 ms |
2624 KB |
Output is correct |
50 |
Correct |
20 ms |
2636 KB |
Output is correct |
51 |
Correct |
21 ms |
2644 KB |
Output is correct |
52 |
Correct |
20 ms |
2636 KB |
Output is correct |
53 |
Correct |
26 ms |
2756 KB |
Output is correct |
54 |
Correct |
31 ms |
2716 KB |
Output is correct |
55 |
Correct |
29 ms |
2596 KB |
Output is correct |
56 |
Correct |
6 ms |
980 KB |
Output is correct |
57 |
Correct |
5 ms |
980 KB |
Output is correct |
58 |
Correct |
169 ms |
2516 KB |
Output is correct |
59 |
Correct |
193 ms |
2548 KB |
Output is correct |
60 |
Correct |
256 ms |
2544 KB |
Output is correct |
61 |
Correct |
166 ms |
2552 KB |
Output is correct |
62 |
Correct |
190 ms |
2576 KB |
Output is correct |
63 |
Correct |
252 ms |
2540 KB |
Output is correct |
64 |
Correct |
174 ms |
2540 KB |
Output is correct |
65 |
Correct |
191 ms |
2500 KB |
Output is correct |
66 |
Correct |
255 ms |
2620 KB |
Output is correct |
67 |
Correct |
15 ms |
1752 KB |
Output is correct |
68 |
Correct |
20 ms |
2880 KB |
Output is correct |
69 |
Correct |
20 ms |
2772 KB |
Output is correct |
70 |
Correct |
31 ms |
2908 KB |
Output is correct |
71 |
Correct |
28 ms |
2904 KB |
Output is correct |
72 |
Correct |
26 ms |
2820 KB |
Output is correct |
73 |
Correct |
27 ms |
2856 KB |
Output is correct |
74 |
Correct |
29 ms |
2912 KB |
Output is correct |