Pagini recente » Cod sursa (job #1876709) | Cod sursa (job #781915) | Cod sursa (job #850688) | Cod sursa (job #285676) | Cod sursa (job #1358404)
#include <fstream>
#include <vector>
#include <cstdio>
#define nmax 2005
#define kmax 16
#define inf 1<<25
using namespace std;
ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
int n,m,k,a[nmax],r[nmax];
char s[30];
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;
f>>n>>m;
f>>k;
for (i=1;i<=k;i++){
f>>a[i];
r[a[i]]=i;
}
f.get();
for (i=1;i<=m;i++) {
f.getline(s,30);
x=0;y=0;z=0;
j=0;
while (s[j]!=' ')
x=x*10+s[j++]-'0';
j++;
while (s[j]!=' ')
y=y*10+s[j++]-'0';
j++;
while (s[j]!=NULL)
z=z*10+s[j++]-'0';
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);
}
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);
}
if (t[j]+y<t[x]) {
t[x]=t[j]+y;
c[++u]=x;
}
}
}
}
g<<t[n];
return 0;
}