Pagini recente » Cod sursa (job #2841435) | Cod sursa (job #636137) | Monitorul de evaluare | Cod sursa (job #3033396) | Cod sursa (job #1646074)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("ubuntzei.in");
ofstream fout ("ubuntzei.out");
const int NMAX = 10005;
const int KMAX = 15;
const int INF = 1e9;
int D[KMAX][NMAX],n,S,C[NMAX],SOL[(1 << KMAX)][KMAX];
vector < pair < int,int > > G[NMAX];
struct cmp{
bool operator()(const int &a,const int &b){
return D[S][a] < D[S][b];
}
};
priority_queue < int , vector < int > , cmp > T;
inline int bit(int x,int y){
return (x & (1 << y)) != 0;
}
inline void dijkstra(int nod)
{
for(int i = 1; i <= n; i++)
D[S][i] = INF;
D[S][nod] = 0;
T.push(nod);
while(!T.empty())
{
nod = T.top();
T.pop();
for(int i = 0 ; i < G[nod].size(); i ++){
if( D[S][G[nod][i].second] > D[S][nod] + G[nod][i].first){
D[S][G[nod][i].second] = D[S][nod] + G[nod][i].first;
T.push(G[nod][i].second);
}
}
}
}
int main()
{
int m,x,y,c,K,newdis,j;
fin >> n >> m >> K;
for(int i = 0; i < K ; i++)
fin >> C[i];
for(int i = 1; i <= m; i++){
fin >> x >> y >> c;
G[x].push_back({c,y});
G[y].push_back({c,x});
}
S = K;
dijkstra(1);
if(K == 0){
fout << D[K][n];
return 0;
}
for(int i = 0 ; i < K; i++){
S = i;
dijkstra(C[i]);
}
for(int i = 1; i < (1 << K); i++){
for( j = 0 ; j < K; j++){
if(i == (1<<j));
break;
}
if( j < K && i == (1<<j)){
SOL[i][j] = D[K][C[j]];
continue;
}
for(j = 0; j < K; j++){
SOL[i][j] = INF;
if(bit(i,j)){
for(int k = 0; k < K; k++){
if(bit(i,k) && j != k){
newdis = SOL[i - (1<<j)][k] + D[k][C[j]];
SOL[i][j] = min(SOL[i][j] , newdis);
}
}
}
}
}
S = INF;
for(int i = 0 ; i < K; i++){
S = min(S,SOL[(1 << K)-1][i] + D[i][n]);
}
fout << S;
return 0;
}