# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
969402 | blackslex | Cities (BOI16_cities) | C++17 | 713 ms | 35692 KiB |
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;
using ll = long long;
using pii = pair<ll, ll>;
int n, m, k, x, y, z;
int main() {
scanf("%d %d %d", &n, &k, &m);
vector<int> c(k);
vector<vector<pii>> v(n + 5, vector<pii>());
for (auto &e: c) scanf("%d", &e);
while (m--) scanf("%d %d %d", &x, &y, &z), v[x].emplace_back(y, z), v[y].emplace_back(x, z);
vector<vector<int>> dp((1 << k) + 5, vector<int>(n + 5, 1e9));
for (int i = 0; i < k; i++) dp[1 << i][c[i]] = 0;
for (int i = 1; i < (1 << k); i++) {
for (int j = i; j; j = (j - 1) & i) {
for (int r = 1; r <= n; r++) {
dp[i][r] = min(dp[i][r], dp[j][r] + dp[i ^ j][r]);
}
}
auto dijk = [&] (vector<int> &curd) {
priority_queue<pii, vector<pii>, greater<pii>> pq;
for (int j = 1; j <= n; j++) pq.emplace(curd[j], j);
while (!pq.empty()) {
auto [nd, nn] = pq.top(); pq.pop();
for (auto &[tn, td]: v[nn]) if (curd[tn] > curd[nn] + td) pq.emplace(curd[tn] = curd[nn] + td, tn);
}
};
dijk(dp[i]);
}
printf("%d", *min_element(dp[(1 << k) - 1].begin(), dp[(1 << k) - 1].end()));
}
Compilation message (stderr)
# | 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... |