답안 #640682

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
640682 2022-09-15T05:51:15 Z devariaota Cities (BOI16_cities) C++17
22 / 100
156 ms 4948 KB
#include <bits/stdc++.h>
#define ll long long

using namespace std;

vector<int>p(22);

int par(int x){
    return ((p[x] == x) ? x : p[x] = par(p[x]));
}

void join(int a, int b){
    a = par(a);
    b = par(b);

    p[b] = a;
}

bool sameSet(int a, int b){
    return (par(a) == par(b));
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int n, k, m; cin >> n >> k >> m;
    vector<tuple<ll, ll, ll>>edges(m);
    vector<int>spec(k);
    if(n > 20) return 0;

    int all = (1<<(n + 1));
    int x = 0;

    for(int i = 0; i < k; i++){
        cin >> spec[i];
        x |= (1<<spec[i]);
    }

    for(int i = 0; i < m; i++){
        ll u, v, w; cin >> u >> v >> w;
        edges[i] = {w, u, v};
    }

    sort(edges.begin(), edges.end());
    ll ans = 1e18;

    for(int mask = 1; mask < all; mask++){
        if((mask&x) != x) continue;
        iota(p.begin(), p.end(), 0);
        ll cost = 0;
        for(int i = 0; i < m; i++){
            auto &[w, u, v] = edges[i];
            if(((mask>>u)&1) && ((mask>>v)&1) && !sameSet(u, v)){
                cost += w;
                join(u, v);
            }
        }
        
        bool ok = true;
        for(int i = 0; i < k - 1; i++) ok &= sameSet(spec[i], spec[i + 1]);
        if(ok) ans = min(ans, cost);
    }

    cout << ans << '\n';

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 156 ms 308 KB Output is correct
3 Correct 87 ms 296 KB Output is correct
4 Correct 51 ms 308 KB Output is correct
5 Correct 26 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 0 ms 340 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 2 ms 4948 KB Output isn't correct
2 Halted 0 ms 0 KB -