[Java]
Package graph_ShortestPath;
// Train of thought: Build an undirected graph.
Import java. util .*;
Import java. io .*;
Public class HDU2680 {
Static int [] [] map;
Static int [] dist;
Static int n;
Public static void main (String [] args) throws IOException {
// Wait SC = new wait (System. in );
StreamTokenizer st = new StreamTokenizer (new BufferedReader (new InputStreamReader (System. in )));
Int m, s;
While (st. nextToken ()! = StreamTokenizer. TT_EOF ){
N = (int) st. nval;
St. nextToken ();
M = (int) st. nval;
St. nextToken ();
S = (int) st. nval;
Map = new int [n + 1] [n + 1];
For (int I = 1; I <= n; I ++)
Arrays. fill (map [I], Integer. MAX_VALUE );
Dist = new int [n + 1];
For (int I = 0; I <m; I ++ ){
St. nextToken ();
Int p = (int) st. nval;
St. nextToken ();
Int q = (int) st. nval;
St. nextToken ();
Int t = (int) st. nval;
If (t <map [q] [p])
Map [q] [p] = t; // returns an undirected graph.
}
St. nextToken ();
Int num = (int) st. nval;
Int min = Integer. MAX_VALUE;
Int [] arr = new int [num + 1];
For (int I = 1; I <= num; I ++ ){
St. nextToken ();
Arr [I] = (int) st. nval;
}
Dijkstra (s );
For (int I = 1; I <= num; I ++ ){
If (dist [arr [I] <min)
Min = dist [arr [I];
}
If (min> = Integer. MAX_VALUE) System. out. println (-1 );
Else System. out. println (min );
}
}
Private static void dijkstra (int ss ){
Boolean [] p = new boolean [n + 1];
For (int I = 1; I <= n; I ++ ){
P [I] = false;
If (I! = Ss) dist [I] = map [ss] [I];
}
P [ss] = true;
Dist [ss] = 0;
For (int I = 0; I <n-1; I ++ ){
Int min = Integer. MAX_VALUE;
Int k =-1;
For (int j = 1; j <= n; j ++ ){
If (! P [j] & dist [j] <min ){
Min = dist [j];
K = j;
}
}
If (k =-1) return;
P [k] = true;
For (int j = 1; j <= n; j ++ ){
If (! P [j] & map [k] [j]! = Integer. MAX_VALUE & dist [j]> dist [k] + map [k] [j])
Dist [j] = dist [k] + map [k] [j];
}
}
}
}