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 ll long long
#define pb push_back
#define ff first
#define ss second
#define all(s) s.begin(),s.end()
#define rall(s) s.rbegin(),s.rend()
int main(){
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
int n,k,m;
cin>>n>>k>>m;
ll dp[32][n];
int p[k];
vector<pair<int,ll>>adj[n];
for(int i=0;i<k;i++){
cin>>p[i];
p[i]--;
}
for(int i=0;i<m;i++){
int a,b;
ll c;
cin>>a>>b>>c;
a--,b--;
adj[a].pb({b,c});
adj[b].pb({a,c});
}
for(int i=0;i<(1<<k);i++){
for(int j=0;j<n;j++){
dp[i][j]=1e18;
}
}
for(int i=0;i<k;i++){
dp[1<<i][p[i]]=0;
}
for(int c=0;c<(1<<k);c++){
for(int a=0;a<c;a++){
if((a|c)!=c) continue;
int b=a^c;
if(b>a) continue;
for(int i=0;i<n;i++){
dp[c][i]=min(dp[c][i],dp[a][i]+dp[b][i]);
}
}
set<pair<ll,int>>st;
for(int i=0;i<n;i++){
if(dp[c][i]<1e18){
st.insert({dp[c][i],i});
}
}
while(!st.empty()){
auto [d,i]=*st.begin();
st.erase(st.begin());
for(auto [j,C]:adj[i]){
ll cost=C+d;
if(cost<dp[c][j]){
if(dp[c][j]<1e18){
st.erase({dp[c][j],j});
}
dp[c][j]=cost;
st.insert({dp[c][j],j});
}
}
}
}
ll ans=1e18;
for(int i=0;i<n;i++){
ans=min(ans,dp[(1<<k)-1][i]);
}
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... |