#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 |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
136 ms |
312 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |