Pagini recente » Cod sursa (job #781044) | Cod sursa (job #1395050) | Cod sursa (job #876632) | Cod sursa (job #1225057) | Cod sursa (job #1358366)
#include <fstream>
#include <vector>
#include <cstdio>
#define nmax 2005
#define kmax 16
#define inf 1<<25
using namespace std;
FILE *f=fopen("ubuntzei.in","r");
ofstream g("ubuntzei.out");
int n,m,k,a[nmax],r[nmax];
vector < pair < int , int > > v[nmax];
vector < pair < int , int > > :: iterator it;
int c[nmax*10],p,u;
int q[1<<kmax][kmax],t[nmax];
int main()
{
int i,j;
int 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].push_back(make_pair(y,z));
v[y].push_back(make_pair(x,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];
it=v[j].begin();
for (;it!=v[j].end();it++) {
x=it->first;
y=it->second;
if (r[x]!=0) {
q[(1<<(r[x]-1))][r[x]]=min(q[(1<<(r[x]-1))][r[x]],t[j]+y);
}
else {
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];
it=v[j].begin();
for (;it!=v[j].end();it++) {
x=it->first;
y=it->second;
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);
}
else {
if (t[j]+y<t[x]) {
t[x]=t[j]+y;
c[++u]=x;
}
}
}
}
}
g<<t[n];
return 0;
}