#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define bug(x) cout << #x << " : " << x << '\n'
#define bug2(x, y) cout << #x << ' ' << #y << " : " << x << ' ' << y << '\n'
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 2e2 + 10;
const int maxm = 5e4 + 10;
const int mod = 1e9 + 7;
const int inf = 2e9 + 10;
int n, m;
int u[maxm], v[maxm], c[maxm], f[maxm];
int d[2][2][maxn][maxn];
int par[2][2][maxn];
bool mark[maxn];
vector<int> G[maxn][2];
int ant(int e, int v){
return ::v[e] + ::u[e] - v;
}
void dijkstra(int src, bool st, bool eg, int s){
int (&d)[maxn] = ::d[st][eg][s];
int (&p)[maxn] = par[st][eg];
fill_n(d, maxn, inf);
memset(mark, 0, sizeof mark);
if(!s)
memset(p, -1, sizeof p);
d[src] = 0;
while(true){
int v = -1, h = inf;
for(int i = 1; i <= n; i ++)
if(!mark[i] and d[i] < h)
v = i, h = d[i];
if(v == -1) return;
mark[v] = true;
for(int e : G[v][eg]){
int u = ant(e, v);
int res = min((ll)h + c[e], (ll)inf);
if(res < d[u]){
d[u] = res;
if(!s)
p[u] = e;
}
}
}
}
int main(){
ios;
cin >> n >> m;
for(int i = 0; i < m; i ++){
int u, v, c, f;
cin >> u >> v >> c >> f;
G[u][0].pb(i);
G[v][1].pb(i);
::u[i] = u;
::v[i] = v;
::c[i] = c;
::f[i] = f;
}
for(int i = 0; i <= 1; i ++){
dijkstra(1, 0, i, 0);
dijkstra(n, 1, i, 0);
}
for(int i = 0; i <= 1; i ++){
for(int j = 0; j <= 1; j ++){
for(int v = 1; v <= n; v ++){
int e = par[i][j][v];
if(~e){
int x = c[e];
c[e] = inf;
dijkstra(i ? n : 1, i, j, v);
c[e] = x;
}
}
}
}
ll ans = (ll)d[0][0][0][n] + d[1][0][0][1];
for(int i = 0; i < m; i ++){
int u = ::u[i];
int v = ::v[i];
int c = ::c[i];
int f = ::f[i];
int state[2][2];
for(int j = 0; j <= 1; j ++){
state[j][0] = i == par[j][0][v] ? v : 0;
state[j][1] = i == par[j][1][u] ? u : 0;
}
int (&d1_out)[maxn] = d[0][0][state[0][0]];
int (&d1_in)[maxn] = d[0][1][state[0][1]];
int (&dn_out)[maxn] = d[1][0][state[1][0]];
int (&dn_in)[maxn] = d[1][1][state[1][1]];
ll res1 = min((ll)d1_out[n], (ll)d1_out[v] + c + dn_in[u]);
ll resn = min((ll)dn_out[1], (ll)dn_out[v] + c + d1_in[u]);
ans = min(ans, res1 + resn + f);
}
if(ans >= inf)
ans = -1;
cout << ans << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
1028 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
79 ms |
1020 KB |
Output is correct |
4 |
Correct |
82 ms |
1068 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
87 ms |
944 KB |
Output is correct |
11 |
Correct |
82 ms |
944 KB |
Output is correct |
12 |
Correct |
83 ms |
952 KB |
Output is correct |
13 |
Correct |
17 ms |
980 KB |
Output is correct |
14 |
Correct |
50 ms |
1024 KB |
Output is correct |
15 |
Correct |
49 ms |
996 KB |
Output is correct |
16 |
Correct |
50 ms |
1040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
3328 KB |
Output is correct |
2 |
Correct |
226 ms |
3404 KB |
Output is correct |
3 |
Correct |
229 ms |
3516 KB |
Output is correct |
4 |
Correct |
80 ms |
1024 KB |
Output is correct |
5 |
Correct |
63 ms |
956 KB |
Output is correct |
6 |
Correct |
17 ms |
708 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
65 ms |
3512 KB |
Output is correct |
10 |
Correct |
57 ms |
3592 KB |
Output is correct |
11 |
Correct |
148 ms |
3492 KB |
Output is correct |
12 |
Correct |
149 ms |
3704 KB |
Output is correct |
13 |
Correct |
140 ms |
3688 KB |
Output is correct |
14 |
Correct |
143 ms |
3572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
984 KB |
Output is correct |
2 |
Correct |
13 ms |
848 KB |
Output is correct |
3 |
Correct |
150 ms |
2792 KB |
Output is correct |
4 |
Correct |
7 ms |
824 KB |
Output is correct |
5 |
Correct |
198 ms |
3268 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
54 ms |
3340 KB |
Output is correct |
9 |
Correct |
58 ms |
3348 KB |
Output is correct |
10 |
Correct |
134 ms |
3284 KB |
Output is correct |
11 |
Correct |
126 ms |
3296 KB |
Output is correct |
12 |
Correct |
124 ms |
3296 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
143 ms |
3408 KB |
Output is correct |
20 |
Correct |
136 ms |
3312 KB |
Output is correct |
21 |
Correct |
126 ms |
3276 KB |
Output is correct |
22 |
Correct |
131 ms |
3292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
1028 KB |
Output is correct |
2 |
Correct |
3 ms |
468 KB |
Output is correct |
3 |
Correct |
79 ms |
1020 KB |
Output is correct |
4 |
Correct |
82 ms |
1068 KB |
Output is correct |
5 |
Correct |
2 ms |
468 KB |
Output is correct |
6 |
Correct |
6 ms |
724 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
87 ms |
944 KB |
Output is correct |
11 |
Correct |
82 ms |
944 KB |
Output is correct |
12 |
Correct |
83 ms |
952 KB |
Output is correct |
13 |
Correct |
17 ms |
980 KB |
Output is correct |
14 |
Correct |
50 ms |
1024 KB |
Output is correct |
15 |
Correct |
49 ms |
996 KB |
Output is correct |
16 |
Correct |
50 ms |
1040 KB |
Output is correct |
17 |
Correct |
216 ms |
3328 KB |
Output is correct |
18 |
Correct |
226 ms |
3404 KB |
Output is correct |
19 |
Correct |
229 ms |
3516 KB |
Output is correct |
20 |
Correct |
80 ms |
1024 KB |
Output is correct |
21 |
Correct |
63 ms |
956 KB |
Output is correct |
22 |
Correct |
17 ms |
708 KB |
Output is correct |
23 |
Correct |
1 ms |
468 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
65 ms |
3512 KB |
Output is correct |
26 |
Correct |
57 ms |
3592 KB |
Output is correct |
27 |
Correct |
148 ms |
3492 KB |
Output is correct |
28 |
Correct |
149 ms |
3704 KB |
Output is correct |
29 |
Correct |
140 ms |
3688 KB |
Output is correct |
30 |
Correct |
143 ms |
3572 KB |
Output is correct |
31 |
Correct |
74 ms |
984 KB |
Output is correct |
32 |
Correct |
13 ms |
848 KB |
Output is correct |
33 |
Correct |
150 ms |
2792 KB |
Output is correct |
34 |
Correct |
7 ms |
824 KB |
Output is correct |
35 |
Correct |
198 ms |
3268 KB |
Output is correct |
36 |
Correct |
1 ms |
340 KB |
Output is correct |
37 |
Correct |
0 ms |
340 KB |
Output is correct |
38 |
Correct |
54 ms |
3340 KB |
Output is correct |
39 |
Correct |
58 ms |
3348 KB |
Output is correct |
40 |
Correct |
134 ms |
3284 KB |
Output is correct |
41 |
Correct |
126 ms |
3296 KB |
Output is correct |
42 |
Correct |
124 ms |
3296 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
340 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
336 KB |
Output is correct |
47 |
Correct |
1 ms |
340 KB |
Output is correct |
48 |
Correct |
1 ms |
340 KB |
Output is correct |
49 |
Correct |
143 ms |
3408 KB |
Output is correct |
50 |
Correct |
136 ms |
3312 KB |
Output is correct |
51 |
Correct |
126 ms |
3276 KB |
Output is correct |
52 |
Correct |
131 ms |
3292 KB |
Output is correct |
53 |
Correct |
220 ms |
3396 KB |
Output is correct |
54 |
Correct |
261 ms |
3332 KB |
Output is correct |
55 |
Correct |
252 ms |
3340 KB |
Output is correct |
56 |
Correct |
79 ms |
972 KB |
Output is correct |
57 |
Correct |
80 ms |
1028 KB |
Output is correct |
58 |
Correct |
186 ms |
2940 KB |
Output is correct |
59 |
Correct |
198 ms |
2836 KB |
Output is correct |
60 |
Correct |
230 ms |
2808 KB |
Output is correct |
61 |
Correct |
201 ms |
2800 KB |
Output is correct |
62 |
Correct |
191 ms |
2832 KB |
Output is correct |
63 |
Correct |
199 ms |
2868 KB |
Output is correct |
64 |
Correct |
320 ms |
2996 KB |
Output is correct |
65 |
Correct |
301 ms |
3020 KB |
Output is correct |
66 |
Correct |
274 ms |
3000 KB |
Output is correct |
67 |
Correct |
13 ms |
2256 KB |
Output is correct |
68 |
Correct |
59 ms |
3516 KB |
Output is correct |
69 |
Correct |
59 ms |
3504 KB |
Output is correct |
70 |
Correct |
149 ms |
3572 KB |
Output is correct |
71 |
Correct |
163 ms |
3552 KB |
Output is correct |
72 |
Correct |
158 ms |
3508 KB |
Output is correct |
73 |
Correct |
159 ms |
3556 KB |
Output is correct |
74 |
Correct |
156 ms |
3660 KB |
Output is correct |