#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fastio ios::sync_with_stdio(false), cin.tie(0)
#pragma GCC optimize("Ofast", "unroll-loops")
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define int long long
#define lowbit(x) x&-x
const int maxn = 1e5 + 5;
const int INF = 1e18;
int dp[40][maxn];
int n, k, m;
vector<pll> adj[maxn];
signed main(void){
fastio;
cin>>n>>k>>m;
vector<int> a(k);
for(int i = 0; i < k; i++) cin>>a[i], a[i] --;
for(int i = 0; i < m; i++){
int a, b, w;
cin>>a>>b>>w;
a--, b--;
adj[a].eb(b, w);
adj[b].eb(a, w);
}
for(int i = 1; i < (1 << k); i++){
for(int j = 0; j < n; j++) dp[i][j] = INF;
}
for(int i = 0; i < k; i++) dp[(1 << i)][a[i]] = 0;
for(int mask = 0; mask < (1 << k); mask++){
for(int i = 0; i < n; i++){
for(int s = mask; s; s = (s - 1) & mask){
dp[mask][i] = min(dp[mask][i], dp[s][i] + dp[mask ^ s][i]);
}
}
priority_queue<pll, vector<pll>, greater<pll>> q;
for(int i = 0; i < n; i++) q.push({dp[mask][i], i});
while(!q.empty()){
auto [d, pos] = q.top(); q.pop();
if(dp[mask][pos] != d) continue;
for(auto [x, w] : adj[pos]){
if(dp[mask][x] > d + w){
dp[mask][x] = d + w;
q.push({dp[mask][x], x});
}
}
}
}
int ans = INF;
for(int i = 0; i < n; i++) ans = min(ans, dp[(1 << k) - 1][i]);
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
6748 KB |
Output is correct |
2 |
Correct |
1 ms |
6780 KB |
Output is correct |
3 |
Correct |
2 ms |
8792 KB |
Output is correct |
4 |
Correct |
2 ms |
14940 KB |
Output is correct |
5 |
Correct |
4 ms |
27228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
464 ms |
32540 KB |
Output is correct |
2 |
Correct |
409 ms |
31524 KB |
Output is correct |
3 |
Correct |
265 ms |
21868 KB |
Output is correct |
4 |
Correct |
74 ms |
21356 KB |
Output is correct |
5 |
Correct |
232 ms |
28584 KB |
Output is correct |
6 |
Correct |
49 ms |
19400 KB |
Output is correct |
7 |
Correct |
5 ms |
8916 KB |
Output is correct |
8 |
Correct |
4 ms |
6748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
15196 KB |
Output is correct |
2 |
Correct |
6 ms |
14996 KB |
Output is correct |
3 |
Correct |
5 ms |
14940 KB |
Output is correct |
4 |
Correct |
6 ms |
15196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
952 ms |
39084 KB |
Output is correct |
2 |
Correct |
999 ms |
38572 KB |
Output is correct |
3 |
Correct |
643 ms |
28036 KB |
Output is correct |
4 |
Correct |
513 ms |
33484 KB |
Output is correct |
5 |
Correct |
149 ms |
29324 KB |
Output is correct |
6 |
Correct |
51 ms |
29664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1774 ms |
51344 KB |
Output is correct |
2 |
Correct |
1795 ms |
51632 KB |
Output is correct |
3 |
Correct |
1802 ms |
49976 KB |
Output is correct |
4 |
Correct |
1079 ms |
40532 KB |
Output is correct |
5 |
Correct |
811 ms |
47744 KB |
Output is correct |
6 |
Correct |
182 ms |
41744 KB |
Output is correct |
7 |
Correct |
68 ms |
42288 KB |
Output is correct |