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 f first
#define s second
#define int long long
#define pii pair <int, int>
#define pb push_back
const int N = 2e5 + 5;
int t,n,k,m,sp[N],a[N],dp[33][N],idx[N],inf = 1e15;
vector < pii > v[N];
main() {
std::ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
cin>>n>>k>>m;
for (int i = 0; i < k; i++) {
cin>>sp[i];
sp[i]--;
}
sort(sp, sp + k);
k = unique(sp, sp + k) - sp;
for (int i = 0; i < n; i++) idx[i] = -1;
for (int i = 0; i < k; i++) {
idx[sp[i]] = i;
}
for (int i = 1; i <= m; i++) {
int a, b, c;
cin>>a>>b>>c;
a--; b--;
v[a].pb({b, c});
v[b].pb({a, c});
}
for (int i = 0; i < (1<<k); i++) {
for (int j = 0; j < n ;j++) {
dp[i][j] = inf;
}
}
int ans = inf;
for (int i = 0; i < k; i++) {
dp[(1<<i)][sp[i]] = 0; // dp[mask][i] --- > minimum spanning tree rooted on vertex i, visited (mask) special vertices
}
for (int mask = 1; mask < (1<<k); mask++) {
priority_queue <pii> q;
for (int i = 0; i < n; i++) {
if (idx[i] != -1 && mask&(1<<idx[i])) dp[mask][i] = min(dp[mask][i], dp[mask ^ (1<<idx[i])][i]);
for (int sub = (mask - 1); sub >= 1; sub = (sub - 1) & mask) {
dp[mask][i] = min(dp[mask][i], dp[sub][i] + dp[mask ^ sub][i]);
}
q.push({-dp[mask][i], i});
}
while (!q.empty()) {
int x = -q.top().f;
int y = q.top().s;
q.pop();
if (x > dp[mask][y]) continue;
for (pii sth : v[y]) {
int to = sth.f; int c = sth.s;
if (dp[mask][to] > x + c) {
dp[mask][to] = x + c;
q.push({-dp[mask][to], to});
}
}
}
if (mask == (1<<k) - 1) for (int x = 0; x < n; x++) ans = min(ans, dp[mask][x]);
}
cout<<ans<<"\n";
}
Compilation message (stderr)
cities.cpp:11:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
11 | main() {
| ^~~~
# | 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... |