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;
const int N = 100100;
const int M = 200200;
const ll INF = 1e15;
int n, m, k;
vector<pair<int, ll>> g[N];
int special[5];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k >> m;
for (int i = 0; i < k; i++) {
cin >> special[i];
special[i]--;
}
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
ll dp[(1 << k)][n] = {};
for (int i = 1; i < (1 << k); i++)
for (int j = 0; j < n; j++)
dp[i][j] = INF;
for (int i = 0; i < k; i++)
dp[(1 << i)][special[i]] = 0;
for (int msk = 1; msk < (1 << k); msk++) {
for (int v = 0; v < n; v++)
for (int sub = msk; sub; sub = (msk & sub - 1))
dp[msk][v] = min(dp[msk][v], dp[sub][v] + dp[(msk ^ sub)][v]);
set<pair<ll, int>> st;
for (int i = 0; i < n; i++)
st.insert({dp[msk][i], i});
while (!st.empty()) {
int x = st.begin()->second;
st.erase(st.begin());
if (dp[msk][x] == INF) break;
for (auto [to, w] : g[x]) {
if (dp[msk][to] > dp[msk][x] + w) {
st.erase({dp[msk][to], to});
dp[msk][to] = dp[msk][x] + w;
st.insert({dp[msk][to], to});
}
}
}
}
cout << *min_element(dp[(1 << k) - 1], dp[(1 << k) - 1] + n);
}
Compilation message (stderr)
cities.cpp: In function 'int main()':
cities.cpp:43:55: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
43 | for (int sub = msk; sub; sub = (msk & sub - 1))
| ~~~~^~~
# | 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... |