#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N = 1010, M = 20010, inf = 0x3f3f3f3f;
int n, m, s, T;
int q[N], dist[N], w[M];
int h[N], e[M], ne[M], idx;
bool st[N];
void add(int a, int b, int c){
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx ++ ;
}
int spfa()
{
memset(dist, 0x3f, sizeof dist);
q[0] = 0;
dist[0] = 0;
int hh = 0, tt = 1;
while (hh != tt)
{
int t = q[hh ++ ];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if (!st[j])
{
q[tt ++ ] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
if(dist[T] == inf) return -1;
return dist[T];
}
int main()
{
while (scanf("%d%d%d", &n, &m, &T) != -1)
{
memset(h, -1, sizeof h);
idx = 0;
while (m -- )
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
scanf("%d", &s);
while(s -- ){
int ver;
scanf("%d", &ver);
add(0, ver, 0);
}
printf("%d\n", spfa());
}
return 0;
}
解法:
创建一个虚起点, 建立与实原点建立一条边权为0的边