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;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
const long long inf = 1e18;
int n, k, m; cin >> n >> k >> m;
vector<int> city(k);
for (int &u : city) {
cin >> u; --u;
}
vector<vector<array<int, 2>>> g(n);
while (m--) {
int u, v, c; cin >> u >> v >> c; --u, --v;
g[u].push_back({v, c});
g[v].push_back({u, c});
}
auto dijkstra = [&](int s) {
vector<long long> d(n, inf);
using T = pair<long long, int>;
priority_queue<T, vector<T>, greater<T>> pq;
pq.push({d[s] = 0, s});
while (pq.size()) {
auto [c, u] = pq.top(); pq.pop();
if (c != d[u]) {
continue;
}
for (auto [v, w] : g[u]) {
if (d[v] > c + w) {
pq.push({d[v] = c + w, v});
}
}
}
return d;
};
vector<vector<long long>> d(k);
for (int i = 0; i < k; ++i) {
d[i] = dijkstra(city[i]);
}
vector<long long> dp(1 << k, inf), c(1 << k);
for (int i = 0; i < k; ++i) {
dp[1 << i] = 0;
}
for (int mask = 1; mask < (1 << k); ++mask) {
if (__builtin_popcount(mask) > 1) {
c[mask] = inf;
for (int i = 0; i < n; ++i) {
long long w = 0;
for (int j = 0; j < k; ++j) {
if (mask >> j & 1) {
w += d[j][i];
}
}
c[mask] = min(c[mask], w);
}
}
}
for (int mask = 0; mask < 1 << k; ++mask) {
for (int smask = mask; smask; smask = (smask - 1) & mask) {
int x = mask ^ smask;
long long y = inf;
for (int i = 0; i < k; ++i) {
if (x >> i & 1) {
y = min(y, c[smask + (1 << i)]);
}
}
dp[mask] = min(dp[mask], dp[x] + y);
}
}
cout << dp.back();
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... |