Stockbroker Grapevine
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 27824 | Accepted: 15414 |
题目来源:http://poj.org/problem?id=1125
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
题目大意:,每一组数据均有T个点的图(T个Stockerbrokes,输入0结束),后有三行,每行开头的一个数n表示当前行标i代表的Stockerbroke(i)能够讲传达给其他Stockerbroke(j)的总数(这句话说着别扭,看例子)。后面的数两两一组,分别表示Stockerbroke(j)的j是多少,以及传达给j需要的时间。
举例:
样例中数据:
2 2 4 3 5 (第一行)
表示从Stockerbroke(1)传递到Stockerbroke(2)的时间为4,传递到Stockerbroke(3)的时间为5,Stockerbroke(1)不能传递信息给其他人。(显然这是一个有向图,一行数据只是单向的)。
那么思路就比较清晰了。
而最后要求是否存在从 i - j 且把整个图连在一起的最少时间路径,存在就需要找出i和所需的时间。
代码如下:
/**********************************/
/*Problem: 1125 User: shinelin */
/*Memory: 700K Time: 0MS */
/*Language: G++ Result: Accepted */
/**********************************/
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <ctype.h>
#include <cstring>
#include <string>
#include <list>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
#define MAXN 105
#define INF 65534
int T, n;
int A[MAXN][MAXN];
void Floyd()
{
for (int k = 1; k <= T; k ++)
{
for(int i = 1; i <= T; i ++)
{
for(int j = 1; j <= T; j ++)
{
if( A[i][k] != INF && A[k][j] != INF && A[i][k] + A[k][j] < A[i][j])
{
A[i][j] = A[i][k] + A[k][j];
}
}
}
}
}
int main()
{
int j, value;
while(~scanf("%d", &T), T)
{
for(int i = 0; i < MAXN; i ++)
{
for(int j = 0; j < MAXN; j ++)
{
A[i][j] = INF;
}
}
for(int i = 1; i <= T; i ++)
{
scanf("%d", &n);
while(n --)
{
scanf("%d", &j);
scanf("%d", &value);
A[i][j] = value;
}
}
Floyd();
int time = INF;
int stock;
int i;
for(i = 1; i <= T; i ++) //以i为源,都找一遍。
{
int sum = 0;//用来找出每一个源的最大时间(最大时间才是走完整个图的)。若最大时间为INF,表示不是完全连通的。
for(int j = 1; j <= T; j ++)
{
if(i != j && A[i][j] > sum)
{
sum = A[i][j];
}
}
if(sum < time) //每个源中走完整个图的时间找出最小的。
{
stock = i;
time = sum;
}
}
if(time == INF)
{
printf("disjoint\n");
}
else
{
printf("%d %d\n", stock, time);
}
}
return 0;
}
心得:这是我做最短路的第一道题,因为之前学最短路的准备比较多,所以看到多源的最短路马上想到了Floyd算法,考虑数据量不是非常大,应该不会超时吧,然后按照想法,码了提交,一次就AC了。这道题难度不大,还要继续努力啊!