#include "bits/stdc++.h"
using namespace std;
#define ar array
const int N = 20;
int d[N][N], cc[N][N];
ar<int, 2> dp[1<<N][N];
signed main(){
ios::sync_with_stdio(0); cin.tie(0);
int n, m; cin>>n>>m;
memset(d, 63, sizeof d);
for(int i=0;i<m;i++){
int a, b, c; cin>>a>>b>>c;
d[a][b] = d[b][a] = c;
if(c>1) cc[a][b] = cc[b][a] = 1;
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(d[i][k] + d[k][j] < d[i][j]){
d[i][j] = d[i][k] + d[k][j];
cc[i][j] = cc[i][k] + cc[k][j];
} else if(d[i][k] + d[k][j] == d[i][j]){
cc[i][j] = min(cc[i][j], cc[i][k] + cc[k][j]);
}
}
}
}
memset(dp, 63, sizeof dp);
for(int i=0;i<n;i++) dp[1<<i][i] = {0, 0};
for(int mask=0;mask<(1 << n);mask++){
for(int j=0;j<n;j++){
if(mask >> j & 1) continue;
for(int k=0;k<n;k++){
if(mask >> k & 1){
dp[mask | (1 << j)][j] = min(dp[mask | (1 << j)][j], {dp[mask][k][0] + d[k][j], dp[mask][k][1] + cc[k][j]});
}
}
}
}
const int inf = 2e9;
ar<int, 2> res = {inf, inf};
for(int i=0;i<n;i++){
res = min(res, dp[(1 << n) - 1][i]);
}
assert(res[1] <= 1);
cout<<res[0]<<"\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
16 |
Correct |
62 ms |
164428 KB |
Output is correct |
17 |
Correct |
63 ms |
164464 KB |
Output is correct |
18 |
Correct |
62 ms |
164372 KB |
Output is correct |
19 |
Correct |
64 ms |
164372 KB |
Output is correct |
20 |
Correct |
81 ms |
164552 KB |
Output is correct |
21 |
Correct |
85 ms |
164448 KB |
Output is correct |
22 |
Correct |
1066 ms |
164556 KB |
Output is correct |
23 |
Correct |
508 ms |
164456 KB |
Output is correct |
24 |
Correct |
531 ms |
164556 KB |
Output is correct |
25 |
Correct |
279 ms |
164528 KB |
Output is correct |
26 |
Correct |
273 ms |
164528 KB |
Output is correct |
27 |
Correct |
886 ms |
164460 KB |
Output is correct |
28 |
Correct |
276 ms |
164464 KB |
Output is correct |
29 |
Correct |
277 ms |
164428 KB |
Output is correct |
30 |
Correct |
273 ms |
164372 KB |
Output is correct |
31 |
Correct |
281 ms |
164428 KB |
Output is correct |
32 |
Correct |
83 ms |
164400 KB |
Output is correct |
33 |
Correct |
64 ms |
164436 KB |
Output is correct |
34 |
Correct |
271 ms |
164408 KB |
Output is correct |
35 |
Correct |
273 ms |
164460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
16 |
Correct |
62 ms |
164428 KB |
Output is correct |
17 |
Correct |
63 ms |
164464 KB |
Output is correct |
18 |
Correct |
62 ms |
164372 KB |
Output is correct |
19 |
Correct |
64 ms |
164372 KB |
Output is correct |
20 |
Correct |
81 ms |
164552 KB |
Output is correct |
21 |
Correct |
85 ms |
164448 KB |
Output is correct |
22 |
Correct |
1066 ms |
164556 KB |
Output is correct |
23 |
Correct |
508 ms |
164456 KB |
Output is correct |
24 |
Correct |
531 ms |
164556 KB |
Output is correct |
25 |
Correct |
279 ms |
164528 KB |
Output is correct |
26 |
Correct |
273 ms |
164528 KB |
Output is correct |
27 |
Correct |
886 ms |
164460 KB |
Output is correct |
28 |
Correct |
276 ms |
164464 KB |
Output is correct |
29 |
Correct |
277 ms |
164428 KB |
Output is correct |
30 |
Correct |
273 ms |
164372 KB |
Output is correct |
31 |
Correct |
281 ms |
164428 KB |
Output is correct |
32 |
Correct |
83 ms |
164400 KB |
Output is correct |
33 |
Correct |
64 ms |
164436 KB |
Output is correct |
34 |
Correct |
271 ms |
164408 KB |
Output is correct |
35 |
Correct |
273 ms |
164460 KB |
Output is correct |
36 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 11 |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
16 |
Correct |
62 ms |
164428 KB |
Output is correct |
17 |
Correct |
63 ms |
164464 KB |
Output is correct |
18 |
Correct |
62 ms |
164372 KB |
Output is correct |
19 |
Correct |
64 ms |
164372 KB |
Output is correct |
20 |
Correct |
81 ms |
164552 KB |
Output is correct |
21 |
Correct |
85 ms |
164448 KB |
Output is correct |
22 |
Correct |
1066 ms |
164556 KB |
Output is correct |
23 |
Correct |
508 ms |
164456 KB |
Output is correct |
24 |
Correct |
531 ms |
164556 KB |
Output is correct |
25 |
Correct |
279 ms |
164528 KB |
Output is correct |
26 |
Correct |
273 ms |
164528 KB |
Output is correct |
27 |
Correct |
886 ms |
164460 KB |
Output is correct |
28 |
Correct |
276 ms |
164464 KB |
Output is correct |
29 |
Correct |
277 ms |
164428 KB |
Output is correct |
30 |
Correct |
273 ms |
164372 KB |
Output is correct |
31 |
Correct |
281 ms |
164428 KB |
Output is correct |
32 |
Correct |
83 ms |
164400 KB |
Output is correct |
33 |
Correct |
64 ms |
164436 KB |
Output is correct |
34 |
Correct |
271 ms |
164408 KB |
Output is correct |
35 |
Correct |
273 ms |
164460 KB |
Output is correct |
36 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 11 |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
16 |
Correct |
62 ms |
164428 KB |
Output is correct |
17 |
Correct |
63 ms |
164464 KB |
Output is correct |
18 |
Correct |
62 ms |
164372 KB |
Output is correct |
19 |
Correct |
64 ms |
164372 KB |
Output is correct |
20 |
Correct |
81 ms |
164552 KB |
Output is correct |
21 |
Correct |
85 ms |
164448 KB |
Output is correct |
22 |
Correct |
1066 ms |
164556 KB |
Output is correct |
23 |
Correct |
508 ms |
164456 KB |
Output is correct |
24 |
Correct |
531 ms |
164556 KB |
Output is correct |
25 |
Correct |
279 ms |
164528 KB |
Output is correct |
26 |
Correct |
273 ms |
164528 KB |
Output is correct |
27 |
Correct |
886 ms |
164460 KB |
Output is correct |
28 |
Correct |
276 ms |
164464 KB |
Output is correct |
29 |
Correct |
277 ms |
164428 KB |
Output is correct |
30 |
Correct |
273 ms |
164372 KB |
Output is correct |
31 |
Correct |
281 ms |
164428 KB |
Output is correct |
32 |
Correct |
83 ms |
164400 KB |
Output is correct |
33 |
Correct |
64 ms |
164436 KB |
Output is correct |
34 |
Correct |
271 ms |
164408 KB |
Output is correct |
35 |
Correct |
273 ms |
164460 KB |
Output is correct |
36 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 11 |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
16 |
Correct |
62 ms |
164428 KB |
Output is correct |
17 |
Correct |
63 ms |
164464 KB |
Output is correct |
18 |
Correct |
62 ms |
164372 KB |
Output is correct |
19 |
Correct |
64 ms |
164372 KB |
Output is correct |
20 |
Correct |
81 ms |
164552 KB |
Output is correct |
21 |
Correct |
85 ms |
164448 KB |
Output is correct |
22 |
Correct |
1066 ms |
164556 KB |
Output is correct |
23 |
Correct |
508 ms |
164456 KB |
Output is correct |
24 |
Correct |
531 ms |
164556 KB |
Output is correct |
25 |
Correct |
279 ms |
164528 KB |
Output is correct |
26 |
Correct |
273 ms |
164528 KB |
Output is correct |
27 |
Correct |
886 ms |
164460 KB |
Output is correct |
28 |
Correct |
276 ms |
164464 KB |
Output is correct |
29 |
Correct |
277 ms |
164428 KB |
Output is correct |
30 |
Correct |
273 ms |
164372 KB |
Output is correct |
31 |
Correct |
281 ms |
164428 KB |
Output is correct |
32 |
Correct |
83 ms |
164400 KB |
Output is correct |
33 |
Correct |
64 ms |
164436 KB |
Output is correct |
34 |
Correct |
271 ms |
164408 KB |
Output is correct |
35 |
Correct |
273 ms |
164460 KB |
Output is correct |
36 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 11 |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
164428 KB |
Output is correct |
2 |
Correct |
62 ms |
164428 KB |
Output is correct |
3 |
Correct |
60 ms |
164424 KB |
Output is correct |
4 |
Correct |
62 ms |
164404 KB |
Output is correct |
5 |
Correct |
60 ms |
164424 KB |
Output is correct |
6 |
Correct |
61 ms |
164380 KB |
Output is correct |
7 |
Correct |
62 ms |
164428 KB |
Output is correct |
8 |
Correct |
63 ms |
164440 KB |
Output is correct |
9 |
Correct |
62 ms |
164416 KB |
Output is correct |
10 |
Correct |
61 ms |
164556 KB |
Output is correct |
11 |
Correct |
61 ms |
164400 KB |
Output is correct |
12 |
Correct |
62 ms |
164444 KB |
Output is correct |
13 |
Correct |
68 ms |
164336 KB |
Output is correct |
14 |
Correct |
62 ms |
164452 KB |
Output is correct |
15 |
Correct |
61 ms |
164564 KB |
Output is correct |
16 |
Correct |
62 ms |
164428 KB |
Output is correct |
17 |
Correct |
63 ms |
164464 KB |
Output is correct |
18 |
Correct |
62 ms |
164372 KB |
Output is correct |
19 |
Correct |
64 ms |
164372 KB |
Output is correct |
20 |
Correct |
81 ms |
164552 KB |
Output is correct |
21 |
Correct |
85 ms |
164448 KB |
Output is correct |
22 |
Correct |
1066 ms |
164556 KB |
Output is correct |
23 |
Correct |
508 ms |
164456 KB |
Output is correct |
24 |
Correct |
531 ms |
164556 KB |
Output is correct |
25 |
Correct |
279 ms |
164528 KB |
Output is correct |
26 |
Correct |
273 ms |
164528 KB |
Output is correct |
27 |
Correct |
886 ms |
164460 KB |
Output is correct |
28 |
Correct |
276 ms |
164464 KB |
Output is correct |
29 |
Correct |
277 ms |
164428 KB |
Output is correct |
30 |
Correct |
273 ms |
164372 KB |
Output is correct |
31 |
Correct |
281 ms |
164428 KB |
Output is correct |
32 |
Correct |
83 ms |
164400 KB |
Output is correct |
33 |
Correct |
64 ms |
164436 KB |
Output is correct |
34 |
Correct |
271 ms |
164408 KB |
Output is correct |
35 |
Correct |
273 ms |
164460 KB |
Output is correct |
36 |
Runtime error |
3 ms |
468 KB |
Execution killed with signal 11 |
37 |
Halted |
0 ms |
0 KB |
- |