POJ 1125-Stockbroker Grapevine(最短路-含孤立点)

来源:互联网 发布:excel表格数据透视表 编辑:程序博客网 时间:2024/06/13 00:58
Stockbroker Grapevine
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 35170 Accepted: 19528

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

32 2 4 3 52 1 2 3 62 1 2 2 253 4 4 2 8 5 31 5 84 1 6 4 10 2 7 5 202 2 5 1 50

Sample Output

3 23 10

Source

Southern African 2001

题目意思:

有N个人,从某一个人出发,需要在最短时间内传达给每个人消息,求起始点和最短时间。
N后面有N行,第i行分别描述了:i能联系到的人数T,从i到T[j]点及其需要的时间。
有可能检测到这样一个断开的网络,有孤立点存在,此时只需输出消息“disjoint”。

解题思路:

求最短路套一个dijkstra的模板,每个人作为节点,他们之间的传达时间作为权值,建立有向图。
分别计算从各个点到其他点需要的最短时间,然后判断是否有孤立点,更新最短路;
若最后判断个数与N相同,则存在孤立点。

#include <iostream>#include <cstdio>#include <cstring>#include <vector>#include <queue>#include <algorithm>#define MAXN 1010#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295using namespace std;int cost[MAXN][MAXN],dis[MAXN],n,t;bool used[MAXN];//标识是否使用过void dijkstra(int s){    memset(used,0,sizeof(used));    fill(dis,dis+n+1,INF);    fill(used,used+n+1,false);    dis[s]=0;    while(true)    {        int v=-1;        //从未使用过的顶点中选择一个距离最小的顶点        for(int u=1; u<=n; ++u)            if(!used[u]&&(v==-1||dis[u]<dis[v]))                v=u;        if(v==-1) break;        used[v]=true;        for(int u=0; u<=n; ++u)            if(dis[u]>dis[v]+cost[v][u])                dis[u]=dis[v]+cost[v][u];    }}int main(){    ios::sync_with_stdio(false);    cin.tie(0);    int a,l;    while(cin>>n&&n)    {        for(int i=0; i<=n; ++i)            for(int j=0; j<=n; ++j)                cost[i][j]=INF;//手动初始化,不能fill了……        for(int i=1; i<=n; i++)        {            cin>>t;            for(int j=0; j<t; ++j)            {                cin>>a>>l;                cost[i][a]=l;//有向图            }        }        int cnt=0,no=-1,d=INF;//不能连接到当前点的点的数量、最短路起始点、最短路        for(int i=1; i<=n; i++)//计算各个点到其他点的最短路        {            dijkstra(i);            int *p=max_element(dis+1,dis+n+1);//最长路径            if(*p==INF) ++cnt;//有不连通的点            else            {                if(*p<d)//更新最短路                    no=i,d=*p;            }        }        if(cnt==n) cout<<"disjoint"<<endl;//有孤立点        else cout<<no<<" "<<d<<endl;    }    return 0;}/*32 2 4 3 52 1 2 3 62 1 2 2 253 4 4 2 8 5 31 5 84 1 6 4 10 2 7 5 202 2 5 1 50*/


0 0
原创粉丝点击