poj 1125 Stockbroker Grapevine(最短路径)

Description

Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to spread the rumours in the fastest possible way. 

Unfortunately for you, stockbrokers only trust information coming from their "Trusted sources" This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information.

Input

Your program will input data for different sets of stockbrokers. Each set starts with a line with the number of stockbrokers. Following this is a line for each stockbroker which contains the number of people who they have contact with, who these people are, and the time taken for them to pass the message to each person. The format of each stockbroker line is as follows: The line starts with the number of contacts (n), followed by n pairs of integers, one pair for each contact. Each pair lists first a number referring to the contact (e.g. a '1' means person number one in the set), followed by the time in minutes taken to pass a message to that person. There are no special punctuation symbols or spacing rules. 

Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people. 

Output

For each set of data, your program must output a single line containing the person who results in the fastest message transmission, and how long before the last person will receive any given message after you give it to this person, measured in integer minutes. 
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message "disjoint". Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all.

Sample Input

3
2 2 4 3 5
2 1 2 3 6
2 1 2 2 2
5
3 4 4 2 8 5 3
1 5 8
4 1 6 4 10 2 7 5 2
0
2 2 5 1 5
0

Sample Output

3 2
3 10

描述:众所周知,证券业依靠的是过度的传言。你需要想出让股票经纪人传播假消息让雇主在股票市场上的占据优势的方法。为了获得最大的效果,你必须以最快的方式传播谣言。对于你来说不幸的是,股票经纪人只信任来自他们的“可信任的来源”的信息,这意味着你要考虑到他们的接触结构时的谣言。这需要一个特定的经纪人和一定的时间吧谣言传递到每个同事。你的任务是编写一个程序,告诉你选择哪一个股票经纪人为谣言的出发点,以及花费多少时间,谣言才可以传遍了整个股票经纪人社区。这个时间是根据最后一个人得到的信息的时间所衡量的。
 
输入:你的程序将包含多组股票经纪人的输入数据。每组始于一个股票经纪人的数量。每行的几组数字第一个表示股票经纪人所接触的人以及把消息传递给这个人所花费的时间。每个股票经纪人的格式如下:每行的开始n表示有几对整数,一对为一个传播人。每一对上的第一个数字指的传播人(例如,一个' 1 '的意思是人在该组中的一个),然后是花费几分钟把消息传递给那个人。没有特殊标点符号或间隔规则。

输出:对于每一组数据,你的程序必须输出一行包含的最快的消息传输人结果,以及最后一个人收到消息所花费的所有时间,分钟以整数计时。你的程序可能会收到的一些关系会排除一些人,也就是有些人可能无法访问。如果你的程序检测到这样一个破碎的网络,只需输出消息“disjoint”。请注意,所花费的时间是从A传递消息到B,B传递信息到A不一定是花费同样的传递时间,但此类传播也是可能的。

 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <stack>
 4 #include <queue>
 5 #include <cstring>
 6 #include <algorithm>
 7 using namespace std;
 8 #define INF 20
 9 #define MAX 150
10 int path[MAX][MAX];
11 int t,n;
12 void Floyd()
13 {
14     for(int k=1; k<=n; k++)
15     {
16         for(int i=1; i<=n; i++)
17         {
18             for(int j=1; j<=n; j++)
19             {
20                 if(i!=j&&path[i][j]>path[i][k]+path[k][j])
21                     path[i][j]=path[i][k]+path[k][j];
22             }
23         }
24     }
25     int max_len,Min=INF,k=0;
26     for(int i=1; i<=n; i++)
27     {
28         max_len=0;
29         for(int j=1; j<=n; j++)
30         {
31             if(i!=j&&path[i][j]>max_len)
32                 max_len=path[i][j];
33         }
34         if(max_len<Min)
35         {
36             Min=max_len;
37             k=i;
38         }
39     }
40     if(Min<INF)
41         cout<<k<<' '<<Min<<endl;
42     else
43         printf("disjoint
");
44     return ;
45 }
46 int main()
47 {
48     while(cin>>n,n)
49     {
50         memset(path,INF,sizeof(path));
51         for(int i=1; i<=n; i++)
52         {
53             scanf("%d",&t);
54             int a,b;
55             while(t--)
56             {
57                 cin>>a>>b;
58                 path[i][a]=b;
59             }
60         }
61         Floyd();
62     }
63     return 0;
64 }
View Code
 
   
原文地址:https://www.cnblogs.com/cxbky/p/4862295.html