Submission #640645

#TimeUsernameProblemLanguageResultExecution timeMemory
640645devariaotaCities (BOI16_cities)C++17
0 / 100
136 ms4948 KiB
#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); if(n > 20) return 0; int all = (1<<(n + 1)); int x = 0; for(int i = 0; i < k; i++){ int a; cin >> a; x |= (1<<a); } 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); } } ans = min(ans, cost); } cout << ans << '\n'; 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...