1. 程式人生 > >POJ-1502-MPI Maelstrom

POJ-1502-MPI Maelstrom

lse cpp return amp 信息 end sum rom using

鏈接:https://vjudge.net/problem/POJ-1502

題意:

n個點,從1號向開始選擇任意個結點發送信息,下一個結點接收到信息後可再向任意個結點發送。

同時發送信息有時間代價。代價有鄰接矩陣給出。只給出坐下全部,x為不連通。

同時為無向的。即a->b == b->a。

求每個結點都接受到信息的最小時間代價。

思路:

Dijkstra,求Dis數組中的最大值

代碼:

#include <iostream>
#include <memory.h>
#include <string>
#include <istream>
#include <sstream>
using namespace std;
const int MAXN = 100+10;
int Map[MAXN][MAXN];
int Dis[MAXN];
int Vis[MAXN];

int main()
{
    int n;
    scanf("%d",&n);

    for (int i = 1;i<=n;i++)
        for (int j = 1;j<=n;j++)
            if (i == j)
                Map[i][j] = 0;
            else
                Map[i][j] = 999999;

    for (int i = 2;i<=n;i++)
        for (int j = 1;j<i;j++)
        {
            string x;
            cin >> x;
            if (x[0] != ‘x‘)
            {
                istringstream iss(x);
                int num;
                iss >> num;
                Map[i][j] = Map[j][i] = num;
            }
        }
    for (int i = 1;i<=n;i++)
        Dis[i] = Map[1][i];
    Vis[1] = 1;
    for (int i = 1;i<=n;i++)
    {
        int w = -1,small = 999999;
        for (int j = 1;j<=n;j++)
            if (Vis[j] == 0&&Dis[j] < small)
            {
                w = j;
                small = Dis[j];
            }
        Vis[w] = 1;
        for (int j = 1;j<=n;j++)
        {
            if (Vis[j] == 0)
            {
                Dis[j] = min(Dis[j],Dis[w]+Map[w][j]);
                //cout << Dis[j] << ‘ ‘ << Dis[w] + Map[w][j] << endl;
            }
        }
    }
    int sum = 0;
    for (int i = 2;i<=n;i++)
        sum = max(sum,Dis[i]);
    cout << sum << endl;

    return 0;
}
/*
4
10
x 10
x x 10
 */

  

POJ-1502-MPI Maelstrom