Pagini recente » Cod sursa (job #1259273) | Cod sursa (job #82563) | Cod sursa (job #3167181) | Cod sursa (job #67944) | Cod sursa (job #2442074)
#include <cstdio>
#include <queue>
#include <algorithm>
using namespace std;
int dp[20][65540];
const int INF = 2000000000;
int n;
struct Nod
{
int x,cost;
bool operator >(const Nod &other) const
{
return this->cost>other.cost;
}
};
vector<Nod> g[2005];
int oras[20];
int dijk[20][2005];
void dijkstra(int s) {
int i;
for(i=1; i<=n; i++)
dijk[s][i]=INF;
dijk[s][oras[s]]=0;
priority_queue<Nod, vector<Nod>, greater<Nod> > q;
q.push({oras[s], 0});
while(!q.empty()){
Nod u=q.top();
if(dijk[s][u.x]==q.top().cost) {
q.pop();
for(i=0; i<g[u.x].size(); i++)
if(dijk[s][g[u.x][i].x]>dijk[s][u.x]+g[u.x][i].cost) {
dijk[s][g[u.x][i].x]=dijk[s][u.x]+g[u.x][i].cost;
q.push({g[u.x][i].x, dijk[s][g[u.x][i].x]});
}
}
else
q.pop();
}
}
int main()
{ freopen("ubuntzei.in", "r",stdin);
freopen("ubuntzei.out", "w",stdout);
int m,k,i,x,y,z,minn,j,l,ll;
scanf("%d%d%d", &n, &m, &k);
for(i=1; i<=k; i++){
scanf("%d", &x);
oras[i]=x;
}
oras[k+1]=n;
oras[k+2]=1;
for(i=1; i<=m; i++){
scanf("%d%d%d", &x, &y, &z);
g[x].push_back({y, z});
g[y].push_back({x, z});
}
for(i=1; i<=k+2; i++)
dijkstra(i);
int lim=(1<<(k+1));
for(i=1; i<lim; i++){
for(j=0; j<=k; j++)
dp[j][i]=INF;
}
for(i=1; i<=k+1; i++)
dp[i-1][1<<(i-1)]=dijk[k+2][oras[i]];
for(i=1; i<lim; i++){
for(j=0; j<=k; j++)
if(dp[j][i]!=INF){
for(l=0; l<=k; l++){
int ll=(1<<l);
if(dp[l][i]==INF)
dp[l][i+ll]=min(dp[l][i+ll], dp[j][i]+dijk[j+1][oras[l+1]]);
}
}
}
printf("%d", dp[k][lim-1]);
return 0;
}