码迷,mamicode.com
首页 > 其他好文 > 详细

POJ-1502-MPI Maelstrom

时间:2019-01-16 00:21:12      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:cpp   str   开始   for   i++   .net   lse   题意   end   

链接: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

标签:cpp   str   开始   for   i++   .net   lse   题意   end   

原文地址:https://www.cnblogs.com/YDDDD/p/10274877.html

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