天天看點

POJ 1125 Stockbroker Grapevine(Floyd)

題目位址:http://poj.org/problem?id=1125

題意:有一群買股票的人,如何從中間選擇一個人使得所有人都知道這個消息的時間最短

思路:先把N個人中最短路徑中最長的算出來,再從N個人最長的路徑中找出最短的

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int inf = 0x3f3f3f3f;
int map1[110][110];
int n;
void Floyd()
{
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(map1[i][j] > map1[i][k] + map1[k][j])
                {
                    map1[i][j] = map1[i][k] + map1[k][j];
                }
            }
        }
    }
}
int main()
{
    int m;
    while(scanf("%d",&n) && n)
    {
        memset(map1,inf,sizeof(map1));
        for(int i=1; i<=n; i++)
        {
           scanf("%d",&m);
           for(int j=1; j<=m; j++)
           {
               int a,b;
               scanf("%d%d",&a,&b);
               map1[i][a] = b;
           }
        }
        Floyd();
        int ans = inf;
        int l;
        for(int i=1; i<=n; i++)
        {
            int max1 = 0;
            for(int j=1; j<=n; j++)
            {
                if(i != j && map1[i][j] > max1)
                    max1 = map1[i][j];
            }
            if(max1 < ans)
            {
                ans = max1;
                l = i;
            }
        }
        if(ans == inf)
            printf("disjoint\n");
        else
            printf("%d %d\n",l,ans);
    }
    return 0;
}