This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |