#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
664 ms |
25420 KB |
Output is correct |
2 |
Correct |
687 ms |
25372 KB |
Output is correct |
3 |
Correct |
398 ms |
19672 KB |
Output is correct |
4 |
Correct |
54 ms |
5132 KB |
Output is correct |
5 |
Correct |
293 ms |
20084 KB |
Output is correct |
6 |
Correct |
50 ms |
5052 KB |
Output is correct |
7 |
Correct |
3 ms |
468 KB |
Output is correct |
8 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
596 KB |
Output is correct |
2 |
Correct |
5 ms |
596 KB |
Output is correct |
3 |
Correct |
4 ms |
468 KB |
Output is correct |
4 |
Correct |
5 ms |
416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1461 ms |
31972 KB |
Output is correct |
2 |
Correct |
1546 ms |
31760 KB |
Output is correct |
3 |
Correct |
948 ms |
25860 KB |
Output is correct |
4 |
Correct |
837 ms |
19040 KB |
Output is correct |
5 |
Correct |
162 ms |
7860 KB |
Output is correct |
6 |
Correct |
60 ms |
6700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3135 ms |
44712 KB |
Output is correct |
2 |
Correct |
3109 ms |
44468 KB |
Output is correct |
3 |
Correct |
3166 ms |
44216 KB |
Output is correct |
4 |
Correct |
2097 ms |
38600 KB |
Output is correct |
5 |
Correct |
1492 ms |
25128 KB |
Output is correct |
6 |
Correct |
253 ms |
9204 KB |
Output is correct |
7 |
Correct |
75 ms |
6860 KB |
Output is correct |