1125:Stockbroker Grapevine (股票经纪人 Grapevine)(已翻译)

描述

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.

众所周知,股票经纪人对谣言反应过度。你已经签订合同,开发一种在股票经纪人之间传播虚假信息的方法,使你的雇主在股票市场上拥有战术优势。为了达到最大的效果,你必须以最快的方式传播谣言。不幸的是,股票经纪人只相信来自他们“可信来源”的信息,这意味着你必须考虑到他们的联系结构时,开始谣言。一个特定的股票经纪人需要一定的时间来把这个谣言传递给他的每一个同事。你的任务将是写一个程序,告诉你选择哪个股票经纪人作为你的谣言的出发点,以及它将花费的时间传播到整个股票经纪人社区。这个持续时间被度量为最后一个人接收信息所需的时间。

输入
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.

你的程序会为不同的股票经纪人输入数据。每一组都以股票经纪人的数目开始。下面是每个股票经纪人的一行,其中包含了他们联系过的人的数量,这些人是谁,以及他们传递信息给每个人所花费的时间。每个股票经纪人行的格式如下: 该行以联系人数(n)开始,后面跟着 n 对整数,每个联系人一对。每一对首先列出一个与联系人有关的号码(例如,“1”表示集合中的第一个人) ,然后是以分钟为单位向该人传递信息所需的时间。没有特殊的标点符号或间距规则。每个人的编号从1到股票经纪人的数目。传递信息所需的时间为1至10分钟(包括在内) ,而联络人数将介乎0至1名股票经纪之间。股票经纪人的数目将从1名到100名不等。输入由一组包含0(零)人的股票经纪人终止。

输出
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.

对于每一组数据,你的程序必须输出一行,其中包含传输速度最快的人,以及最后一个人在你给这个人之后多久会收到任何给定的信息,以整数分钟为单位。你的程序可能会收到一个排除某些人的连接网络,即有些人可能无法联系到。如果您的程序检测到这样一个损坏的网络,只需输出消息“不连接”。请注意,将讯息由 A 传递至 B 所需的时间,并不一定等同于讯息由 B 传递至 A 所需的时间(如果可能的话)。

样例输入
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
样例输出
3 2
3 10
来源
Southern African 2001


分析

把每个经纪人当成顶点,经纪人传给其他经纪人当做有向边,时间数就是边权,环也就是经纪人到自己置为0,不通的置为inf,即无穷大(本题inf不用开太大,太大反而结果不一样),如果一个经纪人不能把消息传给剩余的其他经纪人,那么这个传递网络是破碎的,输出disjoint,否则我们只需寻找这个经纪人传递消息最长的那个时间(因为这个最长的时间就是该经纪人传给所有人所需要的时间),然后遍历所有的经纪人寻找最小的时间,即本题是求最长路径的最小值。


代码

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
 
const int maxn = 105;
#define inf 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;
}

给个赞和关注吧

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值