/*ACMer:MDK
Accepted 2066 46MS 4284K 1465 B G++ MDK
FOR:hdu 2066*/
#include<iostream>
#include<string.h>
#include<limits.h>
#include<stdio.h>
#define MAXN 1005
#define INTMAX 1000000 //这里取的大小让我纠结了很长时间我一开始去0x7fffffff,居然有负数,不知道为啥
using namespace std;
int map[MAXN][MAXN],minl[MAXN],pre[MAXN],live[100],goal[100];
int big = INT_MIN;
int dijkstra(int T,int S,int D)
{
int n = ++big,i,j,k;
int v[MAXN];
for(i = 0;i<n;i++)
v[i]=0,minl[i]=INTMAX,pre[i] = -1;
for(minl[0]=0,j = 0;j<n;j++)
{
for(k = -1,i=0;i<n;i++)
if(!v[i]&&(k==-1||minl[i]<minl[k]))
k=i;
for(v[k]=1,i=0;i<n;i++)
if(!v[i]&&map[k][j]!=INTMAX&&minl[k]+map[k][i]<minl[i])
minl[i]=minl[k]+map[pre[i]=k][i];//,cout<<minl[i]<<endl
}
int mina = INT_MAX;
for(i = 0;i<D;i++)
mina = min(mina,minl[goal[i]]);//,cout<<minl[goal[i]]<<endl
return mina;
}
int main()
{
freopen("d:\\1.txt","r",stdin);
int T,S,D;
while(cin>>T>>S>>D&&T)
{
int a,b;
memset(map,INTMAX,sizeof(map));
memset(minl,INTMAX,sizeof(minl));
memset(pre,0,sizeof(pre));
for(int i = 0,v;i<T;i++){
cin>>a>>b>>v,big = max(big,max(a,b));
if( v < map[a][b] ) map[a][b] = map[b][a] = v;
}//,cout<<map[b][a]<<endl
for(int i = 0;i<S;i++)
cin>>live[i],map[0][live[i]]=map[live[i]][0]=0;
for(int i = 0;i<D;i++)
cin>>goal[i];
cout<<dijkstra(T,S,D)<<endl;
}
}
一开始用Floyd算法,果断超时,后来想想dijkstra还是可行的,近似的可以用多源,输入要加个判断,测试数据很是强烈。