Pagini recente » Cod sursa (job #3269326) | Cod sursa (job #2133423) | Cod sursa (job #2366393) | Cod sursa (job #10903) | Cod sursa (job #3197801)
#include <bits/stdc++.h>
#define DIM 2001
using namespace std;
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
struct nod{
int node, value;
bool operator < (nod x) const{
return (value > x.value);
}
};
nod temp;
vector < pair <int, int> > G[5 * DIM];
int dp[(1LL << 16) + 2][20];
int cost[DIM][DIM];
int event[DIM];
int n, m, x, y, z, k, i, mask, q, j, answer = 1e9;
void Dijkstra(int source){
priority_queue <nod> Q;
temp.node = source;
temp.value = 0;
Q.push(temp);
for(int i=1;i<=n;i++)
cost[source][i] = 1e9;
cost[source][source] = 0;
while(!Q.empty()){
int node = Q.top().node;
int value = Q.top().value;
Q.pop();
if(cost[source][node] != value)
continue;
vector < pair <int, int> > :: iterator j;
for(j = G[node].begin() ; j != G[node].end() ; j++){
if(cost[source][j -> first] > cost[source][node] + j -> second){
cost[source][j -> first] = cost[source][node] + j -> second;
temp.node = j -> first;
temp.value = cost[source][j -> first];
Q.push(temp);
}
}
}
}
int main(){
fin >> n >> m >> k;
for(i=1;i<=k;i++)
fin >> event[i];
event[k + 2] = n;
event[k + 1] = 1;
while(m--){
fin >> x >> y >> z;
G[x].push_back(make_pair(y, z));
G[y].push_back(make_pair(x, z));
}
for(i=1;i<=k+2;i++)
Dijkstra(event[i]);
for(i=1;i<=k;i++)
for(mask = 1 ; mask <= ((1LL << (k)) - 1) ; mask++)
dp[mask][i] = 1e9;
for(i=1;i<=k;i++)
dp[(1LL << (i - 1))][i] = cost[1][event[i]];
for(mask = 1 ; mask <= ((1LL << k) - 1) ; mask++){
if(__builtin_popcount(mask) == 1)
continue;
for(j=0;j<k;j++){
if(((mask >> j) & 1)){
for(q=0;q<k;q++){
if((((mask >> q) & 1)) && q != j)
dp[mask][j + 1] = min(dp[mask][j + 1], dp[mask - (1LL << j)][q + 1] + cost[event[q + 1]][event[j + 1]]);
}
}
}
}
for(i=1;i<=k;i++)
answer = min(answer, dp[((1LL << k) - 1)][i] + cost[event[i]][n]);
if(!k)
fout << cost[1][n];
else
fout << answer;
return 0;
}