#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 smask = mask; ; smask = (smask - 1) & mask) {
smin(dp[i][mask], dp[i][smask] + dp[i][mask ^ smask]);
if (smask == 0) break;
}
}
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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
328 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
727 ms |
29768 KB |
Output is correct |
2 |
Correct |
641 ms |
29568 KB |
Output is correct |
3 |
Correct |
441 ms |
21772 KB |
Output is correct |
4 |
Correct |
58 ms |
8576 KB |
Output is correct |
5 |
Correct |
317 ms |
24472 KB |
Output is correct |
6 |
Correct |
54 ms |
8532 KB |
Output is correct |
7 |
Correct |
3 ms |
596 KB |
Output is correct |
8 |
Correct |
3 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
596 KB |
Output is correct |
2 |
Correct |
6 ms |
596 KB |
Output is correct |
3 |
Correct |
3 ms |
596 KB |
Output is correct |
4 |
Correct |
3 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1495 ms |
36220 KB |
Output is correct |
2 |
Correct |
1517 ms |
35924 KB |
Output is correct |
3 |
Correct |
948 ms |
27984 KB |
Output is correct |
4 |
Correct |
749 ms |
23196 KB |
Output is correct |
5 |
Correct |
170 ms |
11748 KB |
Output is correct |
6 |
Correct |
63 ms |
10224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3151 ms |
48652 KB |
Output is correct |
2 |
Correct |
3044 ms |
48724 KB |
Output is correct |
3 |
Correct |
3036 ms |
48644 KB |
Output is correct |
4 |
Correct |
2166 ms |
40612 KB |
Output is correct |
5 |
Correct |
1507 ms |
29260 KB |
Output is correct |
6 |
Correct |
321 ms |
12960 KB |
Output is correct |
7 |
Correct |
73 ms |
10344 KB |
Output is correct |