[Cpp]
// ZOJ 3396 Conference Call
// Obtain the minimum spanning tree of a specific three-point question.
// Train of thought: enumeration of any point as a support point. to connect a specific three points, you must go through a common point. Find the shortest path from these three points to the enumerated point, and take the minimum value as the answer.
# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace std;
# Define INF 50000000
# Define N 20005
# Define M 505
Int n, m, k;
Int sta [N];
Int head [M], num;
Int dis [4] [M];
Bool vis [M], mark [M];
Struct Edge {
Int from;
Int;
Int val;
Int next;
} Edge [N];
Void addedge (int from, int to, int val ){
Edge [num]. from = from;
Edge [num]. to =;
Edge [num]. val = val;
Edge [num]. next = head [from];
Head [from] = num ++;
}
Void init (){
Memset (head,-1, sizeof (head ));
Num = 0;
}
Void spfa (int s, int index ){
Memset (vis, 0, sizeof (vis ));
For (int I = 1; I <= m; ++ I)
Dis [index] [I] = INF;
Queue <int> Q;
Q. push (s );
Dis [index] [s] = 0;
Vis [s] = 1;
While (! Q. empty ()){
Int p = Q. front ();
Q. pop ();
Vis [p] = 0;
For (int I = head [p]; I! =-1; I = edge [I]. next ){
If (dis [index] [edge [I]. to]> dis [index] [p] + edge [I]. val ){
Dis [index] [edge [I]. to] = dis [index] [p] + edge [I]. val;
If (! Vis [edge [I]. to]) {
Q. push (edge [I]. );
Vis [edge [I]. to] = 1;
}
}
}
}
}
Int main (){
Int I, j, ca = 1;
Int a, B, c;
While (scanf ("% d", & n, & m, & k )! = EOF ){
Init ();
For (I = 1; I <= n; ++ I)
Scanf ("% d", & sta [I]);
For (I = 1; I <= k; ++ I ){
Scanf ("% d", & a, & B, & c );
Addedge (a, B, c );
Addedge (B, a, c );
}
Int bx;
Int qua;
Scanf ("% d", & qua );
Printf ("Case # % d \ n", ca ++ );
For (I = 1; I <= qua; ++ I ){
Scanf ("% d", & a, & B, & c );
Spfa (sta [a], 1 );
Spfa (sta [B], 2 );
Spfa (sta [c], 3 );
Int ans = INF;
For (j = 1; j <= m; ++ j)
If (dis [1] [j] + dis [2] [j] + dis [3] [j] <ans ){
Ans = dis [1] [j] + dis [2] [j] + dis [3] [j];
Bx = j;
}
Printf ("Line % d:", I );
// Printf ("bx: % d \ n", bx, dis [1] [bx], dis [2] [bx], dis [3] [bx]);
If (ans = INF)
Printf ("Impossible to connect! \ N ");
Else
Printf ("The minimum cost for this line is % d. \ n", ans );
}
}
Return 0;
}