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 <queue>
#include <vector>
using namespace std;
long long int f[33][100005], a[6];
vector<pair<int, long long int>> g[100005];
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n, k, m;
cin >> n >> k >> m;
for (int i = 0; i < k; i++) {
cin >> a[i];
}
for (int i = 1; i <= m; i++) {
int u, v, x;
cin >> u >> v >> x;
g[u].push_back({v, x});
g[v].push_back({u, x});
}
for (int i = 1; i < (1 << k); i++) {
int h = __builtin_popcount(i);
priority_queue<pair<long long int, int>> q;
if (h == 1) {
for (int j = 1; j <= n; j++) f[i][j] = 1e18;
for (int j = 0; j < k; j++) {
if (i & (1 << j)) {
f[i][a[j]] = 0;
q.push({-f[i][a[j]], a[j]});
}
}
}
else {
for (int j = 1; j <= n; j++) {
f[i][j] = 1e18;
}
for (int j = 1; j <= n; j++) {
for (int k = i; k > 0; k = k & (k - 1)) {
f[i][j] = min(f[i][j], f[k][j] + f[(i ^ k)][j]);
}
q.push({-f[i][j], j});
}
}
while (!q.empty()) {
pair<long long int, int> p = q.top();
q.pop();
long long int x = -p.first, u = p.second;
if (f[i][u] != x) continue;
for (auto w : g[u]) {
if (f[i][w.first] > f[i][u] + w.second) {
f[i][w.first] = x + w.second;
q.push({-f[i][w.first], w.first});
}
}
}
}
long long int res = 1e18;
for (int i = 1; i <= n; i++) {
res = min(res, f[(1 << k) - 1][i]);
}
cout << res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |