#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
using namespace std;
const ll N = 200 + 6;
const ll inf = 1e13 + 7;
typedef pair<int,int> LL;
struct edge{
ll v,c,id;
};
vector<edge> g[N],rg[N];
ll n,m,u,v,c,inv,banned;
ll ans = inf,d[N],D[4][N];
bool dd[N],spec[100000];
void cond_to_all(ll scr,ll cond,ll Is_in){
memset(dd,0,sizeof(dd)); fill(d,d + n + 1,inf);
d[scr] = 0;
while(1){
ll x = inf,u = -1;
for (ll i = 1;i <= n;i++) if (d[i] < x && !dd[i]) x = d[i],u = i;
if (u == -1) break; dd[u] = 1;
for (auto i : g[u]){
ll v = i.v,L = i.c;
if (d[v] > d[u] + L && i.id != banned){
d[v] = d[u] + L;
}
}
}
for (ll i = 1;i <= n;i++) D[cond][i] = d[i];
}
void all_to_cond(ll scr,ll cond,ll Is_in){
memset(dd,0,sizeof(dd)); fill(d,d + n + 1,inf);
d[scr] = 0;
while(1){
ll x = inf,u = -1;
for (ll i = 1;i <= n;i++) if (d[i] < x && !dd[i]) x = d[i],u = i;
if (u == -1) break; dd[u] = 1;
for (auto i : rg[u]){
ll v = i.v,L = i.c;
if (d[v] > d[u] + L && i.id != banned){
d[v] = d[u] + L;
}
}
}
for (ll i = 1;i <= n;i++) D[cond][i] = d[i];
}
struct Road{
ll u,v,c,inv;
};
Road a[100000];
void Rev(ll id){
banned = id;
u = a[id].u; v = a[id].v; c = a[id].c;
g[v].push_back({u,c,m + 1}); rg[u].push_back({v,c,m + 1});
cond_to_all(1,0,0); cond_to_all(n,2,0); all_to_cond(1,1,0); all_to_cond(n,3,0);
ans = min(ans,D[0][n] + D[2][1] + a[id].inv);
g[v].pop_back(); rg[u].pop_back(); banned = 0;
cond_to_all(1,0,0); cond_to_all(n,2,0); all_to_cond(1,1,0); all_to_cond(n,3,0);
}
/// 0 : 1 to all 1 : all to 1 2 : n to all 3 : all to n
void Trace(ll now,ll cond){
dd[now] = 1;
if (cond == 0||cond == 2){
for (auto i : g[now]){
ll v = i.v,L = i.c;
if (D[cond][v] == D[cond][now] + L && !dd[v]) spec[i.id] = 1,Trace(v,cond);
}
}
else{
for (auto i : rg[now]){
ll v = i.v,L = i.c;
if (D[cond][v] == D[cond][now] + L && !dd[v]) spec[i.id] = 1,Trace(v,cond);
}
}
}
void process(ll now,ll cond){
memset(dd,0,sizeof(dd));
Trace(now,cond);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "test"
if (fopen(task".INP","r")){
freopen(task".INP","r",stdin);
freopen(task".OUT","w",stdout);
}
cin>>n>>m;
for (ll i = 1;i <= m;i++){
cin>>u>>v>>c>>inv; a[i] = {u,v,c,inv};
g[u].push_back({v,c,i}); rg[v].push_back({u,c,i});
}
cond_to_all(1,0,1); cond_to_all(n,2,1); all_to_cond(1,1,1); all_to_cond(n,3,1);
process(1,0); process(n,2); process(1,1); process(n,3);
//ll cnt = 0;
//for (ll i = 1;i <= m;i++) cnt += spec[i];
//assert(cnt <= 4*n);
ans = D[0][n] + D[2][1]; //cout<<ans; return 0;
for (ll i = 1;i <= m;i++){
u = a[i].u; v = a[i].v; c = a[i].c;
if (!spec[i]){
//cout<<D[2][1]; return 0;
ll p = min(D[0][n],D[0][v] + c + D[3][u]),q = min(D[2][1],D[2][v] + c + D[1][u]);
ans = min(ans,p + q + a[i].inv); //cout<<p<<" x "<<D[2][v]; return 0;
}
else Rev(i);
}
cout<<(ans >= inf ? -1 : ans);
}
Compilation message
ho_t4.cpp: In function 'void cond_to_all(long long int, long long int, long long int)':
ho_t4.cpp:24:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
24 | if (u == -1) break; dd[u] = 1;
| ^~
ho_t4.cpp:24:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
24 | if (u == -1) break; dd[u] = 1;
| ^~
ho_t4.cpp: In function 'void all_to_cond(long long int, long long int, long long int)':
ho_t4.cpp:41:9: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
41 | if (u == -1) break; dd[u] = 1;
| ^~
ho_t4.cpp:41:29: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
41 | if (u == -1) break; dd[u] = 1;
| ^~
ho_t4.cpp: In function 'int main()':
ho_t4.cpp:94:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
94 | freopen(task".INP","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
ho_t4.cpp:95:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
95 | freopen(task".OUT","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
213 ms |
620 KB |
Output is correct |
2 |
Correct |
6 ms |
364 KB |
Output is correct |
3 |
Correct |
363 ms |
492 KB |
Output is correct |
4 |
Correct |
383 ms |
620 KB |
Output is correct |
5 |
Correct |
6 ms |
492 KB |
Output is correct |
6 |
Correct |
22 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
192 ms |
620 KB |
Output is correct |
11 |
Correct |
265 ms |
512 KB |
Output is correct |
12 |
Correct |
239 ms |
512 KB |
Output is correct |
13 |
Correct |
103 ms |
492 KB |
Output is correct |
14 |
Correct |
245 ms |
620 KB |
Output is correct |
15 |
Correct |
243 ms |
492 KB |
Output is correct |
16 |
Correct |
234 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
675 ms |
6116 KB |
Output is correct |
2 |
Correct |
690 ms |
7148 KB |
Output is correct |
3 |
Correct |
693 ms |
6892 KB |
Output is correct |
4 |
Correct |
358 ms |
748 KB |
Output is correct |
5 |
Correct |
272 ms |
672 KB |
Output is correct |
6 |
Correct |
48 ms |
492 KB |
Output is correct |
7 |
Correct |
2 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
209 ms |
7148 KB |
Output is correct |
10 |
Correct |
206 ms |
7020 KB |
Output is correct |
11 |
Correct |
454 ms |
7276 KB |
Output is correct |
12 |
Correct |
453 ms |
7020 KB |
Output is correct |
13 |
Correct |
459 ms |
6736 KB |
Output is correct |
14 |
Correct |
442 ms |
7404 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
492 KB |
Output is correct |
2 |
Correct |
39 ms |
364 KB |
Output is correct |
3 |
Correct |
389 ms |
4844 KB |
Output is correct |
4 |
Correct |
25 ms |
492 KB |
Output is correct |
5 |
Correct |
460 ms |
6252 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
152 ms |
6380 KB |
Output is correct |
9 |
Correct |
145 ms |
6124 KB |
Output is correct |
10 |
Correct |
339 ms |
6252 KB |
Output is correct |
11 |
Correct |
356 ms |
6252 KB |
Output is correct |
12 |
Correct |
326 ms |
6508 KB |
Output is correct |
13 |
Correct |
1 ms |
364 KB |
Output is correct |
14 |
Correct |
1 ms |
364 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
355 ms |
6252 KB |
Output is correct |
20 |
Correct |
330 ms |
6124 KB |
Output is correct |
21 |
Correct |
336 ms |
6436 KB |
Output is correct |
22 |
Correct |
321 ms |
5868 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
213 ms |
620 KB |
Output is correct |
2 |
Correct |
6 ms |
364 KB |
Output is correct |
3 |
Correct |
363 ms |
492 KB |
Output is correct |
4 |
Correct |
383 ms |
620 KB |
Output is correct |
5 |
Correct |
6 ms |
492 KB |
Output is correct |
6 |
Correct |
22 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
1 ms |
364 KB |
Output is correct |
9 |
Correct |
1 ms |
492 KB |
Output is correct |
10 |
Correct |
192 ms |
620 KB |
Output is correct |
11 |
Correct |
265 ms |
512 KB |
Output is correct |
12 |
Correct |
239 ms |
512 KB |
Output is correct |
13 |
Correct |
103 ms |
492 KB |
Output is correct |
14 |
Correct |
245 ms |
620 KB |
Output is correct |
15 |
Correct |
243 ms |
492 KB |
Output is correct |
16 |
Correct |
234 ms |
492 KB |
Output is correct |
17 |
Correct |
675 ms |
6116 KB |
Output is correct |
18 |
Correct |
690 ms |
7148 KB |
Output is correct |
19 |
Correct |
693 ms |
6892 KB |
Output is correct |
20 |
Correct |
358 ms |
748 KB |
Output is correct |
21 |
Correct |
272 ms |
672 KB |
Output is correct |
22 |
Correct |
48 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
364 KB |
Output is correct |
24 |
Correct |
1 ms |
364 KB |
Output is correct |
25 |
Correct |
209 ms |
7148 KB |
Output is correct |
26 |
Correct |
206 ms |
7020 KB |
Output is correct |
27 |
Correct |
454 ms |
7276 KB |
Output is correct |
28 |
Correct |
453 ms |
7020 KB |
Output is correct |
29 |
Correct |
459 ms |
6736 KB |
Output is correct |
30 |
Correct |
442 ms |
7404 KB |
Output is correct |
31 |
Correct |
203 ms |
492 KB |
Output is correct |
32 |
Correct |
39 ms |
364 KB |
Output is correct |
33 |
Correct |
389 ms |
4844 KB |
Output is correct |
34 |
Correct |
25 ms |
492 KB |
Output is correct |
35 |
Correct |
460 ms |
6252 KB |
Output is correct |
36 |
Correct |
1 ms |
364 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
152 ms |
6380 KB |
Output is correct |
39 |
Correct |
145 ms |
6124 KB |
Output is correct |
40 |
Correct |
339 ms |
6252 KB |
Output is correct |
41 |
Correct |
356 ms |
6252 KB |
Output is correct |
42 |
Correct |
326 ms |
6508 KB |
Output is correct |
43 |
Correct |
1 ms |
364 KB |
Output is correct |
44 |
Correct |
1 ms |
364 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
1 ms |
384 KB |
Output is correct |
49 |
Correct |
355 ms |
6252 KB |
Output is correct |
50 |
Correct |
330 ms |
6124 KB |
Output is correct |
51 |
Correct |
336 ms |
6436 KB |
Output is correct |
52 |
Correct |
321 ms |
5868 KB |
Output is correct |
53 |
Correct |
664 ms |
6892 KB |
Output is correct |
54 |
Correct |
684 ms |
6508 KB |
Output is correct |
55 |
Correct |
688 ms |
6796 KB |
Output is correct |
56 |
Correct |
383 ms |
492 KB |
Output is correct |
57 |
Correct |
350 ms |
492 KB |
Output is correct |
58 |
Correct |
282 ms |
5368 KB |
Output is correct |
59 |
Correct |
314 ms |
5356 KB |
Output is correct |
60 |
Correct |
438 ms |
5356 KB |
Output is correct |
61 |
Correct |
276 ms |
5356 KB |
Output is correct |
62 |
Correct |
322 ms |
5228 KB |
Output is correct |
63 |
Correct |
458 ms |
5276 KB |
Output is correct |
64 |
Correct |
489 ms |
5740 KB |
Output is correct |
65 |
Correct |
542 ms |
5740 KB |
Output is correct |
66 |
Correct |
670 ms |
5868 KB |
Output is correct |
67 |
Correct |
19 ms |
4700 KB |
Output is correct |
68 |
Correct |
207 ms |
7020 KB |
Output is correct |
69 |
Correct |
206 ms |
7148 KB |
Output is correct |
70 |
Correct |
468 ms |
6764 KB |
Output is correct |
71 |
Correct |
465 ms |
6900 KB |
Output is correct |
72 |
Correct |
466 ms |
7148 KB |
Output is correct |
73 |
Correct |
446 ms |
6892 KB |
Output is correct |
74 |
Correct |
487 ms |
7276 KB |
Output is correct |