#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
using state = pair<ll, int>;
const ll N = 1e5+5, INF = 1e18;
ll ans = INF;
int n, m, k;
vector<pair<int, ll>> adj[N];
vector<int> must;
ll dp[16][N];
int main() {
ios::sync_with_stdio(0); cin.tie(0);
cin >> n >> k >> m;
for(int i = 0; i < k; i++) {
int x; cin >> x;
must.push_back(x);
}
k--;
for(int i = 0; i < m; i++) {
ll u, v, w; cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for(int mask = 0; mask < 16; mask++) {
for(int i = 1; i <= n; i++) {
if(mask == 0) dp[mask][i] = 0;
else dp[mask][i] = INF;
}
}
for(int i = 0; i < k; i++)
dp[(1<<i)][must[i]] = 0;
for(int mask = 0; mask < (1<<k); mask++) {
priority_queue<state, vector<state>, greater<state>> pq;
for(int i = 1; i <= n; i++) {
for(int sub = mask; sub > 0; sub = (sub - 1) & mask)
dp[mask][i] = min(dp[mask][i], dp[sub][i] + dp[mask^sub][i]);
pq.push({dp[mask][i], i});
}
while(!pq.empty()) {
ll dt = pq.top().first; int i = pq.top().second; pq.pop();
if(dt > dp[mask][i]) continue;
for(auto& it : adj[i]) {
if(dp[mask][it.first] > dp[mask][i] + it.second) {
dp[mask][it.first] = dp[mask][i] + it.second;
pq.push({dp[mask][it.first], it.first});
}
}
}
}
cout << dp[(1<<k)-1][must[k]] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2816 KB |
Output is correct |
2 |
Correct |
2 ms |
2816 KB |
Output is correct |
3 |
Correct |
2 ms |
2816 KB |
Output is correct |
4 |
Correct |
2 ms |
2816 KB |
Output is correct |
5 |
Correct |
3 ms |
2816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
36448 KB |
Output is correct |
2 |
Correct |
376 ms |
35872 KB |
Output is correct |
3 |
Correct |
230 ms |
26400 KB |
Output is correct |
4 |
Correct |
83 ms |
15272 KB |
Output is correct |
5 |
Correct |
243 ms |
35920 KB |
Output is correct |
6 |
Correct |
85 ms |
15224 KB |
Output is correct |
7 |
Correct |
4 ms |
3072 KB |
Output is correct |
8 |
Correct |
3 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
3072 KB |
Output is correct |
2 |
Correct |
5 ms |
3072 KB |
Output is correct |
3 |
Correct |
6 ms |
2944 KB |
Output is correct |
4 |
Correct |
4 ms |
3072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
755 ms |
36396 KB |
Output is correct |
2 |
Correct |
645 ms |
35856 KB |
Output is correct |
3 |
Correct |
411 ms |
26268 KB |
Output is correct |
4 |
Correct |
397 ms |
27584 KB |
Output is correct |
5 |
Correct |
144 ms |
18448 KB |
Output is correct |
6 |
Correct |
79 ms |
17716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1230 ms |
36328 KB |
Output is correct |
2 |
Correct |
1225 ms |
36356 KB |
Output is correct |
3 |
Correct |
1188 ms |
36044 KB |
Output is correct |
4 |
Correct |
794 ms |
26396 KB |
Output is correct |
5 |
Correct |
669 ms |
27516 KB |
Output is correct |
6 |
Correct |
189 ms |
18484 KB |
Output is correct |
7 |
Correct |
84 ms |
17528 KB |
Output is correct |