Pagini recente » Cod sursa (job #846440) | Cod sursa (job #1744189) | Cod sursa (job #921401) | Cod sursa (job #1597195) | Cod sursa (job #1358392)
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstdio>
#define nmax 2005
#define kmax 16
#define inf 1<<30
using namespace std;
FILE *f=fopen("ubuntzei.in","r");
ofstream g("ubuntzei.out");
int n,m,k,a[nmax],r[nmax];
short v[nmax][nmax];
int w[nmax][nmax];
int c[nmax*10],p,u;
int q[1<<kmax][kmax],t[nmax];
int main()
{
int i,j,l,x,y,z;
fscanf(f,"%d%d",&n,&m);
fscanf(f,"%d",&k);
for (i=1;i<=k;i++){
fscanf(f,"%d",&a[i]);
r[a[i]]=i;
}
for (i=1;i<=m;i++) {
fscanf(f,"%d%d%d",&x,&y,&z);
v[x][++v[x][0]]=y;
w[x][++w[x][0]]=z;
v[y][++v[y][0]]=x;
w[y][++w[y][0]]=z;
}
for (i=0;i<=(1<<k);i++)
for (j=1;j<=k;j++)
q[i][j]=inf;
for (i=1;i<=n;i++)
t[i]=inf;
t[1]=0;
p=1;u=1;
c[1]=1;
for (;p<=u;p++) {
j=c[p];
for (l=1;l<=v[j][0];l++){
x=v[j][l];
y=w[j][l];
if (r[x]!=0) {
q[(1<<(r[x]-1))][r[x]]=min(q[(1<<(r[x]-1))][r[x]],t[j]+y);
}
if (t[j]+y<t[x]) {
t[x]=t[j]+y;
c[++u]=x;
}
}
}
for (i=1;i<(1<<k);i++) {
p=1;u=0;
for (j=1;j<=n;j++)
t[j]=inf;
for (j=1;j<=k;j++)
if (q[i][j]!=inf) {
t[a[j]]=q[i][j];
c[++u]=a[j];
}
for (;p<=u;p++) {
j=c[p];
for (l=1;l<=v[j][0];l++){
x=v[j][l];
y=w[j][l];
if (r[x]!=0) {
if ((i&(1<<(r[x]-1)))==0)
q[i+(1<<(r[x]-1))][r[x]]=min(q[i+(1<<(r[x]-1))][r[x]],t[j]+y);
}
if (t[j]+y<t[x]) {
t[x]=t[j]+y;
c[++u]=x;
}
}
}
}
g<<t[n];
return 0;
}