This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3")
#include<bits/stdc++.h>
//#include <grader.h>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define ll long long
#define ld long double
#define st first
#define nd second
#define pb push_back
#define INF INT_MAX
#define db double
using namespace std;
const int N = 1e5 + 10;
int n, k, m;
ll dp[N][32];
int city[5];
vector<pll> adj[N];
priority_queue<pll> pq;
void solve() {
int n, k, m;cin >> n >> k >> m;
for (int s = 0; s < (1<<k); ++s) for (int i = 1; i <= n; ++i) dp[i][s] = 1e18;
for (int i = 0, u; i < k; ++i) {
cin >> u;
dp[u][(1<<i)] = 0;
}
for (int i = 1; i <= m; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].pb({v, w}); adj[v].pb({u, w});
}
for (int s = 1; s < (1<<k); ++s) {
for (int j = 0; j < k; ++j) {
if (s&(1<<j))
for (int i = 1; i <= n; ++i)
dp[i][s] = min(dp[i][s], dp[i][(1<<j)] + dp[i][s^(1<<j)]);
}
for (int i = 1; i <= n; ++i) pq.push({-dp[i][s], i});
while (!pq.empty()) {
int a = pq.top().nd; pq.pop();
for (auto [b, w] : adj[a]) {
if (dp[b][s] > dp[a][s] + w) {
dp[b][s] = dp[a][s] + w;
pq.push({-dp[b][s], b});
}
}
}
}
ll ans = LLONG_MAX;
for (int i = 1; i <= n; ++i) ans = min(ans, dp[i][(1<<k)-1]);
cout << ans << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(NULL);
int t = 1;
// cin >> t;
while (t--) {
solve();
}
return 0;
}
Compilation message (stderr)
cities.cpp: In function 'void solve()':
cities.cpp:44:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
44 | for (auto [b, w] : adj[a]) {
| ^
# | 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... |