Too many Good News! Utopia Polycom begins to offer a whole new service, Conference Call. this new service enables three users to make phone callat 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 simul Taneously. It's extremely convenient, yes?
However, it has a lot of semantic ical 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 varous, the cost for the signal to go through the wires is different as well. Assume there areNTelphones (numbered from1ToN) AndM(Numbered from1ToM) 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 telphoneA,BAndC. As showing below, the minimal total cost to make a conference call among telphone1,2,3Is12.
Input
There are multiple cases. For each test case, the first line contain three pZ limit? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> Objective + bjwvZW0 + objective + bDwvZW0 + ICg8ZW0 + bDwvZW0 + objective + bTwvZW0 + objective + IGFuZCA8ZW0 + YjwvZW0 + hybrid + YTwvZW0 + LCA8ZW0 + YjwvZW0 + KSBkb2Vzbg = "t appear in theseLLines, that means tranfer stationsA,BAre not connected.
The next line is a single integerQ(Q≤ 50). Then followQLines. Each line contains three integersX,Y,Z. You are supposed to calculate the minimal total cost to make a conference call among telphoneX,Y,Z. Each test case is seperated by a blank line. Proceed to the end of the file.
Output
For each test caseI, Print case number in the form "Case #I"In one single line. Then for each inquiryJ, Print one line in the form "LineJ: "And if the conference call can be made and the minimal total cost isMin, Print the sentense "The minimum cost for this line isMin. "Else you shoshould print" Impossible to connect! ". Look at the samples for details.
Sample Input
3 5 52151 2 62 3 13 4 24 5 31 5 1211 2 33 3 11232 3 411 2 3
Sample Output
Case #1Line 1: The minimum cost for this line is 12.Case #2Line 1: Impossible to connect!
Now a company has launched a new project that supports telephone communication between three people. Each phone call connects to the nearest Transfer Station. However, to make a call, it is necessary to transmit signals at the transfer station, the longer the transfer distance, the higher the cost. Find the shortest distance required for the call of the given three phones.
Idea: Use the floyd algorithm to find the shortest path between two cities, and then find the shortest path between the three phones based on these paths. There is still a big pitfall. We can't just enumerate the shortest circuit between the given three cities. For example, if we want to find the shortest path of A, B, and C, if it only involves A, B, and C, the obtained path is obviously not the shortest path, assuming that the path is centered on D, the obtained path must be shorter than the previous path.
Code:
# Include
# Include
# Include
# Define INF 100000000 using namespace std; int root [10004]; // used to store the nearest transfer station int f [504] [504]; int n, m, l; void init () {for (int I = 0; I <= n; I ++) for (int j = 0; j <= I; j ++) {if (I! = J) f [I] [j] = f [j] [I] = INF; else f [I] [j] = 0 ;}} void floyd () {for (int k = 1; k <= n; k ++) for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) {if (f [I] [j]> f [I] [k] + f [k] [j]) {f [I] [j] = f [I] [k] + f [k] [j] ;}} int main () {int ans = 1; while (scanf ("% d", & m, & n, & l )! = EOF) {init (); for (int I = 1; I <= m; I ++) scanf ("% d", & root [I]); int x, y, z; for (int I = 0; I
Z) f [x] [y] = f [y] [x] = z;} floyd (); scanf ("% d", & l ); printf ("Case # % d \ n", ans ++); for (int I = 1; I <= l; I ++) {scanf ("% d", & x, & y, & z); x = root [x]; y = root [y]; z = root [z]; int sum = INF; for (int I = 1; I <= n; I ++) // The Key to this question is very pitfall .... {if (sum> f [I] [x] + f [I] [y] + f [I] [z]) sum = f [I] [x] + f [I] [y] + f [I] [z];} printf ("Line % d:", I ); if (sum> = INF) printf ("Impossible to connect! \ N "); else printf (" The minimum cost for this line is % d. \ n ", sum) ;}} return 0 ;}