Submission #745755

#TimeUsernameProblemLanguageResultExecution timeMemory
745755vjudge1Cities (BOI16_cities)C++17
0 / 100
128 ms12436 KiB
#include <bits/stdc++.h>

// #define MULTI_TEST_CASE
// #define TEST_TEXT

using namespace std;

#define ll long long
#define MAX(a, b) (a) = max((a), (b))
#define MIN(a, b) (a) = min((a), (b))
#define all(a) (a).begin(), (a).end()
#define sortedpair(a, b) {min((a), (b)), max((a), (b))}

const ll MOD = 1e9+7;

struct edge {
    int to, w;
};

struct full_edge{
    int from, to, w;
    bool operator<(const full_edge &o){
        return w < o.w;
    }
};

int n, k, m;
vector<vector<edge>> v;
vector<full_edge> edges;
vector<int> p, siz;
vector<int> w;

int hol(int a){
    if(p[a] == -1)return a;
    return p[a] = hol(p[a]);
}

void unio(int a, int b){
    a = hol(a), b = hol(b);
    if(a == b)return;
    if(siz[a] > siz[b])swap(a, b);
    p[a] = b;
    siz[b] += siz[a];
}

void solve(){
    cin>>n>>k>>m;
    w.assign(k, 0);
    for(int&i:w){cin>>i; i--;}
    v.assign(n, {});
    p.assign(n, -1); siz.assign(n, 1);
    for(int i = 0; i < m; i++){
        int a, b, c; cin>>a>>b>>c; a--; b--;
        v[a].push_back({b, c});
        v[b].push_back({a, c});
        edges.push_back({a,b,c});
    }
    sort(all(edges));
    ll ans = 1e18;
    for(int mask = 0; mask < (1<<n); mask++){
        bool benne = true;
        for(int i : w){
            if(!((mask>>i)&1))benne = false;
        }
        if(!benne)continue;
        ll c = 0;
        p.assign(n, -1); siz.assign(n, 1);
        for(auto&i : edges){
            if(hol(i.from) != hol(i.to)){
                unio(i.from, i.to);
                c += i.w;
            }
        }
        benne = true;
        for(int i : w){
            for(int j : w){
                if(hol(i) != hol(j))
                    benne = false;
            }
        }
        MIN(ans, c);
    }
    cout<<ans<<endl;
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int _t = 1;
#ifdef MULTI_TEST_CASE
    cin >> _t;
#endif
    for(int _i = 0; _i < _t; _i++){
        #ifdef TEST_TEXT
        cout<<"Case #"<<_i+1<<": ";
        #endif
        solve();
    }
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...