#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){
vector <vector<ar<int,3>>> adj(n);
for ( int i = 0; i < (int)U.size(); i++ ){
int u = U[i], v = V[i];
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){
auto calc = [&](int sx, int se){
vector <vector<i64>> adj(n, vector <i64> (n, inf));
for ( int i = 0; i < m; i++ ){
int u = U[i], v = V[i];
if ( i == j ) swap(u, v);
chmin(adj[u][v], C[i]);
}
vector <i64> d(n, inf), us(n);
d[sx] = 0;
for ( int iter = 0; iter < n; iter++ ){
int u = -1;
for ( int v = 0; v < n; v++ ){
if ( !us[v] && (u == -1 || d[u] > d[v]) ) u = v;
}
if ( u == se ) return d[u];
us[u] = 1;
for ( int v = 0; v < n; v++ ){
chmin(d[v], d[u] + adj[u][v]);
}
}
return inf;
};
return calc(0, n - 1) + calc(n - 1, 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';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
824 KB |
Output is correct |
2 |
Correct |
1 ms |
792 KB |
Output is correct |
3 |
Correct |
60 ms |
836 KB |
Output is correct |
4 |
Correct |
66 ms |
1080 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
788 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
55 ms |
840 KB |
Output is correct |
11 |
Correct |
76 ms |
836 KB |
Output is correct |
12 |
Correct |
70 ms |
832 KB |
Output is correct |
13 |
Correct |
34 ms |
836 KB |
Output is correct |
14 |
Correct |
53 ms |
832 KB |
Output is correct |
15 |
Correct |
55 ms |
836 KB |
Output is correct |
16 |
Correct |
52 ms |
1144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
3284 KB |
Output is correct |
2 |
Correct |
113 ms |
3172 KB |
Output is correct |
3 |
Correct |
97 ms |
3192 KB |
Output is correct |
4 |
Correct |
35 ms |
868 KB |
Output is correct |
5 |
Correct |
45 ms |
1092 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
7 ms |
804 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
68 ms |
3160 KB |
Output is correct |
10 |
Correct |
65 ms |
3076 KB |
Output is correct |
11 |
Correct |
119 ms |
3196 KB |
Output is correct |
12 |
Correct |
107 ms |
3212 KB |
Output is correct |
13 |
Correct |
119 ms |
3264 KB |
Output is correct |
14 |
Correct |
101 ms |
3336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
832 KB |
Output is correct |
2 |
Correct |
19 ms |
800 KB |
Output is correct |
3 |
Correct |
195 ms |
2476 KB |
Output is correct |
4 |
Correct |
9 ms |
792 KB |
Output is correct |
5 |
Correct |
205 ms |
3236 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
128 ms |
3264 KB |
Output is correct |
9 |
Correct |
144 ms |
3184 KB |
Output is correct |
10 |
Correct |
179 ms |
3088 KB |
Output is correct |
11 |
Correct |
162 ms |
3152 KB |
Output is correct |
12 |
Correct |
167 ms |
3232 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 |
185 ms |
3248 KB |
Output is correct |
20 |
Correct |
167 ms |
3436 KB |
Output is correct |
21 |
Correct |
167 ms |
3164 KB |
Output is correct |
22 |
Correct |
173 ms |
3168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
824 KB |
Output is correct |
2 |
Correct |
1 ms |
792 KB |
Output is correct |
3 |
Correct |
60 ms |
836 KB |
Output is correct |
4 |
Correct |
66 ms |
1080 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
3 ms |
788 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
55 ms |
840 KB |
Output is correct |
11 |
Correct |
76 ms |
836 KB |
Output is correct |
12 |
Correct |
70 ms |
832 KB |
Output is correct |
13 |
Correct |
34 ms |
836 KB |
Output is correct |
14 |
Correct |
53 ms |
832 KB |
Output is correct |
15 |
Correct |
55 ms |
836 KB |
Output is correct |
16 |
Correct |
52 ms |
1144 KB |
Output is correct |
17 |
Correct |
103 ms |
3284 KB |
Output is correct |
18 |
Correct |
113 ms |
3172 KB |
Output is correct |
19 |
Correct |
97 ms |
3192 KB |
Output is correct |
20 |
Correct |
35 ms |
868 KB |
Output is correct |
21 |
Correct |
45 ms |
1092 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
7 ms |
804 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
68 ms |
3160 KB |
Output is correct |
26 |
Correct |
65 ms |
3076 KB |
Output is correct |
27 |
Correct |
119 ms |
3196 KB |
Output is correct |
28 |
Correct |
107 ms |
3212 KB |
Output is correct |
29 |
Correct |
119 ms |
3264 KB |
Output is correct |
30 |
Correct |
101 ms |
3336 KB |
Output is correct |
31 |
Correct |
52 ms |
832 KB |
Output is correct |
32 |
Correct |
19 ms |
800 KB |
Output is correct |
33 |
Correct |
195 ms |
2476 KB |
Output is correct |
34 |
Correct |
9 ms |
792 KB |
Output is correct |
35 |
Correct |
205 ms |
3236 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
128 ms |
3264 KB |
Output is correct |
39 |
Correct |
144 ms |
3184 KB |
Output is correct |
40 |
Correct |
179 ms |
3088 KB |
Output is correct |
41 |
Correct |
162 ms |
3152 KB |
Output is correct |
42 |
Correct |
167 ms |
3232 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 |
185 ms |
3248 KB |
Output is correct |
50 |
Correct |
167 ms |
3436 KB |
Output is correct |
51 |
Correct |
167 ms |
3164 KB |
Output is correct |
52 |
Correct |
173 ms |
3168 KB |
Output is correct |
53 |
Correct |
153 ms |
3528 KB |
Output is correct |
54 |
Correct |
172 ms |
3248 KB |
Output is correct |
55 |
Correct |
158 ms |
3096 KB |
Output is correct |
56 |
Correct |
67 ms |
1952 KB |
Output is correct |
57 |
Correct |
46 ms |
832 KB |
Output is correct |
58 |
Correct |
103 ms |
2616 KB |
Output is correct |
59 |
Correct |
121 ms |
2588 KB |
Output is correct |
60 |
Correct |
160 ms |
2516 KB |
Output is correct |
61 |
Correct |
108 ms |
2624 KB |
Output is correct |
62 |
Correct |
117 ms |
2464 KB |
Output is correct |
63 |
Correct |
162 ms |
2612 KB |
Output is correct |
64 |
Correct |
123 ms |
3128 KB |
Output is correct |
65 |
Correct |
137 ms |
3000 KB |
Output is correct |
66 |
Correct |
182 ms |
2920 KB |
Output is correct |
67 |
Correct |
10 ms |
3016 KB |
Output is correct |
68 |
Correct |
109 ms |
3168 KB |
Output is correct |
69 |
Correct |
101 ms |
3224 KB |
Output is correct |
70 |
Correct |
182 ms |
3176 KB |
Output is correct |
71 |
Correct |
184 ms |
3192 KB |
Output is correct |
72 |
Correct |
185 ms |
3328 KB |
Output is correct |
73 |
Correct |
192 ms |
3236 KB |
Output is correct |
74 |
Correct |
176 ms |
3260 KB |
Output is correct |