#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define pii pair<int, int>
#define pll pair<ll, ll>
#define pdd pair<double, double>
#define ALL(x) x.begin(), x.end()
#define vi vector<int>
#define vl vector<ll>
#define SZ(x) (int)x.size()
#define CASE int t;cin>>t;for(int ca=1;ca<=t;ca++)
#define IOS ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;
const int MAX = 1 << 20, MOD = 1e9 + 7;
ll dp[MAX][32];
vector<pll> edge[MAX];
void solve(){
int n, k, m;
cin >> n >> k >> m;
for(int i = 1;i <= n;i++)
for(int j = 0;j < (1 << k);j++)
dp[i][j] = 2e18;
for(int i = 0;i < k;i++){
int x;
cin >> x;
dp[x][1 << i] = 0;
}
for(int i = 1;i <= m;i++){
int u, v, w;
cin >> u >> v >> w;
edge[u].PB(MP(v, w));
edge[v].PB(MP(u, w));
}
for(int i = 1;i < (1 << k);i++){
for(int j = 0;j < k;j++){
if(i >> j & 1)
for(int l = 1;l <= n;l++)
dp[l][i] = min(dp[l][i], dp[l][1 << j] + dp[l][i ^ (1 << j)]);
}
priority_queue<pll> pq;
for(int j = 1;j <= n;j++)
pq.push(MP(-dp[j][i], j));
while(!pq.empty()){
auto now = pq.top();
pq.pop();
now.F = -now.F;
if(now.F != dp[now.S][i])
continue;
for(auto j : edge[now.S]){
if(now.F + j.S < dp[j.F][i]){
dp[j.F][i] = now.F + j.S;
pq.push(MP(-dp[j.F][i], j.F));
}
}
}
}
ll ans = 2e18;
for(int i = 1;i <= n;i++)
ans = min(ans, dp[i][(1 << k) - 1]);
cout << ans << '\n';
}
int main(){
IOS
// CASE
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
24916 KB |
Output is correct |
2 |
Correct |
14 ms |
24952 KB |
Output is correct |
3 |
Correct |
14 ms |
24940 KB |
Output is correct |
4 |
Correct |
14 ms |
24916 KB |
Output is correct |
5 |
Correct |
15 ms |
24948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
680 ms |
71096 KB |
Output is correct |
2 |
Correct |
666 ms |
70628 KB |
Output is correct |
3 |
Correct |
454 ms |
60980 KB |
Output is correct |
4 |
Correct |
79 ms |
37684 KB |
Output is correct |
5 |
Correct |
398 ms |
71096 KB |
Output is correct |
6 |
Correct |
72 ms |
37616 KB |
Output is correct |
7 |
Correct |
17 ms |
25412 KB |
Output is correct |
8 |
Correct |
16 ms |
25340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
25276 KB |
Output is correct |
2 |
Correct |
18 ms |
25300 KB |
Output is correct |
3 |
Correct |
17 ms |
25316 KB |
Output is correct |
4 |
Correct |
16 ms |
25220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1397 ms |
71080 KB |
Output is correct |
2 |
Correct |
1448 ms |
70636 KB |
Output is correct |
3 |
Correct |
905 ms |
61076 KB |
Output is correct |
4 |
Correct |
814 ms |
55832 KB |
Output is correct |
5 |
Correct |
169 ms |
41780 KB |
Output is correct |
6 |
Correct |
78 ms |
39792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2751 ms |
71280 KB |
Output is correct |
2 |
Correct |
2794 ms |
71100 KB |
Output is correct |
3 |
Correct |
2701 ms |
70876 KB |
Output is correct |
4 |
Correct |
1911 ms |
61168 KB |
Output is correct |
5 |
Correct |
1438 ms |
55856 KB |
Output is correct |
6 |
Correct |
278 ms |
41880 KB |
Output is correct |
7 |
Correct |
92 ms |
40112 KB |
Output is correct |