For each member's house, add the shortest distances to two fixed nodes and sum all results, counting unreachable as -1.
Medium4Shortest pathGraphHeapInterviewNo attempts yetTime limit1sMemory limit512 MBIf you are picked for the second class of the KIST Knights, the team gathers at KIST in Wolgok, Seoul and at CR Food in Yangjae to talk things over and work together. Everyone wants to reach the meeting place quickly.
Treat each place as a node and each road as an edge, and you get a graph. You are given the node number of the house of each of the N members, together with the node numbers of KIST and CR Food. The distance di of the ith member is defined as (shortest distance from the house to KIST) + (shortest distance from the house to CR Food). A shortest distance to a place that cannot be reached is defined as −1. For example, a member who cannot reach KIST and whose shortest distance to CR Food is 10 has distance −1+10=9, and a member who can reach neither KIST nor CR Food has distance −1+(−1)=−2.
Write a program that prints the value of ∑di.
The first line contains the number of KIST Knights members N, the number of places V, and the number of roads E. (1 ≤ N ≤ 100, 1 ≤ V ≤ 1000, 0 ≤ E ≤ 10000)
The second line contains the location A of KIST and the location B of CR Food. (1 ≤ A, B ≤ V)
The third line contains the house locations Hi of the N members, separated by spaces. (1 ≤ i ≤ N, 1 ≤ Hi ≤ V)
Each of the next E lines contains the two endpoints a, b of a road and its length l. (1 ≤ a, b ≤ V, 1 ≤ l ≤ 100) A road can be travelled in both directions. Several roads may join the same two places, and a road with a equal to b may appear.
Print the sum of the distances of all members on one line. A shortest distance to KIST or to CR Food that cannot be reached counts as −1.