码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2016-01-12 15:19:22      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:

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

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

# 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算法多源最短路径

标签:

原文地址:http://www.cnblogs.com/digitalhermit/p/5124055.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!