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;
typedef long long ll;
using state = pair<ll, int>;
const ll N = 1e5+5, INF = 1e18;
ll ans = INF;
int n, m, k;
vector<pair<int, ll>> adj[N];
vector<int> must;
ll dp[16][N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k >> m;
for(int i = 0; i < k; i++) {
int x; cin >> x;
must.push_back(x);
}
k--;
for(int i = 0; i < m; i++) {
ll u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for(int mask = 0; mask < 16; mask++) {
for(int i = 1; i <= n; i++) {
if(mask == 0) dp[mask][i] = 0;
else dp[mask][i] = INF;
}
}
for(int i = 0; i < k; i++)
dp[(1<<i)][must[i]] = 0;
for(int mask = 0; mask < (1<<k); mask++) {
priority_queue<state, vector<state>, greater<state>> pq;
for(int i = 1; i <= n; i++) {
for(int sub = mask; sub > 0; sub = (sub - 1) & mask)
dp[mask][i] = min(dp[mask][i], dp[sub][i] + dp[mask^sub][i]);
pq.push({dp[mask][i], i});
}
while(!pq.empty()) {
ll dt = pq.top().first; int i = pq.top().second; pq.pop();
if(dt > dp[mask][i]) continue;
for(auto& it : adj[i]) {
if(dp[mask][it.first] > dp[mask][i] + it.second) {
dp[mask][it.first] = dp[mask][i] + it.second;
pq.push({dp[mask][it.first], it.first});
}
}
}
}
cout << dp[(1<<k)-1][must[k]] << '\n';
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... |