Submission #538183

# Submission time Handle Problem Language Result Execution time Memory
538183 2022-03-16T07:36:25 Z 79brue Mountains and Valleys (CCO20_day1problem3) C++14
5 / 25
465 ms 82480 KB
#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));
        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);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 9 ms 2900 KB Output is correct
21 Correct 10 ms 2900 KB Output is correct
22 Correct 465 ms 82480 KB Output is correct
23 Correct 201 ms 41448 KB Output is correct
24 Correct 243 ms 41356 KB Output is correct
25 Correct 100 ms 20924 KB Output is correct
26 Correct 94 ms 20948 KB Output is correct
27 Correct 453 ms 82380 KB Output is correct
28 Correct 87 ms 20820 KB Output is correct
29 Correct 113 ms 20928 KB Output is correct
30 Correct 118 ms 20924 KB Output is correct
31 Correct 119 ms 20820 KB Output is correct
32 Correct 9 ms 2900 KB Output is correct
33 Correct 0 ms 468 KB Output is correct
34 Correct 94 ms 20928 KB Output is correct
35 Correct 96 ms 20940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 596 KB Output is correct
2 Correct 4 ms 596 KB Output is correct
3 Correct 4 ms 692 KB Output is correct
4 Correct 4 ms 596 KB Output is correct
5 Correct 4 ms 596 KB Output is correct
6 Correct 4 ms 724 KB Output is correct
7 Correct 4 ms 596 KB Output is correct
8 Correct 4 ms 596 KB Output is correct
9 Correct 4 ms 596 KB Output is correct
10 Correct 5 ms 596 KB Output is correct
11 Correct 6 ms 596 KB Output is correct
12 Correct 6 ms 696 KB Output is correct
13 Correct 4 ms 596 KB Output is correct
14 Correct 5 ms 700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 9 ms 2900 KB Output is correct
21 Correct 10 ms 2900 KB Output is correct
22 Correct 465 ms 82480 KB Output is correct
23 Correct 201 ms 41448 KB Output is correct
24 Correct 243 ms 41356 KB Output is correct
25 Correct 100 ms 20924 KB Output is correct
26 Correct 94 ms 20948 KB Output is correct
27 Correct 453 ms 82380 KB Output is correct
28 Correct 87 ms 20820 KB Output is correct
29 Correct 113 ms 20928 KB Output is correct
30 Correct 118 ms 20924 KB Output is correct
31 Correct 119 ms 20820 KB Output is correct
32 Correct 9 ms 2900 KB Output is correct
33 Correct 0 ms 468 KB Output is correct
34 Correct 94 ms 20928 KB Output is correct
35 Correct 96 ms 20940 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 408 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Incorrect 1 ms 340 KB Output isn't correct
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 9 ms 2900 KB Output is correct
21 Correct 10 ms 2900 KB Output is correct
22 Correct 465 ms 82480 KB Output is correct
23 Correct 201 ms 41448 KB Output is correct
24 Correct 243 ms 41356 KB Output is correct
25 Correct 100 ms 20924 KB Output is correct
26 Correct 94 ms 20948 KB Output is correct
27 Correct 453 ms 82380 KB Output is correct
28 Correct 87 ms 20820 KB Output is correct
29 Correct 113 ms 20928 KB Output is correct
30 Correct 118 ms 20924 KB Output is correct
31 Correct 119 ms 20820 KB Output is correct
32 Correct 9 ms 2900 KB Output is correct
33 Correct 0 ms 468 KB Output is correct
34 Correct 94 ms 20928 KB Output is correct
35 Correct 96 ms 20940 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 0 ms 408 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Incorrect 1 ms 340 KB Output isn't correct
40 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 9 ms 2900 KB Output is correct
21 Correct 10 ms 2900 KB Output is correct
22 Correct 465 ms 82480 KB Output is correct
23 Correct 201 ms 41448 KB Output is correct
24 Correct 243 ms 41356 KB Output is correct
25 Correct 100 ms 20924 KB Output is correct
26 Correct 94 ms 20948 KB Output is correct
27 Correct 453 ms 82380 KB Output is correct
28 Correct 87 ms 20820 KB Output is correct
29 Correct 113 ms 20928 KB Output is correct
30 Correct 118 ms 20924 KB Output is correct
31 Correct 119 ms 20820 KB Output is correct
32 Correct 9 ms 2900 KB Output is correct
33 Correct 0 ms 468 KB Output is correct
34 Correct 94 ms 20928 KB Output is correct
35 Correct 96 ms 20940 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 4 ms 692 KB Output is correct
39 Correct 4 ms 596 KB Output is correct
40 Correct 4 ms 596 KB Output is correct
41 Correct 4 ms 724 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 4 ms 596 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 5 ms 596 KB Output is correct
46 Correct 6 ms 596 KB Output is correct
47 Correct 6 ms 696 KB Output is correct
48 Correct 4 ms 596 KB Output is correct
49 Correct 5 ms 700 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 408 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Incorrect 1 ms 340 KB Output isn't correct
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 9 ms 2900 KB Output is correct
21 Correct 10 ms 2900 KB Output is correct
22 Correct 465 ms 82480 KB Output is correct
23 Correct 201 ms 41448 KB Output is correct
24 Correct 243 ms 41356 KB Output is correct
25 Correct 100 ms 20924 KB Output is correct
26 Correct 94 ms 20948 KB Output is correct
27 Correct 453 ms 82380 KB Output is correct
28 Correct 87 ms 20820 KB Output is correct
29 Correct 113 ms 20928 KB Output is correct
30 Correct 118 ms 20924 KB Output is correct
31 Correct 119 ms 20820 KB Output is correct
32 Correct 9 ms 2900 KB Output is correct
33 Correct 0 ms 468 KB Output is correct
34 Correct 94 ms 20928 KB Output is correct
35 Correct 96 ms 20940 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 4 ms 692 KB Output is correct
39 Correct 4 ms 596 KB Output is correct
40 Correct 4 ms 596 KB Output is correct
41 Correct 4 ms 724 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 4 ms 596 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 5 ms 596 KB Output is correct
46 Correct 6 ms 596 KB Output is correct
47 Correct 6 ms 696 KB Output is correct
48 Correct 4 ms 596 KB Output is correct
49 Correct 5 ms 700 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 408 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Incorrect 1 ms 340 KB Output isn't correct
54 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 0 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 0 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 0 ms 340 KB Output is correct
13 Correct 0 ms 340 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 0 ms 468 KB Output is correct
17 Correct 0 ms 468 KB Output is correct
18 Correct 1 ms 468 KB Output is correct
19 Correct 1 ms 724 KB Output is correct
20 Correct 9 ms 2900 KB Output is correct
21 Correct 10 ms 2900 KB Output is correct
22 Correct 465 ms 82480 KB Output is correct
23 Correct 201 ms 41448 KB Output is correct
24 Correct 243 ms 41356 KB Output is correct
25 Correct 100 ms 20924 KB Output is correct
26 Correct 94 ms 20948 KB Output is correct
27 Correct 453 ms 82380 KB Output is correct
28 Correct 87 ms 20820 KB Output is correct
29 Correct 113 ms 20928 KB Output is correct
30 Correct 118 ms 20924 KB Output is correct
31 Correct 119 ms 20820 KB Output is correct
32 Correct 9 ms 2900 KB Output is correct
33 Correct 0 ms 468 KB Output is correct
34 Correct 94 ms 20928 KB Output is correct
35 Correct 96 ms 20940 KB Output is correct
36 Correct 5 ms 596 KB Output is correct
37 Correct 4 ms 596 KB Output is correct
38 Correct 4 ms 692 KB Output is correct
39 Correct 4 ms 596 KB Output is correct
40 Correct 4 ms 596 KB Output is correct
41 Correct 4 ms 724 KB Output is correct
42 Correct 4 ms 596 KB Output is correct
43 Correct 4 ms 596 KB Output is correct
44 Correct 4 ms 596 KB Output is correct
45 Correct 5 ms 596 KB Output is correct
46 Correct 6 ms 596 KB Output is correct
47 Correct 6 ms 696 KB Output is correct
48 Correct 4 ms 596 KB Output is correct
49 Correct 5 ms 700 KB Output is correct
50 Correct 0 ms 340 KB Output is correct
51 Correct 0 ms 408 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Incorrect 1 ms 340 KB Output isn't correct
54 Halted 0 ms 0 KB -