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 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(!((mask>>i.from)&1) || !((mask>>i.to)&1))continue;
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;
}
}
if(benne)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 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... |