Description:
As we all know, the securities industry relies on rumors of excessive use. You need to come up with a way to spread false intelligence among stock brokers so that your employer can take advantage of the stock market. To achieve the best results, you must spread the fastest rumors. Unfortunately, stock brokers only trust their "reliable sources", which means you must consider their contact structure before spreading rumors. It requires a specific stock broker and a certain amount of time to pass rumors to each of his colleagues. Your task is to write a program that tells you which stock broker to choose as the starting point of rumor and how much time it takes to spread the rumor to the stock broker of the whole society. This period is used to measure the time that a person in the past receives the information.
Input
Your program contains the input data of multiple groups of stock brokers. Each group starts with the number of stock brokers. The next few lines are information that every broker has access to other people, including who these people are and the time required to send the message to them. The contact information of each broker with others is in the following format: the first number at the beginning indicates a total of n contacts, followed by n pairs of integers. The first number listed in each pair of integers refers to a contact (for example, a '1' refers to a person numbered 1 ), the second step is to take minutes to pass a message to the person. There are no special punctuation or space rules. The number of each person is 1 to the number of brokers. The transfer time is from 1 to 10 minutes (including 10 minutes ). The number of brokers ranges from 1 to 100. When the number of people who enter the stock broker is 0, the program is terminated.
Output
For each group of data, your program must output one line, including the person with the highest transmission speed, and after the last person receives the message, the total time used (in integer minutes ). Some relationships your program may receive will exclude some people, that is, some people may not be able to access. If your program detects such a broken network, you only need to output the message "disjoint ". Please note that the time spent is from A to B, and it does not necessarily take the same time for B to transfer information to A, but it is also possible for such transmission.
Idea: traverse each node, find the shortest time between the node and other nodes, select the largest one from the shortest time, and then select the smallest one from the maximum time. (Haha, it's a bit difficult)
Check the Code:
[Cpp]
# Include <iostream>
Using namespace std;
Int n; // number of nodes
Int map [101] [101]; // storage edge
Bool v [101]; // mark the Array
Int dis [101]; // propagation time from the start point to the corresponding node
Int data [101]; // The longest time for node Propagation
Int qian [101]; // record the precursor node of the node in Propagation
Int dij (int form) // calculate the shortest time
{
Int I, j, k = 0;
/* Initialize the corresponding array */
Memset (v, 0,101 );
For (I = 1; I <= n; I ++) {data [I] = 0; dis [I] = 10000000; qian [I] = I ;}
For (I = 1; I <= n; I ++)
If (! V [I] & map [form] [I]) {dis [I] = map [form] [I]; qian [I] = form ;}
V [form] = 1;
/* ============================= */
Int sum = 0; // minimum record time
Int num = 1; // record the number of nodes traversed
For (I = 1; I <n; I ++)
{
Int min = 100000;
For (j = 1; j <= n; j ++) if (! V [j] & min> dis [j]) {min = dis [j]; k = j ;}
If (min = 100000) break; // if the min value does not change, the graph has an independent node.
V [k] = 1;
If (min> data [qian [k])
{
/* If min is greater than the maximum propagation time of the precursor node, you only need to add the difference between min and data [qian [k] to modify the maximum propagation time of the precursor node.
If min is less than the maximum propagation time of the precursor node, the total propagation time remains unchanged */
Sum + = min-data [qian [k];
Data [qian [k] = min;
}
Num ++;
For (j = 1; j <= n; j ++)
{
If (! V [j] & map [k] [j])
If (dis [j]> map [k] [j] + dis [k]) {dis [j] = map [k] [j] + dis [k]; qian [j] = qian [k];}
}
}
If (num! = N) return 10000000; // There are independent nodes in the figure.
Else return sum;
}
Int main ()
{
While (scanf ("% d", & n ))
{
If (n = 0) break;
Memset (map, 0, sizeof (map ));
Int I, j, k, to, w;
For (I = 1; I <= n; I ++)
{
Cin> j;
For (k = 1; k <= j; k ++)
{
Cin> to> w;
Map [I] [to] = w;
}
}
Int form = 0, min_time = 1000000;
For (I = 1; I <= n; I ++)
{Www.2cto.com
K = dij (I );
If (min_time> k) {min_time = k; form = I ;}
}
If (form = 0) cout <"disjoint" <endl; // nodes are encouraged in the figure.
Else cout <form <"" <min_time <endl;
}
Return 0;
}
Author: mylovepanning