//!yrt tsuj uoy srettam gnihton no emoc
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define pii pair <ll, int>
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define F first
#define S second
const int maxn = 2e5 + 5;
int x[maxn], y[maxn];
int im[maxn];
vector <pii> adj[maxn];
ll dp[maxn][35];
ll inf = 1e17;
int n, m, k;
int main(){
fast_io;
cin >> n >> k >> m;
for(int i = 0; i < k; i ++){
cin >> im[i];
im[i] --;
}
for(int i = 0; i < m; i ++){
int x, y;
ll w;
cin >> x >> y >> w;
x --; y --;
adj[x].pb({y, w});
adj[y].pb({x, w});
}
ll ans = inf;
for(int mask = 0; mask < (1 << k); mask ++){
for(int i = 0; i < n; i ++){
dp[i][mask] = inf;
}
}
for(int i = 0; i < k; i ++){
dp[im[i]][1 << i] = 0;
}
for(int mask = 1; mask < (1 << k); mask ++){
for(int m1 = mask; m1; m1 = (m1 - 1) & mask){
for(int i = 0; i < n; i ++){
dp[i][mask] = min(dp[i][mask], dp[i][mask ^ m1] + dp[i][m1]);
}
}
priority_queue <pii> s;
for(int i = 0; i < n; i ++){
if(dp[i][mask] < inf){
s.push({dp[i][mask], i});
}
}
while(s.size()){
ll we = s.top().F;
int x = s.top().S;
s.pop();
if(dp[x][mask] == we){
for(auto u : adj[x]){
if(dp[u.F][mask] > dp[x][mask] + u.S){
dp[u.F][mask] = dp[x][mask] + u.S;
s.push({dp[u.F][mask], u.F});
}
}
}
}
}
for(int i = 0; i < n; i ++){
ans = min(ans, dp[i][(1 << k) - 1]);
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
4940 KB |
Output is correct |
2 |
Correct |
3 ms |
4940 KB |
Output is correct |
3 |
Correct |
3 ms |
4940 KB |
Output is correct |
4 |
Correct |
4 ms |
4940 KB |
Output is correct |
5 |
Correct |
4 ms |
4940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6100 ms |
43912 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
5324 KB |
Output is correct |
2 |
Correct |
23 ms |
5436 KB |
Output is correct |
3 |
Correct |
53 ms |
5328 KB |
Output is correct |
4 |
Correct |
42 ms |
5196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6081 ms |
43840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6058 ms |
43840 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |