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;
}
时间: 2024-11-06 08:29:05

POJ1125-Stockbroker Grapevine Floyd算法多源最短路径的相关文章

POJ 1125 Stockbroker Grapevine (Floyd算法)

Floyd算法计算每对顶点之间的最短路径的问题 题目中隐含了一个条件是一个人可以同时将谣言传递给多个人 题目最终的要求是时间最短,那么就要遍历一遍求出每个点作为源点时,最长的最短路径长是多少,再求这些值当中最小的是多少,就是题目所求 #include<bits/stdc++.h> using namespace std; int n,x,p,t; int m[120][120],dist[120][120],Max[120]; void floyd(int n,int m[][120],int

POJ1125 Stockbroker Grapevine【Floyd】

Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 27977 Accepted: 15527 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the st

hdu 2680 最短路径(dijkstra算法+多源最短路径单源化求最小值)

Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7062    Accepted Submission(s): 2301 Problem Description One day , Kiki wants to visit one of her friends. As she is liable

poj1125 Stockbroker Grapevine 最短路 dijkstral + 优先队列

// poj1125 Stockbroker Grapevine 最短路 dijkstral + 优先队列 // // 一个模板吧,留着纪念 #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> using namespace std; typedef pair<int,int> P; const i

POJ1125 Stockbroker Grapevine 多源最短路 Floyd

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

POJ1125 Stockbroker Grapevine 【Floyd】

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27431   Accepted: 15201 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

poj 1125 Stockbroker Grapevine -- floyd 全源最短路

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33164   Accepted: 18259 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th

POJ1125 Stockbroker Grapevine

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

POJ1125——Stockbroker Grapevine

Stockbroker Grapevine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 27547   Accepted: 15264 Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst th