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 <iostream>
#include <algorithm>
#include <vector>
#include <set>
#include <cstring>
#include <queue>
#include <map>
#include <cmath>
#include <iomanip>
using namespace std;
#define ff first
#define sc second
#define pb push_back
#define ll long long
#define pll pair<ll, int>
#define pii pair <int, int>
#define ull unsigned long long
// #define int long long
// #define int unsigned long long
const ll inf = 1e18 + 7;
const ll weirdMod = 998244353;
vector<vector<pii>> G;
void solve() {
int n, k, m;
cin >> n >> k >> m; G.resize(n + 1);
ll dp[n + 1][1 << k]; int f[k];
for (int i = 1; i <= n; i++)
for (int j = 0; j < (1 << k); j++) dp[i][j] = inf;
for (int i = 0; i < k; i++) {
cin >> f[i]; dp[f[i]][1 << i] = 0;
} for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
G[x].pb({y, z}); G[y].pb({x, z});
} for (int msk = 1; msk < (1 << k); msk++) {
for (int j = 0; j < k; j++) {
if (!(msk & (1 << j))) continue;
int msk2 = (msk ^ (1 << j));
for (int i = 1; i <= n; i++)
dp[i][msk] = min(dp[i][msk], dp[i][1 << j] + dp[i][msk2]);
} priority_queue<pll, vector<pll>, greater<pll>> q;
for (int i = 1; i <= n; i++)
q.push({dp[i][msk], i});
while (q.size()) {
auto [w, u] = q.top(); q.pop();
if (dp[u][msk] != w) continue;
for (auto [v, wgt] : G[u]) {
if (dp[v][msk] > dp[u][msk] + wgt)
q.push({dp[v][msk] = dp[u][msk] + wgt, v});
}
}
} ll mn = inf;
for (int i = 1; i <= n; i++) {
mn = min(mn, dp[i][(1 << k) - 1]);
} cout << mn;
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
int t = 1;
// cin >> t;
while (t--) {
solve();
cout << '\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... |