#include <bits/stdc++.h>
using namespace std;
#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
//~ #define int long long
using i64 = long long;
template <class F, class _S>
bool chmin(F &u, const _S &v){
bool flag = false;
if ( u > v ){
u = v; flag |= true;
}
return flag;
}
template <class F, class _S>
bool chmax(F &u, const _S &v){
bool flag = false;
if ( u < v ){
u = v; flag |= true;
}
return flag;
}
const i64 inf = 1e12;
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n, m; cin >> n >> m;
// subtask #2
vector <int> U(m), V(m), C(m), D(m);
for ( int i = 0; i < m; i++ ){
cin >> U[i] >> V[i] >> C[i] >> D[i];
--U[i], --V[i];
}
vector <int> is(m);
auto get = [&](auto U, auto V, auto C, int sx, int rev = -1){
vector <vector<ar<int,3>>> adj(n);
for ( int i = 0; i < (int)U.size(); i++ ){
int u = U[i], v = V[i];
if ( i == rev ) swap(u, v);
adj[u].pb({v, C[i], i});
}
vector <i64> dp(n, inf);
vector <int> fa(n, -1);
priority_queue <pair<i64,int>> q;
q.push({0, sx}); dp[sx] = 0;
while ( !q.empty() ){
auto [val, u] = q.top();
q.pop();
if ( -val != dp[u] ) continue;
for ( auto &[v, w, j]: adj[u] ){
if ( chmin(dp[v], dp[u] + w) ){
q.push({-dp[v], v});
fa[v] = j;
}
}
}
for ( auto &x: fa ){
if ( x != -1 ) is[x] = 1;
}
return dp;
};
auto f = [&](int j){
return get(U, V, C, 0, j)[n - 1] + get(U, V, C, n - 1, j)[0] + D[j];
};
int s = 0, e = n - 1;
auto fs = get(U, V, C, s), fe = get(U, V, C, e);
auto IS = is;
auto ts = get(V, U, C, s), te = get(V, U, C, e);
i64 opt = fs[e] + fe[s];
for ( int i = 0; i < m; i++ ){
i64 x = min(fs[e], fs[V[i]] + C[i] + te[U[i]]);
i64 y = min(fe[s], fe[V[i]] + C[i] + ts[U[i]]);
if ( IS[i] ){
chmin(opt, f(i));
} else{
chmin(opt, x + y + D[i]);
}
}
if ( opt >= inf ){
opt = -1;
}
cout << opt;
cout << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
26 ms |
560 KB |
Output is correct |
4 |
Correct |
28 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
22 ms |
348 KB |
Output is correct |
11 |
Correct |
35 ms |
552 KB |
Output is correct |
12 |
Correct |
27 ms |
348 KB |
Output is correct |
13 |
Correct |
12 ms |
348 KB |
Output is correct |
14 |
Correct |
19 ms |
352 KB |
Output is correct |
15 |
Correct |
18 ms |
556 KB |
Output is correct |
16 |
Correct |
18 ms |
552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
535 ms |
4000 KB |
Output is correct |
2 |
Correct |
559 ms |
4048 KB |
Output is correct |
3 |
Correct |
534 ms |
3984 KB |
Output is correct |
4 |
Correct |
25 ms |
604 KB |
Output is correct |
5 |
Correct |
19 ms |
548 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
264 ms |
3928 KB |
Output is correct |
10 |
Correct |
247 ms |
3944 KB |
Output is correct |
11 |
Correct |
448 ms |
3968 KB |
Output is correct |
12 |
Correct |
381 ms |
3940 KB |
Output is correct |
13 |
Correct |
440 ms |
4028 KB |
Output is correct |
14 |
Correct |
379 ms |
4044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
548 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
3 |
Correct |
451 ms |
3056 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
509 ms |
3824 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
306 ms |
3964 KB |
Output is correct |
9 |
Correct |
336 ms |
4008 KB |
Output is correct |
10 |
Correct |
449 ms |
3936 KB |
Output is correct |
11 |
Correct |
472 ms |
4024 KB |
Output is correct |
12 |
Correct |
485 ms |
3984 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
437 ms |
4008 KB |
Output is correct |
20 |
Correct |
412 ms |
3856 KB |
Output is correct |
21 |
Correct |
465 ms |
4080 KB |
Output is correct |
22 |
Correct |
420 ms |
3856 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
26 ms |
560 KB |
Output is correct |
4 |
Correct |
28 ms |
556 KB |
Output is correct |
5 |
Correct |
3 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
22 ms |
348 KB |
Output is correct |
11 |
Correct |
35 ms |
552 KB |
Output is correct |
12 |
Correct |
27 ms |
348 KB |
Output is correct |
13 |
Correct |
12 ms |
348 KB |
Output is correct |
14 |
Correct |
19 ms |
352 KB |
Output is correct |
15 |
Correct |
18 ms |
556 KB |
Output is correct |
16 |
Correct |
18 ms |
552 KB |
Output is correct |
17 |
Correct |
535 ms |
4000 KB |
Output is correct |
18 |
Correct |
559 ms |
4048 KB |
Output is correct |
19 |
Correct |
534 ms |
3984 KB |
Output is correct |
20 |
Correct |
25 ms |
604 KB |
Output is correct |
21 |
Correct |
19 ms |
548 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
264 ms |
3928 KB |
Output is correct |
26 |
Correct |
247 ms |
3944 KB |
Output is correct |
27 |
Correct |
448 ms |
3968 KB |
Output is correct |
28 |
Correct |
381 ms |
3940 KB |
Output is correct |
29 |
Correct |
440 ms |
4028 KB |
Output is correct |
30 |
Correct |
379 ms |
4044 KB |
Output is correct |
31 |
Correct |
18 ms |
548 KB |
Output is correct |
32 |
Correct |
2 ms |
348 KB |
Output is correct |
33 |
Correct |
451 ms |
3056 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
509 ms |
3824 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
306 ms |
3964 KB |
Output is correct |
39 |
Correct |
336 ms |
4008 KB |
Output is correct |
40 |
Correct |
449 ms |
3936 KB |
Output is correct |
41 |
Correct |
472 ms |
4024 KB |
Output is correct |
42 |
Correct |
485 ms |
3984 KB |
Output is correct |
43 |
Correct |
0 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
0 ms |
348 KB |
Output is correct |
48 |
Correct |
0 ms |
348 KB |
Output is correct |
49 |
Correct |
437 ms |
4008 KB |
Output is correct |
50 |
Correct |
412 ms |
3856 KB |
Output is correct |
51 |
Correct |
465 ms |
4080 KB |
Output is correct |
52 |
Correct |
420 ms |
3856 KB |
Output is correct |
53 |
Correct |
455 ms |
4240 KB |
Output is correct |
54 |
Correct |
543 ms |
3952 KB |
Output is correct |
55 |
Correct |
507 ms |
4048 KB |
Output is correct |
56 |
Correct |
30 ms |
348 KB |
Output is correct |
57 |
Correct |
22 ms |
344 KB |
Output is correct |
58 |
Correct |
282 ms |
3128 KB |
Output is correct |
59 |
Correct |
334 ms |
3232 KB |
Output is correct |
60 |
Correct |
407 ms |
3176 KB |
Output is correct |
61 |
Correct |
258 ms |
3128 KB |
Output is correct |
62 |
Correct |
302 ms |
3352 KB |
Output is correct |
63 |
Correct |
403 ms |
3140 KB |
Output is correct |
64 |
Correct |
744 ms |
3280 KB |
Output is correct |
65 |
Correct |
723 ms |
3392 KB |
Output is correct |
66 |
Correct |
977 ms |
3412 KB |
Output is correct |
67 |
Correct |
19 ms |
4264 KB |
Output is correct |
68 |
Correct |
251 ms |
5004 KB |
Output is correct |
69 |
Correct |
243 ms |
5064 KB |
Output is correct |
70 |
Correct |
468 ms |
5128 KB |
Output is correct |
71 |
Correct |
444 ms |
5032 KB |
Output is correct |
72 |
Correct |
376 ms |
5140 KB |
Output is correct |
73 |
Correct |
465 ms |
5220 KB |
Output is correct |
74 |
Correct |
422 ms |
5132 KB |
Output is correct |