#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n, m;
int dist[22][22];
int DP[1048576][20];
vector<int> link[5002];
queue<pair<int, int> > que;
bool visited[5002];
int main(){
scanf("%d %d", &n, &m);
if(n>20){
for(int i=1; i<=m; i++){
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
if(c==1){
link[x].push_back(y);
link[y].push_back(x);
}
}
que.push(make_pair(1, 0));
visited[1] = 1;
int nxt = 0, dst=0;
while(!que.empty()){
auto tmp = que.front(); que.pop();
nxt = tmp.first;
dst = tmp.second;
for(auto x: link[tmp.first]){
if(visited[x]) continue;
visited[x] = 1;
que.push(make_pair(x, tmp.second+1));
}
}
memset(visited, 0, sizeof(visited));
nxt = dst = 0;
que.push(make_pair(nxt, 0));
visited[nxt] = 1;
while(!que.empty()){
auto tmp = que.front(); que.pop();
nxt = tmp.first;
dst = tmp.second;
for(auto x: link[tmp.first]){
if(visited[x]) continue;
visited[x] = 1;
que.push(make_pair(x, tmp.second+1));
}
}
printf("%d", 2*n-2-dst);
return 0;
}
for(int i=0; i<=n; i++){
for(int j=0; j<=n; j++){
dist[i][j] = 1e9;
}
}
for(int i=1; i<=m; i++){
int x, y, c;
scanf("%d %d %d", &x, &y, &c);
dist[x][y] = dist[y][x] = c;
}
for(int k=0; k<n; k++){
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
}
}
}
for(int i=0; i<(1<<n); i++) for(int j=0; j<=n; j++) DP[i][j] = 1e9;
for(int i=0; i<n; i++) DP[(1<<i)][i] = 0;
for(int d=1; d<(1<<n); d++){
for(int i=0; i<n; i++){
if((d>>i)&1){
// printf("%d %d: %d\n", d, i, DP[d][i]);
for(int j=0; j<n; j++){
DP[(d|(1<<j))][j] = min(DP[(d|(1<<j))][j], DP[d][i] + dist[i][j]);
}
}
}
}
int ans = 1e9;
for(int i=0; i<n; i++) ans = min(ans, DP[(1<<n)-1][i]);
printf("%d", ans);
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:16:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | scanf("%d %d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:20:18: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d %d %d", &x, &y, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:68:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
68 | scanf("%d %d %d", &x, &y, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
684 KB |
Output is correct |
20 |
Correct |
10 ms |
2900 KB |
Output is correct |
21 |
Correct |
9 ms |
3004 KB |
Output is correct |
22 |
Correct |
438 ms |
82480 KB |
Output is correct |
23 |
Correct |
206 ms |
41448 KB |
Output is correct |
24 |
Correct |
197 ms |
41452 KB |
Output is correct |
25 |
Correct |
102 ms |
20948 KB |
Output is correct |
26 |
Correct |
105 ms |
20948 KB |
Output is correct |
27 |
Correct |
433 ms |
82488 KB |
Output is correct |
28 |
Correct |
107 ms |
20944 KB |
Output is correct |
29 |
Correct |
93 ms |
20940 KB |
Output is correct |
30 |
Correct |
104 ms |
20936 KB |
Output is correct |
31 |
Correct |
100 ms |
20948 KB |
Output is correct |
32 |
Correct |
10 ms |
2996 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
105 ms |
20948 KB |
Output is correct |
35 |
Correct |
96 ms |
20948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
684 KB |
Output is correct |
20 |
Correct |
10 ms |
2900 KB |
Output is correct |
21 |
Correct |
9 ms |
3004 KB |
Output is correct |
22 |
Correct |
438 ms |
82480 KB |
Output is correct |
23 |
Correct |
206 ms |
41448 KB |
Output is correct |
24 |
Correct |
197 ms |
41452 KB |
Output is correct |
25 |
Correct |
102 ms |
20948 KB |
Output is correct |
26 |
Correct |
105 ms |
20948 KB |
Output is correct |
27 |
Correct |
433 ms |
82488 KB |
Output is correct |
28 |
Correct |
107 ms |
20944 KB |
Output is correct |
29 |
Correct |
93 ms |
20940 KB |
Output is correct |
30 |
Correct |
104 ms |
20936 KB |
Output is correct |
31 |
Correct |
100 ms |
20948 KB |
Output is correct |
32 |
Correct |
10 ms |
2996 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
105 ms |
20948 KB |
Output is correct |
35 |
Correct |
96 ms |
20948 KB |
Output is correct |
36 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
684 KB |
Output is correct |
20 |
Correct |
10 ms |
2900 KB |
Output is correct |
21 |
Correct |
9 ms |
3004 KB |
Output is correct |
22 |
Correct |
438 ms |
82480 KB |
Output is correct |
23 |
Correct |
206 ms |
41448 KB |
Output is correct |
24 |
Correct |
197 ms |
41452 KB |
Output is correct |
25 |
Correct |
102 ms |
20948 KB |
Output is correct |
26 |
Correct |
105 ms |
20948 KB |
Output is correct |
27 |
Correct |
433 ms |
82488 KB |
Output is correct |
28 |
Correct |
107 ms |
20944 KB |
Output is correct |
29 |
Correct |
93 ms |
20940 KB |
Output is correct |
30 |
Correct |
104 ms |
20936 KB |
Output is correct |
31 |
Correct |
100 ms |
20948 KB |
Output is correct |
32 |
Correct |
10 ms |
2996 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
105 ms |
20948 KB |
Output is correct |
35 |
Correct |
96 ms |
20948 KB |
Output is correct |
36 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
684 KB |
Output is correct |
20 |
Correct |
10 ms |
2900 KB |
Output is correct |
21 |
Correct |
9 ms |
3004 KB |
Output is correct |
22 |
Correct |
438 ms |
82480 KB |
Output is correct |
23 |
Correct |
206 ms |
41448 KB |
Output is correct |
24 |
Correct |
197 ms |
41452 KB |
Output is correct |
25 |
Correct |
102 ms |
20948 KB |
Output is correct |
26 |
Correct |
105 ms |
20948 KB |
Output is correct |
27 |
Correct |
433 ms |
82488 KB |
Output is correct |
28 |
Correct |
107 ms |
20944 KB |
Output is correct |
29 |
Correct |
93 ms |
20940 KB |
Output is correct |
30 |
Correct |
104 ms |
20936 KB |
Output is correct |
31 |
Correct |
100 ms |
20948 KB |
Output is correct |
32 |
Correct |
10 ms |
2996 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
105 ms |
20948 KB |
Output is correct |
35 |
Correct |
96 ms |
20948 KB |
Output is correct |
36 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
684 KB |
Output is correct |
20 |
Correct |
10 ms |
2900 KB |
Output is correct |
21 |
Correct |
9 ms |
3004 KB |
Output is correct |
22 |
Correct |
438 ms |
82480 KB |
Output is correct |
23 |
Correct |
206 ms |
41448 KB |
Output is correct |
24 |
Correct |
197 ms |
41452 KB |
Output is correct |
25 |
Correct |
102 ms |
20948 KB |
Output is correct |
26 |
Correct |
105 ms |
20948 KB |
Output is correct |
27 |
Correct |
433 ms |
82488 KB |
Output is correct |
28 |
Correct |
107 ms |
20944 KB |
Output is correct |
29 |
Correct |
93 ms |
20940 KB |
Output is correct |
30 |
Correct |
104 ms |
20936 KB |
Output is correct |
31 |
Correct |
100 ms |
20948 KB |
Output is correct |
32 |
Correct |
10 ms |
2996 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
105 ms |
20948 KB |
Output is correct |
35 |
Correct |
96 ms |
20948 KB |
Output is correct |
36 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
424 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
428 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
0 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 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 |
428 KB |
Output is correct |
17 |
Correct |
1 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
468 KB |
Output is correct |
19 |
Correct |
1 ms |
684 KB |
Output is correct |
20 |
Correct |
10 ms |
2900 KB |
Output is correct |
21 |
Correct |
9 ms |
3004 KB |
Output is correct |
22 |
Correct |
438 ms |
82480 KB |
Output is correct |
23 |
Correct |
206 ms |
41448 KB |
Output is correct |
24 |
Correct |
197 ms |
41452 KB |
Output is correct |
25 |
Correct |
102 ms |
20948 KB |
Output is correct |
26 |
Correct |
105 ms |
20948 KB |
Output is correct |
27 |
Correct |
433 ms |
82488 KB |
Output is correct |
28 |
Correct |
107 ms |
20944 KB |
Output is correct |
29 |
Correct |
93 ms |
20940 KB |
Output is correct |
30 |
Correct |
104 ms |
20936 KB |
Output is correct |
31 |
Correct |
100 ms |
20948 KB |
Output is correct |
32 |
Correct |
10 ms |
2996 KB |
Output is correct |
33 |
Correct |
1 ms |
468 KB |
Output is correct |
34 |
Correct |
105 ms |
20948 KB |
Output is correct |
35 |
Correct |
96 ms |
20948 KB |
Output is correct |
36 |
Incorrect |
4 ms |
468 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |