http://acm.hdu.edu.cn/showproblem.php?pid=2066
把与草儿相连的城市最短距离置为0,然后进行dijkstra,在t个城市里找出距离最近的一个即可。
#include <iostream>
#include <vector>
#include <queue>
#include <cstdio>
using namespace std; const int maxn = ;
const int INF = <<;
struct edge {
int to,cost;
edge(){}
edge( int x,int y ) {
to=x;
cost=y;
}
}; typedef pair<int,int>P;
vector<edge>G[maxn];
int d[maxn],s[maxn],e[maxn];
int T,S,D,n; void dijkstra() {
priority_queue<P,vector<P>,greater<P> >que;
for(int i=;i<=n;i++) d[i]=INF;
for(int i=;i<=S;i++)
{
d[s[i]]=;
que.push(P(,s[i]));
}
while(!que.empty()) {
P p=que.top(); que.pop();
int v=p.second;
if(d[v]<p.first) continue;
for(int i=;i<G[v].size();i++) {
edge e=G[v][i];
if(d[e.to]>d[v]+e.cost) {
d[e.to]=d[v]+e.cost;
que.push(P(d[e.to],e.to));
}
}
}
} int main()
{
//freopen("a.txt","r",stdin);
while(~scanf("%d%d%d",&T,&S,&D))
{
for(int i=;i<=maxn;i++) G[i].clear();
n=;
int a,b,c,v;
for(int i=;i<=T;i++)
{
scanf("%d%d%d",&a,&b,&c);
// printf("%d %d %d\n",a,b,c);
G[a].push_back(edge(b,c));
G[b].push_back(edge(a,c));
if(a>n) n=a;
if(b>n) n=b;
}
for(int i=;i<=S;i++) scanf("%d",&s[i]);
for(int i=;i<=D;i++) scanf("%d",&e[i]);
dijkstra();
int ans=INF;
for(int i=;i<=D;i++)
if(d[e[i]]<ans) ans=d[e[i]];
printf("%d\n",ans); }
return ;
}