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>
#define ll long long
#define ld long double
#define sp ' '
#define en '\n'
#define smin(a, b) a = min(a, b)
#define smax(a, b) a = max(a, b)
using namespace std;
const int N = 3e5 + 5;
const ll linf = 1e18;
const int mod = 1e9 + 7;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n, k, m;
cin >> n >> k >> m;
int K = 1 << k;
vector<vector<ll>> dp(n, vector<ll>(K, linf));
vector<int> imp(k);
for (int i = 0; i < k; i++) {
cin >> imp[i];
imp[i] -= 1;
dp[imp[i]][1 << i] = 0;
}
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
u -= 1; v -= 1;
g[u].push_back({v, w});
g[v].push_back({u, w});
}
for (int mask = 0; mask < K; mask++) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < k; j++) {
if ((1 << j) & mask) smin(dp[i][mask], dp[i][mask ^ (1 << j)] + dp[i][1 << j]);
}
}
priority_queue<pair<ll, int>> pq;
for (int i = 0; i < n; i++) {
if (dp[i][mask] == linf) continue;
pq.push({-dp[i][mask], i});
}
while (!pq.empty()) {
ll d = -pq.top().first;
int u = pq.top().second;
pq.pop();
if (d > dp[u][mask]) continue;
for (auto p : g[u]) {
ll w = d + p.second;
if (dp[p.first][mask] > w) {
dp[p.first][mask] = w;
pq.push({-w, p.first});
}
}
}
}
ll ans = linf;
for (int i = 0; i < n; i++) smin(ans, dp[i][K - 1]);
cout << ans << en;
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... |