Pagini recente » Cod sursa (job #2575964) | Cod sursa (job #1694795) | Cod sursa (job #2571153) | Cod sursa (job #966432) | Cod sursa (job #594420)
Cod sursa(job #594420)
#include<queue>
#include<vector>
#include<fstream>
using namespace std;
ifstream in("ubuntzei.in");
ofstream out("ubuntzei.out");
const int N=2010;
const int INF=0x3f3f3f;
int n,m,k,v[N],d[N],t[N],cost[18][18];
vector<int>a[N];
vector<int>c[N];
bool viz[N];
queue<int>coada;
int cc[131072][18];
inline int min(int eee,int www)
{
return eee<www?eee:www;
}
void read()
{
int x,y,z;
in>>n>>m>>k;
for(int i=1;i<=k;i++)
in>>v[i];
for(int i=1;i<=m;i++)
{
in>>x>>y>>z;
a[x].push_back(y);
a[y].push_back(x);
c[x].push_back(z);
c[y].push_back(z);
}
}
void init()
{
for(int i=0;i<=n;i++)
d[i]=INF,viz[i]=0,t[i]=0;
}
void bford(int param)
{
init();
int r,x,cost;
coada.push(param);
d[param]=0;
viz[param]=1;
bool ok=1;
while(ok && !coada.empty())
{
r=coada.front();
coada.pop();
viz[r]=0;
for(size_t i=0;i<a[r].size();i++)
{
x=a[r][i];
cost=c[r][i];
if(d[r]+cost<d[x])
{
d[x]=d[r]+cost;
if(!viz[x])
{
viz[x]=true;
t[x]++;
if(t[x]>n-1)
ok=0;
coada.push(x);
}
}
}
}
}
void solve()
{
for(int i=0;i<=k+1;i++)
for(int j=0;j<=k+1;j++)
cost[i][j]=INF;
int x;
for(int i=1;i<=k;i++)
{
x=v[i];
bford(x);
cost[x][0]=d[1];
cost[x][k+1]=d[n];
for(int j=1;j<=k;j++)
cost[x][j]=cost[j][x]=d[ v[j] ];
}
cc[1][0]=0;
int o=k+1;
for(int i=0;i<(1<<o);i++)
for(int j=0;j<o;j++)
if(i &(1<<j) )
{
for(int k=0;k<=o;k++)
{
if(!cost[j][k])
continue;
if(i & (1<<k) )
cc[i][j]=min(cc[i][j], cc[1 ^ (1<<j) ][k] + cost[k][j] );
}
}
out<<c[(1<<k)][o];
}
int main()
{
read();
bford(1);
solve();
return 0;
}