#include <bits/stdc++.h>
using namespace std;
const int MN = 203;
struct Edge {
int u,v,c,d;
};
set<pair<int,int>> adj[MN][MN];
int dist1[MN][MN], distN[MN][MN], dist12[MN][MN], distN2[MN][MN], p[MN], n;
vector<int> e1 = {-1},eN = {-1}, e12={-1}, eN2={-1};
void dijkstra (int *dist, int src) {
memset(dist,0x3f,sizeof(int) * MN); memset(p,-1,sizeof p);
dist[src] = 0;
p[src] = -1;
vector<bool> vis(n+1);
while (true) {
int cur = -1;
for (int i = 1; i <= n; i++) if (!vis[i] && dist[i] < 1e9 && (cur == -1 || dist[i] < dist[cur])) cur = i;
if (!~cur) break;
vis[cur] = 1;
for (int i = 1; i <= n; i++) if (!adj[cur][i].empty()) {
int att = dist[cur] + adj[cur][i].begin()->first;
if (att < dist[i]) {
dist[i] = att;
p[i] = adj[cur][i].begin()->second;
}
}
}
}
int main () {
int m;
scanf ("%d %d",&n,&m);
vector<Edge> edges(m);
for (int i = 0; i < m; i++) {
Edge &e = edges[i];
scanf ("%d %d %d %d",&e.u,&e.v,&e.c,&e.d);
adj[e.u][e.v].emplace(e.c,i);
}
dijkstra(dist1[0],1);
for (int i = 1; i <= n; i++) if (~p[i]) e1.push_back(p[i]);
for (int i = 1; i < (int)e1.size(); i++) {
Edge &e = edges[e1[i]];
adj[e.u][e.v].erase({e.c,e1[i]});
dijkstra(dist1[i],1);
adj[e.u][e.v].emplace(e.c,e1[i]);
}
dijkstra(distN2[0],n);
for (int i = 1; i <= n; i++) if (~p[i]) eN2.push_back(p[i]);
for (int i = 1; i < (int)eN2.size(); i++) {
Edge &e = edges[eN2[i]];
adj[e.u][e.v].erase({e.c,eN2[i]});
dijkstra(distN2[i],n);
adj[e.u][e.v].emplace(e.c,eN2[i]);
}
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
adj[i][j].clear();
for (int i = 0; i < m; i++) {
Edge &e = edges[i];
adj[e.v][e.u].emplace(e.c,i);
}
dijkstra(distN[0],n);
for (int i = 1; i <= n; i++) if (~p[i]) eN.push_back(p[i]);
for (int i = 1; i < (int)eN.size(); i++) {
Edge &e = edges[eN[i]];
adj[e.v][e.u].erase({e.c,eN[i]});
dijkstra(distN[i],n);
adj[e.v][e.u].emplace(e.c,eN[i]);
}
dijkstra(dist12[0],1);
for (int i = 1; i <= n; i++) if (~p[i]) e12.push_back(p[i]);
for (int i = 1; i < (int)e12.size(); i++) {
Edge &e = edges[e12[i]];
adj[e.v][e.u].erase({e.c,e12[i]});
dijkstra(dist12[i],1);
adj[e.v][e.u].emplace(e.c,e12[i]);
}
int ret = dist1[0][n] + distN2[0][1];
if (ret > 1e9) ret = 2e9;
for (int i = 0; i < m; i++) {
//want: dist(1,e.v) without edge i + dist(e.u,n) without edge i + e.d
Edge &e = edges[i];
int A = dist1[0][e.v], B = distN[0][e.u], C = distN2[0][e.v], D = dist12[0][e.u], E = dist1[0][n], F = distN2[0][1];
for (int j = 1; j < (int)e1.size(); j++) if (e1[j] == i) {
A = dist1[j][e.v];
E = dist1[j][n];
}
for (int j = 1; j < (int)eN.size(); j++) if (eN[j] == i) {
B = distN[j][e.u];
}
for (int j = 1; j < (int)eN2.size(); j++) if (eN2[j] == i) {
C = distN2[j][e.v];
F = distN2[j][1];
}
for (int j = 1; j < (int)e12.size(); j++) if (e12[j] == i) {
D = dist12[j][e.u];
}
int G = min(A + B + e.c, E), H = min(C + D + e.c, F);
if (G >= 1e9 || H >= 1e9) continue;
ret = min(ret,G + H + e.d);
}
printf ("%d\n",ret > 15e8 ? -1 : ret);
return 0;
}
//only O(N) "special" edges both ways, do dijkstra without each
//O(N^3) total
Compilation message
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:31:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
31 | scanf ("%d %d",&n,&m);
| ~~~~~~^~~~~~~~~~~~~~~
ho_t4.cpp:35:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
35 | scanf ("%d %d %d %d",&e.u,&e.v,&e.c,&e.d);
| ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
2924 KB |
Output is correct |
2 |
Correct |
7 ms |
2412 KB |
Output is correct |
3 |
Correct |
168 ms |
2924 KB |
Output is correct |
4 |
Correct |
160 ms |
2924 KB |
Output is correct |
5 |
Correct |
4 ms |
2412 KB |
Output is correct |
6 |
Correct |
13 ms |
2412 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
2 ms |
2304 KB |
Output is correct |
9 |
Correct |
3 ms |
2412 KB |
Output is correct |
10 |
Correct |
164 ms |
3180 KB |
Output is correct |
11 |
Correct |
161 ms |
2944 KB |
Output is correct |
12 |
Correct |
159 ms |
2924 KB |
Output is correct |
13 |
Correct |
36 ms |
2668 KB |
Output is correct |
14 |
Correct |
102 ms |
2796 KB |
Output is correct |
15 |
Correct |
100 ms |
2796 KB |
Output is correct |
16 |
Correct |
103 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
418 ms |
7020 KB |
Output is correct |
2 |
Correct |
427 ms |
7040 KB |
Output is correct |
3 |
Correct |
420 ms |
7248 KB |
Output is correct |
4 |
Correct |
159 ms |
3084 KB |
Output is correct |
5 |
Correct |
127 ms |
2924 KB |
Output is correct |
6 |
Correct |
32 ms |
2540 KB |
Output is correct |
7 |
Correct |
3 ms |
2284 KB |
Output is correct |
8 |
Correct |
2 ms |
2284 KB |
Output is correct |
9 |
Correct |
124 ms |
6892 KB |
Output is correct |
10 |
Correct |
144 ms |
6892 KB |
Output is correct |
11 |
Correct |
248 ms |
7020 KB |
Output is correct |
12 |
Correct |
264 ms |
7020 KB |
Output is correct |
13 |
Correct |
241 ms |
7020 KB |
Output is correct |
14 |
Correct |
243 ms |
7020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
142 ms |
2924 KB |
Output is correct |
2 |
Correct |
34 ms |
2540 KB |
Output is correct |
3 |
Correct |
345 ms |
6116 KB |
Output is correct |
4 |
Correct |
14 ms |
2412 KB |
Output is correct |
5 |
Correct |
348 ms |
7020 KB |
Output is correct |
6 |
Correct |
2 ms |
2284 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
116 ms |
6764 KB |
Output is correct |
9 |
Correct |
119 ms |
6636 KB |
Output is correct |
10 |
Correct |
201 ms |
6764 KB |
Output is correct |
11 |
Correct |
207 ms |
6764 KB |
Output is correct |
12 |
Correct |
201 ms |
6764 KB |
Output is correct |
13 |
Correct |
2 ms |
2284 KB |
Output is correct |
14 |
Correct |
2 ms |
2284 KB |
Output is correct |
15 |
Correct |
2 ms |
2284 KB |
Output is correct |
16 |
Correct |
2 ms |
2284 KB |
Output is correct |
17 |
Correct |
2 ms |
2284 KB |
Output is correct |
18 |
Correct |
2 ms |
2284 KB |
Output is correct |
19 |
Correct |
204 ms |
6764 KB |
Output is correct |
20 |
Correct |
204 ms |
6892 KB |
Output is correct |
21 |
Correct |
209 ms |
6892 KB |
Output is correct |
22 |
Correct |
204 ms |
6892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
149 ms |
2924 KB |
Output is correct |
2 |
Correct |
7 ms |
2412 KB |
Output is correct |
3 |
Correct |
168 ms |
2924 KB |
Output is correct |
4 |
Correct |
160 ms |
2924 KB |
Output is correct |
5 |
Correct |
4 ms |
2412 KB |
Output is correct |
6 |
Correct |
13 ms |
2412 KB |
Output is correct |
7 |
Correct |
2 ms |
2284 KB |
Output is correct |
8 |
Correct |
2 ms |
2304 KB |
Output is correct |
9 |
Correct |
3 ms |
2412 KB |
Output is correct |
10 |
Correct |
164 ms |
3180 KB |
Output is correct |
11 |
Correct |
161 ms |
2944 KB |
Output is correct |
12 |
Correct |
159 ms |
2924 KB |
Output is correct |
13 |
Correct |
36 ms |
2668 KB |
Output is correct |
14 |
Correct |
102 ms |
2796 KB |
Output is correct |
15 |
Correct |
100 ms |
2796 KB |
Output is correct |
16 |
Correct |
103 ms |
2796 KB |
Output is correct |
17 |
Correct |
418 ms |
7020 KB |
Output is correct |
18 |
Correct |
427 ms |
7040 KB |
Output is correct |
19 |
Correct |
420 ms |
7248 KB |
Output is correct |
20 |
Correct |
159 ms |
3084 KB |
Output is correct |
21 |
Correct |
127 ms |
2924 KB |
Output is correct |
22 |
Correct |
32 ms |
2540 KB |
Output is correct |
23 |
Correct |
3 ms |
2284 KB |
Output is correct |
24 |
Correct |
2 ms |
2284 KB |
Output is correct |
25 |
Correct |
124 ms |
6892 KB |
Output is correct |
26 |
Correct |
144 ms |
6892 KB |
Output is correct |
27 |
Correct |
248 ms |
7020 KB |
Output is correct |
28 |
Correct |
264 ms |
7020 KB |
Output is correct |
29 |
Correct |
241 ms |
7020 KB |
Output is correct |
30 |
Correct |
243 ms |
7020 KB |
Output is correct |
31 |
Correct |
142 ms |
2924 KB |
Output is correct |
32 |
Correct |
34 ms |
2540 KB |
Output is correct |
33 |
Correct |
345 ms |
6116 KB |
Output is correct |
34 |
Correct |
14 ms |
2412 KB |
Output is correct |
35 |
Correct |
348 ms |
7020 KB |
Output is correct |
36 |
Correct |
2 ms |
2284 KB |
Output is correct |
37 |
Correct |
2 ms |
2284 KB |
Output is correct |
38 |
Correct |
116 ms |
6764 KB |
Output is correct |
39 |
Correct |
119 ms |
6636 KB |
Output is correct |
40 |
Correct |
201 ms |
6764 KB |
Output is correct |
41 |
Correct |
207 ms |
6764 KB |
Output is correct |
42 |
Correct |
201 ms |
6764 KB |
Output is correct |
43 |
Correct |
2 ms |
2284 KB |
Output is correct |
44 |
Correct |
2 ms |
2284 KB |
Output is correct |
45 |
Correct |
2 ms |
2284 KB |
Output is correct |
46 |
Correct |
2 ms |
2284 KB |
Output is correct |
47 |
Correct |
2 ms |
2284 KB |
Output is correct |
48 |
Correct |
2 ms |
2284 KB |
Output is correct |
49 |
Correct |
204 ms |
6764 KB |
Output is correct |
50 |
Correct |
204 ms |
6892 KB |
Output is correct |
51 |
Correct |
209 ms |
6892 KB |
Output is correct |
52 |
Correct |
204 ms |
6892 KB |
Output is correct |
53 |
Correct |
367 ms |
7148 KB |
Output is correct |
54 |
Correct |
388 ms |
7020 KB |
Output is correct |
55 |
Correct |
402 ms |
7148 KB |
Output is correct |
56 |
Correct |
164 ms |
2924 KB |
Output is correct |
57 |
Correct |
164 ms |
2924 KB |
Output is correct |
58 |
Correct |
369 ms |
6252 KB |
Output is correct |
59 |
Correct |
366 ms |
6252 KB |
Output is correct |
60 |
Correct |
357 ms |
6252 KB |
Output is correct |
61 |
Correct |
365 ms |
6252 KB |
Output is correct |
62 |
Correct |
356 ms |
6252 KB |
Output is correct |
63 |
Correct |
365 ms |
6252 KB |
Output is correct |
64 |
Correct |
287 ms |
6252 KB |
Output is correct |
65 |
Correct |
280 ms |
6252 KB |
Output is correct |
66 |
Correct |
249 ms |
6252 KB |
Output is correct |
67 |
Correct |
54 ms |
5612 KB |
Output is correct |
68 |
Correct |
130 ms |
6892 KB |
Output is correct |
69 |
Correct |
125 ms |
6892 KB |
Output is correct |
70 |
Correct |
228 ms |
7020 KB |
Output is correct |
71 |
Correct |
229 ms |
7020 KB |
Output is correct |
72 |
Correct |
227 ms |
7020 KB |
Output is correct |
73 |
Correct |
230 ms |
7020 KB |
Output is correct |
74 |
Correct |
233 ms |
7020 KB |
Output is correct |