0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-25 19:32:12 +00:00
OI-codes/S2OJ/161/data/std.cpp

57 lines
1.1 KiB (Stored with Git LFS)
C++

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x=0,f=1,c=getchar();
while(c<48) c=='-'&&(f=-1),c=getchar();
while(c>47) x=x*10+c-'0',c=getchar();
return x*f;
}
const int MAXN = 505;
const int INF = 0x3f3f3f3f;
struct Edge {int to,val;};
std::vector<Edge> G[MAXN];
int p[MAXN],inq[MAXN],f[MAXN][MAXN<<3];
int n,m,k; queue<int> q;
inline void addedge(int u,int v,int w)
{
G[u].push_back(Edge{v,w});
G[v].push_back(Edge{u,w});
}
inline void spfa(int st)
{
while(!q.empty())
{
int x=q.front(); q.pop(); inq[x]=0;
for(Edge &e : G[x])
if(f[e.to][st]>f[x][st]+e.val)
{
f[e.to][st]=f[x][st]+e.val;
if(!inq[e.to]) q.push(e.to),inq[e.to]=1;
}
}
}
int main(int argc, char const *argv[])
{
n=read(),m=read(),k=read();
memset(f,INF,sizeof(f));
for(int i=1,x,y; i<=m; ++i)
x=read(),y=read(),addedge(x,y,read());
for(int i=1; i<=k; ++i)
f[p[i]=read()][1<<(i-1)]=0;
for(int st=0; st<(1<<k); spfa(st),++st)
for(int i=1; i<=n; ++i)
{
for(int t=st; t; t=(t-1)&st)
f[i][st]=min(f[i][st],f[i][t]+f[i][st^t]);
if(f[i][st]^INF) q.push(i),inq[i]=1;
}
printf("%d",f[p[1]][(1<<k)-1]);
return 0;
}