思路:建立超级源点0,向每个相邻的城市建一条边权为0的边,跑最短路。注意,数据中有存在自环的情况,特判一下
#include<cstdio>
#include<queue>
#include<algorithm>
using namespace std;
const int N = 1e3 + 5;
const int Inf = 1e9;
typedef pair<int, int> P;
vector<P> vec[N];
inline void add(int x, int y, int v) {
vec[x].push_back(P(v, y));
}
int dis[N], vis[N];
void Dijstra(int u) {
dis[u] = 0;
priority_queue<P, vector<P>, greater<P> >que;
que.push(P(0, u));
while(!que.empty()) {
int x = que.top().second;
que.pop();
if(vis[x])
continue;
vis[x] = 1;
for(auto e : vec[x]) {
int to = e.second;
int di = e.first;
if(dis[to] > dis[x] + di) {
dis[to] = dis[x] + di;
que.push(P(dis[to], to));
}
}
}
}
int go[N];
int main() {
int neibor, e, to;
while(~scanf("%d%d%d", &e, &neibor, &to)) {
for(int i = 0; i <= 1000; i += 1){
dis[i] = Inf;
vis[i] = false;
vec[i].clear();
}
for(int i = 0; i < e; i += 1) {
int x, y, v;
scanf("%d%d%d", &x, &y, &v);
if(x == y) continue;
add(x, y, v);
add(y, x, v);
}
for(int i = 0; i < neibor; i += 1) {
int x;
scanf("%d", &x);
add(0, x, 0);
}
for(int i = 0; i < to; i += 1)
scanf("%d", &go[i]);
Dijstra(0);
int res = Inf;
for(int i = 0; i < to; i += 1) {
if(dis[go[i]] == Inf)
continue;
res = min(res, dis[go[i]]);
}
printf("%d\n", res);
}
return 0;
}