#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define szz(r) (ll)r.size()
#define vi vector<int>
#define vvi vector<vi>
#define pp pair<ll,int>
#define ub(x,i) upper_bound(all(x),i)-x.begin()
using namespace std;
const int mxn=1e5+5;
ll d[32][mxn]{0};
vector<pii>g[mxn];
int a[5];
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,k,m;cin>>n>>k>>m;
for(int i=0;i<k;i++)cin>>a[i];
for(int i=0;i<m;i++){
int a,b,c;cin>>a>>b>>c;
g[a].pb({b,c});g[b].pb({a,c});
}int x=(1<<k);
for(int i=0;i<x;i++)for(int j=1;j<=n;j++)d[i][j]=1e16;
for(int i=0;i<k;i++)d[(1<<i)][a[i]]=0;
for(int msk=1;msk<x;msk++){
priority_queue<pair<ll,int>,vector<pair<ll,int>>,greater<pair<ll,int>>>pq;
for(int i=1;i<=n;i++){
int msk2=msk;
while(msk2>0){
d[msk][i]=min(d[msk][i],d[msk^msk2][i]+d[msk2][i]);
msk2=(msk2-1)&msk;
}pq.push({d[msk][i],i});
}
while(!pq.empty()){
auto u=pq.top();pq.pop();
if(u.f>d[msk][u.s])continue;
for(auto [v,w]:g[u.s]){
if(d[msk][v]>u.f+w){
d[msk][v]=u.f+w;pq.push({d[msk][v],v});
}
}
}
}cout<<*min_element(d[x-1]+1,d[x-1]+n+1);
}
# | 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... |