Pagini recente » Cod sursa (job #702738) | Cod sursa (job #1052125) | Cod sursa (job #1307654) | Cod sursa (job #2184874) | Cod sursa (job #561745)
Cod sursa(job #561745)
#include<cstdio>
#include<queue>
#define Nmx 2001
#define INF 3155745
#define Mx 23
using namespace std;
int n,m,k,c[Mx],cost[Nmx],viz[Nmx];
int mat[Mx][Mx],bst[32775][Mx];
queue<int> Q;
struct nod
{
int inf,cost;
nod *urm;
};
nod *G[Nmx];
void add(int x,int y,int c)
{
nod *aux=new nod;
aux->inf = y;
aux->cost = c;
aux->urm = G[x];
G[x]=aux;
}
void citire()
{
scanf("%d%d",&n,&m);
scanf("%d",&k);
c[0]=1;
for(int i=1;i<=k;++i)
scanf("%d",&c[i]);
c[k+1]=n;
int x,y,c;
for(int i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&c);
add(x,y,c);
add(y,x,c);
}
}
void belman(int x)
{
for(int i=1;i<=n;++i)
{
cost[i]=INF;
viz[i]=0;
}
Q.push(x);
cost[x]=0;
viz[x]=1;
while(!Q.empty())
{
int y = Q.front();
Q.pop();
viz[y]=0;
for(nod *p=G[y];p;p=p->urm)
if(cost[p->inf]>cost[y]+p->cost)
{
cost[p->inf]=cost[y]+p->cost;
if(!viz[p->inf])
{
Q.push(p->inf);
viz[p->inf]=1;
}
}
}
}
int main()
{
freopen("ubuntzei.in","r",stdin);
freopen("ubuntzei.out","w",stdout);
citire();
for(int i=0;i<=k;++i)
{
belman(c[i]);
for(int j=1; j<=k+1; ++j)
mat[i][j]=cost[c[j]];
}
int oldstare;
if(!k)
printf("%d\n",mat[0][1]);
else
{
memset(bst,INF,sizeof(bst));
for(int stare=0;stare<(1<<k);++stare)
for(int i=1;i<=k;++i)
if(stare&(1<<(i-1)))
{
oldstare=stare^(1<<(i-1));
if (!oldstare)
bst[stare][i]=mat[0][i];
else
for (int j=1; j<=k; ++j)
if (oldstare&(1<<(j-1)))
bst[stare][i]=min (bst[stare][i],bst[oldstare][j]+mat[i][j]);
}
int sol=INF;
for(int j=1;j<=k;++j)
sol=min(sol,bst[(1<<k)-1][j]+mat[j][k+1]);
if(sol==INF)
printf("-1\n");
else printf("%d\n",sol);
}
return 0;
}