Stockbroker Grapevine
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 28312 | Accepted: 15711 |
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.
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.
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.
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
解题思路:
把每个经纪人当成顶点,经纪人传给其他经纪人当做有向边,时间数就是边权,环也就是经纪人到自己置为0,不通的置为inf,即无穷大(本题inf不用开太大,太大反而结果不一样),如果一个经纪人不能把消息传给剩余的其他经纪人,那么这个传递网络是破碎的,输出disjoint,否则我们只需寻找这个经纪人传递消息最长的那个时间(因为这个最长的时间就是该经纪人传给所有人所需要的时间),然后遍历所有的经纪人寻找最小的时间,即本题是求最长路径的最小值。
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 105;
#define inf 20 //本题20就可以代表无穷大了,奇怪的是如果开大了,答案就不一样了,真是奇葩
int c[maxn][maxn];
int m,n;
void floyd() //最短路floyd算法
{
int k,i,j;
for(k=1;k<=m;k++)
{
for(i=1;i<=m;i++)
{
for(j=1;j<=m;j++)
{
if(c[i][k] + c[k][j] < c[i][j])
{
c[i][j] = c[i][k] + c[k][j];
//cout<<c[i][j]<<endl;
}
}
}
}
int max_1,min_1,pos;
min_1 = inf;
pos = 0;
/*
要找的是最大路径的最小值,因为我们置两个经纪人如果不连通的话那么就是inf,inf不可能是最大路径的最小值,如果找不到一个经纪人
可以把所有经纪人联系在一起,那么就是破碎的网络,输出disjoint.
*/
for(i=1;i<=m;i++)
{
max_1 = 0;
for(j=1;j<=m;j++)
{
if(c[i][j] > max_1 && i != j)
{
max_1 = c[i][j];
}
}
if(max_1 < min_1)
{
min_1 = max_1;
pos = i;
}
}
if(min_1 < inf)
cout<<pos<<" "<<min_1<<endl;
else
cout<<"disjoint"<<endl;
}
int main()
{
int i,j;
int num1,num2;
//freopen("111","r",stdin);
while(cin>>m && m)
{
memset(c,inf,sizeof(c));
for(i=1;i<=m;i++)
{
cin>>n;
for(j=1;j<=n;j++)
{
cin>>num1>>num2;
c[i][num1] = num2;
}
}
for(i=1;i<=m;i++)
{
c[i][i] = 0;
}
floyd();
}
return 0;
}