Pagini recente » Cod sursa (job #216624) | Cod sursa (job #2118868) | Cod sursa (job #2400132) | Cod sursa (job #835681) | Cod sursa (job #2107123)
#include <bits/stdc++.h>
#define INF 2140000000
#define MOD 1000000007
#define MaxN 2005
#define Mask 1<<15
using namespace std;
FILE *IN=fopen("ubuntzei.in","r"),*OUT=fopen("ubuntzei.out","w");
int N,M,K,X,Y,Z,d[MaxN][Mask],spec[MaxN];
vector<pair<short,int> > Graph[MaxN];
queue<pair<short,int> >Q;
void BFS(short node,int mask)
{
Q.push({node,mask});
while(!Q.empty())
{
node=Q.front().first;
mask=Q.front().second;
Q.pop();
for(int i=0;i<Graph[node].size();i++)
{
if(spec[Graph[node][i].first]!=-1&&!(mask&(1<<spec[Graph[node][i].first]))&&(d[Graph[node][i].first][mask+(1<<spec[Graph[node][i].first])]==0||d[Graph[node][i].first][mask+(1<<spec[Graph[node][i].first])]>Graph[node][i].second+d[node][mask]))
{
d[Graph[node][i].first][mask+(1<<spec[Graph[node][i].first])]=Graph[node][i].second+d[node][mask];
Q.push({Graph[node][i].first,mask+(1<<spec[Graph[node][i].first])});
}
else if(d[Graph[node][i].first][mask]==0||d[Graph[node][i].first][mask]>Graph[node][i].second+d[node][mask])
{
d[Graph[node][i].first][mask]=Graph[node][i].second+d[node][mask];
Q.push({Graph[node][i].first,mask});
}
}
}
}
int main()
{
fscanf(IN,"%d %d %d",&N,&M,&K);
for(int i=1;i<=N;i++)
spec[i]=-1;
for(int i=0;i<K;i++)
{
fscanf(IN,"%d",&X);
spec[X]=i;
}
for(int i=1;i<=M;i++)
{
fscanf(IN,"%d %d %d",&X,&Y,&Z);
Graph[X].push_back({Y,Z});
Graph[Y].push_back({X,Z});
}
BFS(1,0);
fprintf(OUT,"%d",d[N][(1<<K)-1]);
return 0;
}