POJ1125-Stockbroker Grapevine Floyd算法多源最短路径

这题的思路还是比较简单,用弗洛伊德算法打表后,枚举来找到最小值

代码如下 注意最后判断时候的语句 在这里错误了很多次

# include<iostream>
# include<algorithm>

using namespace std;

int p[105][105];
const int INF = 99999999;

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++)
                p[i][j] = min(p[i][j],p[i][k]+p[k][j]);
}

int main()
{
    int m, e, t;

    while (cin >> n)
    {
        if (n == 0)
            break;

        for (int i = 1; i <= n; i++)
            for (int k = 1; k <= n; k++)
            {
                if (i == k)
                    p[i][k] = 0;
                else
                    p[i][k] = INF;
            }


        for (int i = 1; i <= n; i++)
        {
            cin >> m;

            if (m)
            {
                for (int k = 1; k <= m; k++)
                {
                    cin >> e >> t;
                    p[i][e] = t;
                }
            }
        }



        floyd();


        int mymin = INF;
        bool flag = false;
        int s,j;

        for (int i = 1; i <= n; i++)
        {
            int mymax = -INF;//注意这个初始化写在循环里

            for ( j = 1; j <= n; j++)
            {
                if (i != j)
                {
                    if (p[i][j] == INF)
                        break;
                    mymax = max(mymax, p[i][j]);
                }
                
            }

            if (j == n + 1)
                flag = true;
            else
                continue;

            
                if (mymin > mymax)
                {
                    mymin = mymax;
                    s = i;
                }
        //上面这一部分的判断条件要想清楚
        }

        if (flag)
            cout << s << " " << mymin << endl;
        else
            cout << "disjoint" << endl;

    }

    return 0;
}

 

你可能感兴趣的:(POJ1125-Stockbroker Grapevine Floyd算法多源最短路径)