// #pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
// #define int long long
#define sp << ' ' <<
#define nl << '\n'
const int INF = 2e9+1;
int n, m, ans = INF;
vector<array<int, 3>> g[2][200];
inline int add(int x, int y, int z){
if(max(x, max(y, z)) == INF) return INF;
return x + y + z;
}
inline int add(int x, int y){
if(max(x, y) == INF) return INF;
return x + y;
}
void dijkstra(int r, int s, vector<int> &d, int i){
d.assign(n, INF);
bitset<200> vis; if(r>=0) vis[r] = 1;
d[s] = 0;
if(r == s) return;
for(int _=0; _<n; ++_){
array<int, 2> j = {INF, -1};
for(int u=0; u<n; ++u) if(!vis[u] && d[u] < j[0]) j = {d[u], u};
if(j[1] < 0 || j[1] == r) continue;
vis[j[1]] = 1;
for(auto &[v, w, t] : g[i][j[1]])
if(v != r && !vis[v]) d[v] = min(d[v], add(j[0], w));
}
}
signed main(){
cin.tie(0)->sync_with_stdio(0);
cin >> n >> m;
while(m--){
int u, v, w, d; cin >> u >> v >> w >> d;
--u, --v;
g[0][u].push_back({v, w, d});
g[1][v].push_back({u, w, d});
}
for(int u=0; u<n; ++u){
vector<int> d[2], e[2];
for(int i=0; i<2; ++i){
dijkstra(u, i ? n-1 : 0, d[i], 0);
dijkstra(u, i ? n-1 : 0, e[i], 1);
}
for(auto &[v, w, x] : g[1][u])
for(int j=0; j<2; ++j)
d[j][u] = min(d[j][u], add(w, d[j][v]));
array<int, 2> x[2], y[2];
for(int j=0; j<2; ++j) x[j] = y[j] = {INF, -1};
for(int i=0; i<(int)g[0][u].size(); ++i){
for(int j=0; j<2; ++j){
y[j] = min(y[j], {add(e[j][g[0][u][i][0]], g[0][u][i][1]), i});
if(x[j] > y[j]) swap(x[j], y[j]);
}
}
if(u == 0) x[0][0] = y[0][0] = 0;
if(u==n-1) x[1][0] = y[1][0] = 0;
for(int i=0; i<(int)g[0][u].size(); ++i){
auto &[v, w, t] = g[0][u][i];
array<int, 2> uE;
for(int j=0; j<2; ++j) uE[j] = x[j][1] == i ? y[j][0] : x[j][0];
int a = add(d[0][v], w, uE[1]);
int b = add(d[1][v], w, uE[0]);
ans = min(ans, add(a, t, min(e[0][n-1], add(d[1][u], uE[0]))));
ans = min(ans, add(b, t, min(e[1][0], add(uE[1], d[0][u]))));
ans = min(ans, add(a, b, t));
}
}
vector<int> d[2];
dijkstra(-1, 0, d[0], 0);
dijkstra(-1, n-1, d[1], 0);
ans = min(ans, add(d[0][n-1], d[1][0]));
cout << (ans == INF ? -1 : ans) nl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
460 KB |
Output is correct |
2 |
Correct |
60 ms |
332 KB |
Output is correct |
3 |
Correct |
113 ms |
352 KB |
Output is correct |
4 |
Correct |
114 ms |
356 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
73 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
111 ms |
352 KB |
Output is correct |
11 |
Correct |
111 ms |
332 KB |
Output is correct |
12 |
Correct |
110 ms |
352 KB |
Output is correct |
13 |
Correct |
91 ms |
332 KB |
Output is correct |
14 |
Correct |
106 ms |
368 KB |
Output is correct |
15 |
Correct |
104 ms |
352 KB |
Output is correct |
16 |
Correct |
103 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
302 ms |
2212 KB |
Output is correct |
2 |
Correct |
298 ms |
2332 KB |
Output is correct |
3 |
Correct |
304 ms |
2296 KB |
Output is correct |
4 |
Correct |
118 ms |
332 KB |
Output is correct |
5 |
Correct |
112 ms |
332 KB |
Output is correct |
6 |
Correct |
73 ms |
332 KB |
Output is correct |
7 |
Correct |
60 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
168 ms |
2252 KB |
Output is correct |
10 |
Correct |
169 ms |
2236 KB |
Output is correct |
11 |
Correct |
264 ms |
2348 KB |
Output is correct |
12 |
Correct |
248 ms |
2288 KB |
Output is correct |
13 |
Correct |
253 ms |
2372 KB |
Output is correct |
14 |
Correct |
252 ms |
2512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
110 ms |
332 KB |
Output is correct |
2 |
Correct |
77 ms |
332 KB |
Output is correct |
3 |
Correct |
243 ms |
1828 KB |
Output is correct |
4 |
Correct |
75 ms |
332 KB |
Output is correct |
5 |
Correct |
294 ms |
2252 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
168 ms |
2240 KB |
Output is correct |
9 |
Correct |
164 ms |
2204 KB |
Output is correct |
10 |
Correct |
259 ms |
2252 KB |
Output is correct |
11 |
Correct |
256 ms |
2296 KB |
Output is correct |
12 |
Correct |
255 ms |
2320 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
0 ms |
204 KB |
Output is correct |
16 |
Correct |
0 ms |
204 KB |
Output is correct |
17 |
Correct |
0 ms |
204 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
259 ms |
2308 KB |
Output is correct |
20 |
Correct |
256 ms |
2300 KB |
Output is correct |
21 |
Correct |
253 ms |
2176 KB |
Output is correct |
22 |
Correct |
258 ms |
2328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
460 KB |
Output is correct |
2 |
Correct |
60 ms |
332 KB |
Output is correct |
3 |
Correct |
113 ms |
352 KB |
Output is correct |
4 |
Correct |
114 ms |
356 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
73 ms |
332 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
111 ms |
352 KB |
Output is correct |
11 |
Correct |
111 ms |
332 KB |
Output is correct |
12 |
Correct |
110 ms |
352 KB |
Output is correct |
13 |
Correct |
91 ms |
332 KB |
Output is correct |
14 |
Correct |
106 ms |
368 KB |
Output is correct |
15 |
Correct |
104 ms |
352 KB |
Output is correct |
16 |
Correct |
103 ms |
332 KB |
Output is correct |
17 |
Correct |
302 ms |
2212 KB |
Output is correct |
18 |
Correct |
298 ms |
2332 KB |
Output is correct |
19 |
Correct |
304 ms |
2296 KB |
Output is correct |
20 |
Correct |
118 ms |
332 KB |
Output is correct |
21 |
Correct |
112 ms |
332 KB |
Output is correct |
22 |
Correct |
73 ms |
332 KB |
Output is correct |
23 |
Correct |
60 ms |
332 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
168 ms |
2252 KB |
Output is correct |
26 |
Correct |
169 ms |
2236 KB |
Output is correct |
27 |
Correct |
264 ms |
2348 KB |
Output is correct |
28 |
Correct |
248 ms |
2288 KB |
Output is correct |
29 |
Correct |
253 ms |
2372 KB |
Output is correct |
30 |
Correct |
252 ms |
2512 KB |
Output is correct |
31 |
Correct |
110 ms |
332 KB |
Output is correct |
32 |
Correct |
77 ms |
332 KB |
Output is correct |
33 |
Correct |
243 ms |
1828 KB |
Output is correct |
34 |
Correct |
75 ms |
332 KB |
Output is correct |
35 |
Correct |
294 ms |
2252 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
0 ms |
204 KB |
Output is correct |
38 |
Correct |
168 ms |
2240 KB |
Output is correct |
39 |
Correct |
164 ms |
2204 KB |
Output is correct |
40 |
Correct |
259 ms |
2252 KB |
Output is correct |
41 |
Correct |
256 ms |
2296 KB |
Output is correct |
42 |
Correct |
255 ms |
2320 KB |
Output is correct |
43 |
Correct |
0 ms |
204 KB |
Output is correct |
44 |
Correct |
0 ms |
204 KB |
Output is correct |
45 |
Correct |
0 ms |
204 KB |
Output is correct |
46 |
Correct |
0 ms |
204 KB |
Output is correct |
47 |
Correct |
0 ms |
204 KB |
Output is correct |
48 |
Correct |
0 ms |
204 KB |
Output is correct |
49 |
Correct |
259 ms |
2308 KB |
Output is correct |
50 |
Correct |
256 ms |
2300 KB |
Output is correct |
51 |
Correct |
253 ms |
2176 KB |
Output is correct |
52 |
Correct |
258 ms |
2328 KB |
Output is correct |
53 |
Correct |
364 ms |
2096 KB |
Output is correct |
54 |
Correct |
360 ms |
2264 KB |
Output is correct |
55 |
Correct |
362 ms |
2208 KB |
Output is correct |
56 |
Correct |
113 ms |
332 KB |
Output is correct |
57 |
Correct |
114 ms |
356 KB |
Output is correct |
58 |
Correct |
307 ms |
1612 KB |
Output is correct |
59 |
Correct |
312 ms |
1700 KB |
Output is correct |
60 |
Correct |
308 ms |
1612 KB |
Output is correct |
61 |
Correct |
310 ms |
1688 KB |
Output is correct |
62 |
Correct |
304 ms |
1704 KB |
Output is correct |
63 |
Correct |
304 ms |
1612 KB |
Output is correct |
64 |
Correct |
243 ms |
1784 KB |
Output is correct |
65 |
Correct |
208 ms |
2636 KB |
Output is correct |
66 |
Correct |
193 ms |
2740 KB |
Output is correct |
67 |
Correct |
19 ms |
2372 KB |
Output is correct |
68 |
Correct |
189 ms |
3412 KB |
Output is correct |
69 |
Correct |
195 ms |
3424 KB |
Output is correct |
70 |
Correct |
317 ms |
3524 KB |
Output is correct |
71 |
Correct |
293 ms |
3352 KB |
Output is correct |
72 |
Correct |
293 ms |
3396 KB |
Output is correct |
73 |
Correct |
297 ms |
3476 KB |
Output is correct |
74 |
Correct |
294 ms |
3444 KB |
Output is correct |