POJ 1125 Stockbroker Grapevine

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <cstring>
#include <stack>
#include <queue>
//#include <climits>
#include <cstdio>
#include <cmath>
const int INT_MAX = (1 << 21);
using namespace std;
int d[105][105];
struct Node
{
    int x, y, dis;
} ans, maxd;
void init()
{
    for (int i = 0; i < 105; i++)
        for (int j = 0; j < 105; j++)
        {
            if (i == j)
                d[i][j] = 0;
            else
                d[i][j] = INT_MAX;
        }
}
void floyd(int n)
{
    for (int k = 0; k < n; k++)
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                if (d[i][j] > d[i][k] + d[k][j])
                    d[i][j] = d[i][k] + d[k][j];
}
int main()
{
    int n, j, i, a, b, m;

    while (cin >> n)
    {
        if (!n)
            break;
        init();
        for (i = 0; i < n; i++)
        {
            cin >> m;
            for (j = 0; j < m; j++)
            {
                cin >> a >> b;
                d[i][a-1] = b;
            }
        }
        floyd(n);
        ans.dis = INT_MAX;
        for (i = 0; i < n; i++)
        {
            maxd.dis = 0;
            for (j = 0; j < n; j++)
            {
                if (maxd.dis < d[i][j])
                {
                    maxd.dis = d[i][j];
                    maxd.x = i;
                    maxd.y = j;
                }
            }
            if (ans.dis > maxd.dis)
                ans = maxd;
        }
        if (ans.dis >= INT_MAX)
            cout << "disjoint" << endl;
        else
            cout << ans.x + 1 << ' ' << ans.dis << endl;

    }
    return 0;
}

你可能感兴趣的:(broker)