#include <bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n,k,m;
cin >> n >> k >> m;
vector<vector<pair<int,int>>> graph(n+1);
int DP[n+1][1<<k];
bool vis[n+1][1<<k];
memset(vis,false,sizeof(vis));
vector<int> impo(k);
for(int i=0;i<k;i++){
cin >> impo[i];
}
for(int i=0;i<m;i++){
int a,b,c;
cin >> a >> b >> c;
graph[a].push_back({b,c});
graph[b].push_back({a,c});
}
for(int mask=1;mask<(1<<k);mask++){
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
if(!(mask&(mask-1))){
for(int u=1;u<=n;u++){
DP[u][mask]=1e18;
}
for(int i=0;i<k;i++){
if(mask&(1<<i)){
DP[impo[i]][mask]=0;
pq.push({0,impo[i]});
}
}
}
else{
for(int u=1;u<=n;u++){
DP[u][mask]=1e18;
for(int s=((mask-1)&mask);s;s=((s-1)&mask)){
DP[u][mask]=min(DP[u][mask],DP[u][s]+DP[u][mask^s]);
}
pq.push({DP[u][mask],u});
}
}
while(!pq.empty()){
int u=pq.top().second,weight=pq.top().first;
pq.pop();
if(weight>DP[u][mask]||vis[u][mask]){
continue;
}
vis[u][mask]=true;
for(pair<int,int> x:graph[u]){
int v=x.first;
int dist=DP[u][mask]+x.second;
if(dist<DP[v][mask]){
DP[v][mask]=dist;
pq.push({dist,v});
}
}
}
}
/*for(int mask=1;mask<(1<<k);mask++){
cout << mask << ": " << endl;
for(int u=1;u<=n;u++){
cout << DP[u][mask] << ' ';
}
cout << endl;
}*/
int ans=1e18;
for(int u=1;u<=n;u++){
ans=min(ans,DP[u][(1<<k)-1]);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
406 ms |
30748 KB |
Output is correct |
2 |
Correct |
360 ms |
30196 KB |
Output is correct |
3 |
Correct |
212 ms |
20832 KB |
Output is correct |
4 |
Correct |
41 ms |
13056 KB |
Output is correct |
5 |
Correct |
162 ms |
25104 KB |
Output is correct |
6 |
Correct |
40 ms |
13004 KB |
Output is correct |
7 |
Correct |
2 ms |
600 KB |
Output is correct |
8 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
604 KB |
Output is correct |
2 |
Correct |
4 ms |
604 KB |
Output is correct |
3 |
Correct |
3 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
851 ms |
37856 KB |
Output is correct |
2 |
Correct |
864 ms |
37244 KB |
Output is correct |
3 |
Correct |
488 ms |
27876 KB |
Output is correct |
4 |
Correct |
431 ms |
26908 KB |
Output is correct |
5 |
Correct |
124 ms |
15660 KB |
Output is correct |
6 |
Correct |
47 ms |
15344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2001 ms |
51924 KB |
Output is correct |
2 |
Correct |
2056 ms |
52164 KB |
Output is correct |
3 |
Correct |
1912 ms |
51336 KB |
Output is correct |
4 |
Correct |
1322 ms |
41972 KB |
Output is correct |
5 |
Correct |
907 ms |
33936 KB |
Output is correct |
6 |
Correct |
198 ms |
17176 KB |
Output is correct |
7 |
Correct |
57 ms |
15184 KB |
Output is correct |