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>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
struct edge {
int from, to, wei;
bool operator < (const edge &other) const {
return wei > other.wei;
}
};
const int maxn = 20;
const int inf = 1e16 + 42;
int n, k, m;
vector<edge> graph[maxn];
bool special[maxn];
int try_for(int mask) {
vector<bool> vis(n, false);
int start = -1;
for(int i = 0; i < n; i++) {
if(mask & (1 << i)) {
start = i;
break;
}
}
priority_queue<edge> q;
q.push({-1, start, 0});
int res = 0;
while(!q.empty()) {
edge e = q.top();
q.pop();
//cout << e.from << " " << e.to << " " << e.wei << "\n";
if(vis[e.to]) {
continue;
}
vis[e.to] = true;
res += e.wei;
for(edge nei : graph[e.to]) {
//cout << nei.to;
if(mask & (1 << nei.to)) {
//cout << " +1";
if(!vis[nei.to]) {
//cout << " +1";
q.push(nei);
}
}
//cout << "\n";
}
}
for(int i = 0; i < n; i++) {
if(mask & (1 << i)) {
if(!vis[i]) {
return inf;
}
}
}
return res;
}
void solve() {
cin >> n >> k >> m;
for(int i = 0; i < k; i++) {
int x;
cin >> x;
x--;
special[x] = true;
}
for(int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
a--, b--;
graph[a].pb({a, b, c});
graph[b].pb({b, a, c});
}
int ans = inf;
for(int mask = 0; mask < (1 << n); mask++) {
bool ok = true;
for(int i = 0; i < n; i++) {
if(special[i] && ((mask & (1 << i)) == 0)) {
ok = false;
break;
}
}
if(!ok) {
continue;
}
int res = try_for(mask);
if(ans > res) {
ans = res;
}
}
cout << ans << "\n";
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
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... |