ZOJ Problem Set - 3396
Good News! Utopia Polycom begins to offer a whole new service, Conference Call. This new service enables three users to make phone calls at the same time and talk to each other. It's very useful when several people are discussing about one shared topic. For example, Tom, Rosemary and you plan to go picnic tomorrow. You just need to make one conference call, so that you can discuss with Tom and Rosemary simultaneously. It's extremely convenient, yes?
However, it has a lot of technological problems to start this service. It took engineers of Utopia Polycom five years to conquer all these problems. One of the biggest problem is how to make the communication line connected. To solve this problem, they have constructed a powerful net of transfer stations. Every single phone is connected to its nearest transfer station, some transfer stations are connected to each other by wires. When to make a conference call, signal will start from the dialling telphone. The signal will be transferred through transfer stations and finally reach the other two phones.
Since the distances between each two transfer stations are various, the cost for the signal to go through the wires is different as well. Assume there are n telphones(numbered from 1 to n) and m(numbered from 1 to m) transfer stations in this communication net. The total cost for a line is the sum of the costs of the wires in the line. Our problems remains, what is the minimal total cost to make a conference call among three telphone a, b and c. As showing below, the minimal total cost to make a conference call among telphone 1, 2, 3 is 12.
There are multiple cases. For each test case, the first line contain three positive integers, n (n ≤ 10000), m (m ≤ 500), l (l ≤ 10000). l means the number of wires. Then follows n lines. Each line contains one integer ti (1 ≤ ti ≤ m), which means the number of transfer station No.i telphone is connected to. Then follows l lines, each line contains three integers a, b, C (1 ≤ C ≤ 500), which means a and b transfer stations are connected to each other by a wire with the amount of cost C. If a pair (a, b) doesn't appear in these l lines, that means tranfer stations a, b are not connected.
The next line is a single integer q (q ≤ 50). Then follow q lines. Each line contains three integers x, y, z. You are supposed to calculate the minimal total cost to make a conference call among telphone x, y, z. Each test case is seperated by a blank line. Proceed to the end of the file.
For each test case i, print case number in the form "Case #i" in one single line. Then for each inquiry j, print one line in the form "Line j: " and if the conference call can be made and the minimal total cost is Min, print the sentense "The minimum cost for this line is Min." else you should print "Impossible to connect!". Look at the samples for details.
3 5 5 2 1 5 1 2 6 2 3 1 3 4 2 4 5 3 1 5 12 1 1 2 3 3 3 1 1 2 3 2 3 4 1 1 2 3
Case #1 Line 1: The minimum cost for this line is 12. Case #2 Line 1: Impossible to connect!
Author: ZHU, Yuke
Contest: ZOJ Monthly, September 2010