#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N = (int) 2e5 + 7;
const int K = 5;
const int INF = (ll) 1e18;
int dp[1 << K][N], special[N];
vector<pair<int, int>> g[N];
bool vis[N];
int n, k, m;
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k >> m;
vector<int> cities(k + 1);
for (int i = 1; i <= k; i++) {
cin >> cities[i];
special[cities[i]] = i;
}
for (int i = 1; i <= m; i++) {
int x, y, z;
cin >> x >> y >> z;
g[x].push_back({y, z});
g[y].push_back({x, z});
}
for (int mask = 0; mask < (1 << k); mask++) {
for (int i = 0; i <= n; i++) {
dp[mask][i] = INF;
}
}
for (int i = 1; i <= k; i++) {
dp[(1 << (i - 1))][cities[i]] = 0;
}
for (int i = 1; i <= n; i++) {
dp[0][i] = 0;
}
for (int mask = 0; mask < (1 << k); mask++) {
for (int i = 1; i <= n; i++) {
for (int submask = mask & (mask - 1); submask > 0; submask = (submask - 1) & submask) {
ll newmask = (mask ^ submask);
if (special[i]) {
newmask ^= (1 << (special[i] - 1));
}
dp[mask][i] = min(dp[mask][i], dp[newmask][i] + dp[submask][i]);
}
}
priority_queue<pair<int, int>> pq;
for (int i = 0; i <= n; i++) {
vis[i] = 0;
pq.push({-dp[mask][i], i});
}
while (!pq.empty()) {
pair<int, int> u = pq.top();
pq.pop();
if (vis[u.second]) {
continue;
}
vis[u.second] = 1;
for (auto &v : g[u.second]) {
if (dp[mask][u.second] + v.second < dp[mask][v.first]) {
dp[mask][v.first] = dp[mask][u.second] + v.second;
pq.push({-dp[mask][v.first], v.first});
}
}
}
}
ll ret = INF;
for (int i = 0; i <= n; i++) {
ret = min(ret, dp[(1 << k) - 1][i]);
}
cout << ret;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
616 ms |
32596 KB |
Output is correct |
2 |
Correct |
636 ms |
32124 KB |
Output is correct |
3 |
Correct |
523 ms |
22544 KB |
Output is correct |
4 |
Correct |
84 ms |
17688 KB |
Output is correct |
5 |
Correct |
423 ms |
29388 KB |
Output is correct |
6 |
Correct |
73 ms |
17672 KB |
Output is correct |
7 |
Correct |
8 ms |
5260 KB |
Output is correct |
8 |
Correct |
6 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
5460 KB |
Output is correct |
2 |
Incorrect |
9 ms |
5456 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1195 ms |
38856 KB |
Output is correct |
2 |
Correct |
982 ms |
38364 KB |
Output is correct |
3 |
Incorrect |
703 ms |
28836 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2036 ms |
51616 KB |
Output is correct |
2 |
Incorrect |
2056 ms |
51632 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |