#include <bits/stdc++.h>
#define FOR(i, x, y) for (int i = x; i < y; i++)
typedef long long ll;
using namespace std;
int important[5];
ll dp[1 << 5][100001];
bool visited[100001];
vector<pair<ll, int>> graph[100001];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, k, m;
cin >> n >> k >> m;
FOR(i, 0, k) cin >> important[i];
while (m--) {
int a, b, c;
cin >> a >> b >> c;
graph[a].push_back({c, b});
graph[b].push_back({c, a});
}
memset(dp, 0x3f, sizeof(dp));
FOR(i, 0, k) dp[1 << i][important[i]] = 0;
FOR(mask, 1, 1 << k) {
priority_queue<pair<ll, int>> pq;
for (int i = mask; i; i = mask & (i - 1)) {
if ((mask ^ i) > i) continue;
FOR(j, 1, n + 1) dp[mask][j] = min(dp[mask][j], dp[i][j] + dp[mask ^ i][j]);
}
fill(visited + 1, visited + n + 1, false);
FOR(i, 1, n + 1) if (dp[mask][i] != 0x3f3f3f3f3f3f3f3f) pq.push({-dp[mask][i], i});
while (pq.size()) {
ll cost = pq.top().first;
int curr = pq.top().second;
pq.pop();
if (!visited[curr]) {
visited[curr] = true;
for (pair<ll, int> i : graph[curr]) if (i.first - cost < dp[mask][i.second]) {
dp[mask][i.second] = i.first - cost;
pq.push({cost - i.first, i.second});
}
}
}
}
cout << *min_element(dp[(1 << k) - 1] + 1, dp[(1 << k) - 1] + n + 1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
27776 KB |
Output is correct |
2 |
Correct |
19 ms |
27776 KB |
Output is correct |
3 |
Correct |
19 ms |
27776 KB |
Output is correct |
4 |
Correct |
19 ms |
27776 KB |
Output is correct |
5 |
Correct |
19 ms |
27776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
610 ms |
48784 KB |
Output is correct |
2 |
Correct |
584 ms |
48312 KB |
Output is correct |
3 |
Correct |
325 ms |
38968 KB |
Output is correct |
4 |
Correct |
117 ms |
40312 KB |
Output is correct |
5 |
Correct |
320 ms |
46604 KB |
Output is correct |
6 |
Correct |
101 ms |
40120 KB |
Output is correct |
7 |
Correct |
21 ms |
28032 KB |
Output is correct |
8 |
Correct |
20 ms |
27904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
27904 KB |
Output is correct |
2 |
Correct |
23 ms |
27904 KB |
Output is correct |
3 |
Correct |
21 ms |
27904 KB |
Output is correct |
4 |
Correct |
21 ms |
27904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1174 ms |
48952 KB |
Output is correct |
2 |
Correct |
1114 ms |
48300 KB |
Output is correct |
3 |
Correct |
714 ms |
38992 KB |
Output is correct |
4 |
Correct |
635 ms |
45960 KB |
Output is correct |
5 |
Correct |
214 ms |
41608 KB |
Output is correct |
6 |
Correct |
105 ms |
42360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2335 ms |
48988 KB |
Output is correct |
2 |
Correct |
2241 ms |
49040 KB |
Output is correct |
3 |
Correct |
2242 ms |
48400 KB |
Output is correct |
4 |
Correct |
1543 ms |
39016 KB |
Output is correct |
5 |
Correct |
1146 ms |
45912 KB |
Output is correct |
6 |
Correct |
288 ms |
41568 KB |
Output is correct |
7 |
Correct |
120 ms |
42104 KB |
Output is correct |