Pagini recente » Cod sursa (job #1726108) | Cod sursa (job #3213083) | Infoarena Monthly 2014 - Runda 9, Solutii | Cod sursa (job #1735444) | Cod sursa (job #1894529)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cstring>
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
int n,m,k;
int K[20],KI[2002];
vector<pair<int,int>> L[2002];
int D[1<<18+2][16];
int DINIT[20][2002];
void read(){
int x,y,t;
in>>n>>m>>k;
for(int i=1;i<=k;i++){
in>>K[i];
KI[K[i]]=i;
}
for(int i=1;i<=m;i++){
in>>x>>y>>t;
L[x].push_back({y,t});
L[y].push_back({x,t});
}
}
void dijkstra(int st){
int U[2002], nod, indi=KI[st];
memset(U,0,sizeof(U));
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> PQ;
PQ.push({0,st});
DINIT[KI[st]][st]=0;
while(!PQ.empty()){
nod=PQ.top().second;
PQ.pop();
if(!U[nod]){
U[nod]=1;
for(auto x : L[nod]){
if(DINIT[KI[st]][x.first]>DINIT[KI[st]][nod]+x.second){
DINIT[KI[st]][x.first]=DINIT[KI[st]][nod]+x.second;
if(!U[x.first])
PQ.push({DINIT[KI[st]][x.first],x.first});
}
}
}
}
}
void solve(){
KI[1]=0;
K[0]=1;
K[k+1]=n;
KI[n]=k+1;
k+=2;
for(int i=0;i<(1<<k);i++){
for(int j=0;j<=k;j++){
D[i][j]=1000000000;
}
}
for(int i=0;i<=k;i++){
for(int j=0;j<=n;j++){
DINIT[i][j]=1000000000;
}
}
dijkstra(1);
if(k==1){
out<<DINIT[KI[1]][n];
return;
}
for(int i=1;i<k;i++){
dijkstra(K[i]);
}
D[1][0]=0;
for(int i=1;i<(1<<(k));i++){
for(int j=0;j<k;j++){
if(i & (1<<j)){
for(int l=0;l<k;l++){
if(l!=j&& i&(1<<l)){
D[i][j]=min(D[i][j],D[i ^ (1<<j)][l]+DINIT[j][K[l]]);
}
}
}
}
}
out<<D[(1<<k)-1][k-1];
}
int main(){
read();
solve();
return 0;
}