This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |