Submission #19761405


Source Code Expand

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
bool chmin(int& a, int b){ if(a > b){ a = b; return 1; } return 0; }
const int INF = 0x3fffffff;

int main(){
    int N, M;
    cin >> N >> M;
    vector<vector<int>> g(N);
    for(int i = 0; i < M; i++){
        int A, B;
        cin >> A >> B;
        A--; B--;
        g[A].push_back(B);
        g[B].push_back(A);
    }
    int K;
    cin >> K;
    vector<int> C(K);
    for(int& c : C){
        cin >> c;
        c--;
    }
    
    auto BFS = [&](int s){
        vector cost(N, INF);
        cost[s] = 0;
        queue<int> q;
        q.push(s);
        while(q.size()){
            int x = q.front();
            q.pop();
            for(int y : g[x]) if(chmin(cost[y], cost[x] + 1)) q.push(y);
        }
        for(int i = 0; i < K; i++) cost[i] = cost[C[i]];
        cost.resize(K);
        return cost;
    };
    
    vector<vector<int>> cost(K);
    for(int i = 0; i < K; i++) cost[i] = BFS(C[i]);
    
    vector dp(1 << K, vector(K, INF));
    for(int i = 0; i < K; i++) dp[1 << i][i] = 1;
    for(int bit = 1; bit < 1 << K; bit++) for(int i = 0; i < K; i++) if(bit & 1 << i){
        const int bit2 = bit ^ 1 << i;
        for(int j = 0; j < K; j++) if(bit2 & 1 << j) chmin(dp[bit][i], dp[bit2][j] + cost[i][j]);
    }
    
    int ans = *min_element(dp.back().begin(), dp.back().end());
    if(ans == INF) ans = -1;
    cout << ans << endl;
}

Submission Info

Submission Time
Task E - Magical Ornament
User tatyam
Language C++ (GCC 9.2.1)
Score 500
Code Size 1511 Byte
Status AC
Exec Time 241 ms
Memory 29052 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 40
Set Name Test Cases
Sample 01_sample.txt, 02_sample.txt, 03_sample.txt
All 01_sample.txt, 02_sample.txt, 03_sample.txt, 04_hand.txt, 05_hand.txt, 06_small.txt, 07_small.txt, 08_small.txt, 09_small.txt, 10_small.txt, 11_small.txt, 12_small.txt, 13_small.txt, 14_small.txt, 15_small.txt, 16_small.txt, 17_small.txt, 18_small.txt, 19_small.txt, 20_small.txt, 21_large.txt, 22_large.txt, 23_large.txt, 24_large.txt, 25_large.txt, 26_max.txt, 27_max.txt, 28_max.txt, 29_max.txt, 30_max.txt, 31_max.txt, 32_tree.txt, 33_tree.txt, 34_tree.txt, 35_path.txt, 36_path.txt, 37_path.txt, 38_star.txt, 39_star.txt, 40_star.txt
Case Name Status Exec Time Memory
01_sample.txt AC 4 ms 3540 KiB
02_sample.txt AC 2 ms 3424 KiB
03_sample.txt AC 3 ms 3488 KiB
04_hand.txt AC 6 ms 3572 KiB
05_hand.txt AC 6 ms 5784 KiB
06_small.txt AC 2 ms 3528 KiB
07_small.txt AC 2 ms 3536 KiB
08_small.txt AC 3 ms 3580 KiB
09_small.txt AC 3 ms 3420 KiB
10_small.txt AC 2 ms 3620 KiB
11_small.txt AC 2 ms 3476 KiB
12_small.txt AC 2 ms 3436 KiB
13_small.txt AC 2 ms 3580 KiB
14_small.txt AC 2 ms 3576 KiB
15_small.txt AC 3 ms 3424 KiB
16_small.txt AC 1 ms 3576 KiB
17_small.txt AC 2 ms 3492 KiB
18_small.txt AC 2 ms 3656 KiB
19_small.txt AC 2 ms 3420 KiB
20_small.txt AC 3 ms 3484 KiB
21_large.txt AC 94 ms 18656 KiB
22_large.txt AC 129 ms 18668 KiB
23_large.txt AC 31 ms 6884 KiB
24_large.txt AC 63 ms 6728 KiB
25_large.txt AC 60 ms 6860 KiB
26_max.txt AC 230 ms 28828 KiB
27_max.txt AC 230 ms 28792 KiB
28_max.txt AC 222 ms 28364 KiB
29_max.txt AC 227 ms 27380 KiB
30_max.txt AC 190 ms 23124 KiB
31_max.txt AC 155 ms 18564 KiB
32_tree.txt AC 221 ms 28692 KiB
33_tree.txt AC 221 ms 28748 KiB
34_tree.txt AC 226 ms 28804 KiB
35_path.txt AC 241 ms 28428 KiB
36_path.txt AC 237 ms 28556 KiB
37_path.txt AC 222 ms 28504 KiB
38_star.txt AC 170 ms 29032 KiB
39_star.txt AC 188 ms 29008 KiB
40_star.txt AC 196 ms 29052 KiB