由于是反着做,一定要反向加边
import java.util.*;
public class Main {
static int N=10010;
static int[] dist=new int[N];
static boolean[] st=new boolean[N];
static Map<Integer,List<Node>> adj=new HashMap<>();
static int n,m,s,INF=0x3f3f3f3f;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
adj.clear();
Arrays.fill(st,false);
int res=0x3f3f3f3f;
n = sc.nextInt();
m = sc.nextInt();
s = sc.nextInt();
for (int i = 0; i < m; i++) {
int a = sc.nextInt();
int b = sc.nextInt();
int w = sc.nextInt();
//一定要反向加边
if (!adj.containsKey(b)) adj.put(b,new ArrayList<>());
adj.get(b).add(new Node(a,w));
}
dijstral();
int w=sc.nextInt();
while (w-->0){
int end=sc.nextInt();
int t=dist[end];
res=Math.min(res,t);
}
System.out.println(res==INF?-1:res);
}
}
static void dijstral(){
Arrays.fill(dist,INF);
dist[s]=0;
PriorityQueue<int[]> queue=new PriorityQueue<>((a,b)->a[1]-b[1]);
queue.offer(new int[]{s,0});
while (!queue.isEmpty()){
int[] t=queue.poll();
int v=t[0];
if (st[v]) continue;
st[v]=true;
List<Node> list=adj.get(v);
if (list==null) continue;
for (Node node:list){
int b=node.b;
if (dist[b]>t[1]+node.w){
dist[b]=t[1]+node.w;
queue.offer(new int[]{b,dist[b]});
}
}
}
}
static class Node {
int b;
int w;
public Node(int b, int w) {
this.b = b;
this.w = w;
}
}
}