首页 > 其他分享 >POJ 1502 MPI Maelstrom(最短路)

POJ 1502 MPI Maelstrom(最短路)

时间:2023-04-20 22:44:25浏览次数:38  
标签:will Maelstrom processors map int 1502 num POJ include


MPI Maelstrom


Time Limit: 1000MS

 

Memory Limit: 10000K

Total Submissions: 5476

 

Accepted: 3409


Description


BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee's research advisor, Jack Swigert, has asked her to benchmark the new system.  
``Since the Apollo is a distributed shared memory machine, memory access and communication times are not uniform,'' Valentine told Swigert. ``Communication is fast between processors that share the same memory subsystem, but it is slower between processors that are not on the same subsystem. Communication between the Apollo and machines in our lab is slower yet.''  

``How is Apollo's port of the Message Passing Interface (MPI) working out?'' Swigert asked.  

``Not so well,'' Valentine replied. ``To do a broadcast of a message from one processor to all the other n-1 processors, they just do a sequence of n-1 sends. That really serializes things and kills the performance.''  

``Is there anything you can do to fix that?''  

``Yes,'' smiled Valentine. ``There is. Once the first processor has sent the message to another, those two can then send messages to two other hosts at the same time. Then there will be four hosts that can send, and so on.''  

``Ah, so you can do the broadcast as a binary tree!''  

``Not really a binary tree -- there are some particular features of our network that we should exploit. The interface cards we have allow each processor to simultaneously send messages to any number of the other processors connected to it. However, the messages don't necessarily arrive at the destinations at the same time -- there is a communication cost involved. In general, we need to take into account the communication costs for each link in our network topologies and plan accordingly to minimize the total time required to do a broadcast.''


Input


The input will describe the topology of a network connecting n processors. The first line of the input will be n, the number of processors, such that 1 <= n <= 100.  

The rest of the input defines an adjacency matrix, A. The adjacency matrix is square and of size n x n. Each of its entries will be either an integer or the character x. The value of A(i,j) indicates the expense of sending a message directly from node i to node j. A value of x for A(i,j) indicates that a message cannot be sent directly from node i to node j.  

Note that for a node to send a message to itself does not require network communication, so A(i,i) = 0 for 1 <= i <= n. Also, you may assume that the network is undirected (messages can go in either direction with equal overhead), so that A(i,j) = A(j,i). Thus only the entries on the (strictly) lower triangular portion of A will be supplied.  

The input to your program will be the lower triangular section of A. That is, the second line of input will contain one entry, A(2,1). The next line will contain two entries, A(3,1) and A(3,2), and so on.


Output


Your program should output the minimum communication time required to broadcast a message from the first processor to all the other processors.


Sample Input


5 50 30 5 100 20 50 10 x x 10


Sample Output


35


Source


East Central North America 1996


大体意思没读懂,就1A了,有点惊讶。
题目大意:n台机器能相互传递传递信息,如果数位x时代表两台机器不能传递信息,求第一台机器传递到其他机器所需要的最短时间,所以只需要从求得的num[i]中找出最大的数就可以了,
输入要求:是一个三角形,第一行输入一个n,代表有n台机器,下面有n-1行,第二行有一个数代表map[2][1],第三行有两个数字,代表map[3][1],map[3][2],第四行有3个数字,代表map[4][1]......;剩下的就很简单了。

#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<string>
#include<stdio.h>
#include <climits>

using namespace std;

const int INF = 9999999;
const int N = 201;
int n,m;
int map[N][N];
int v[N],num[N];


int strtoint(char* str)
{
    if (str[0] == 'x') return -1;
    int ret = 0;
    while (*str)
    {
        ret = ret * 10 + *str++ - '0';
    }
    return ret;
}

void dijkstra()
{
    int s = 1;
    for(int i=0;i<=n;i++)
    {
        num[i] = INF;
        v[i] = 0;
    }
    num[s] = 0;
    for(int i=1;i<n;i++)
    {
        int min = INF;
        int k;
        for(int j=1;j<=n;j++)
        {
            if(v[j] == 0 && num[j] < min)
            {
                min = num[j];
                k = j;
            }
        }
        if(min == INF)
        {
            break;
        }
        v[k] = 1;
        for(int j=1;j<=n;j++)
        {
            if(v[j] == 0 && num[j] > num[k] + map[k][j])
            {
                num[j] = num[k] + map[k][j];
            }
        }
    }
    int ans = -1;
    for(int i=1;i<=n;i++)
    {
        if(num[i]!=INF && num[i]>ans)
        {
            ans = num[i];
        }
    }
    cout << ans << endl;
}

int main()
{
    while(cin >> n)
    {
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                map[i][j] = INF;
            }
            map[i][i] = 0;
        }
        char xx[100];
        for(int i=2;i<=n;i++)
        {
            for(int j=1;j<=i-1;j++)
            {
                cin >> xx;
                if(xx[0] != 'x')
                {
                    map[i][j] = strtoint(xx);
                    //cout << "map[i][j] = " << map[i][j] << endl;
                    map[j][i] = map[i][j];
                }


            }
        }
        dijkstra();
    }
    return 0;
}





标签:will,Maelstrom,processors,map,int,1502,num,POJ,include
From: https://blog.51cto.com/u_14834528/6210787

相关文章

  • POJ 2442 Sequence
    F- SequenceTimeLimit:6000MS     MemoryLimit:65536KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice POJ2442DescriptionGivenmsequences,eachcontainsnnon-negativeinteger.Nowwemayselectonenumber......
  • POJ3984 迷宫问题(BFS 记忆路径)
    迷宫问题TimeLimit:1000MS     MemoryLimit:65536KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice POJ3984SystemCrawler (2014-09-11)Description定义一个二维数组: intmaze[5][5]={0,1,0,0......
  • POJ 1789 Truck History
    TruckHistoryTimeLimit:2000MS     MemoryLimit:65536KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice POJ1789DescriptionAdvancedCargoMovement,Ltd.usestrucksofdifferenttypes.Sometrucksareused......
  • POJ--3050 Hopscotch(暴搜)
    记录21:362023-4-16http://poj.org/problem?id=3050reference:《挑战程序设计竞赛(第2版)》第二章练习题索引p135DescriptionThecowsplaythechild'sgameofhopscotchinanon-traditionalway.Insteadofalinearsetofnumberedboxesintowhichtohop,thec......
  • kuangbin专题一 简单搜索 迷宫问题(POJ-3984)
    迷宫问题TimeLimit:1000MS MemoryLimit:65536KDescription定义一个二维数组:intmaze[5][5]={0,1,0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,1,0,};它表示一个迷宫,其中的1表示墙壁,0表示可以走的路,只能横着走或竖着走,不能斜着走,要求编......
  • kuangbin专题一 简单搜索 罐子(POJ-3414)
    PotsTimeLimit:1000MS MemoryLimit:65536KDescriptionYouaregiventwopots,havingthevolumeofAandBlitersrespectively.Thefollowingoperationscanbeperformed:FILL(i)fillthepoti(1≤i≤2)fromthetap;DROP(i)emptythep......
  • poj2750(线段树+复杂区间合并)
    PottedFlowerPOJ-2750思路:我们将题目简单化,假设我们要求的是序列的最大连续子段和,且可以包括所有数。我们的线段树需要维护这段区间的最大前缀和pre,最大后缀和suf,区间和sum,区间连续最大和mx。那么难点就在于如何由子节点更新父节点。我们可以知道,tr[p].sum=tr[lc].sum......
  • kuangbin专题一 简单搜索 找倍数(POJ-1426)
    FindTheMultipleTimeLimit:1000MS MemoryLimit:10000KDescriptionGivenapositiveintegern,writeaprogramtofindoutanonzeromultiplemofnwhosedecimalrepresentationcontainsonlythedigits0and1.Youmayassumethatnisnotgreaterth......
  • poj2777(线段树)
    CountColorPOJ-2777思路:暴力能过,线段树维护这个区间的颜色,如果是混色则置为1,如果是单一颜色则设为这个颜色,修改就是正常的区间修改,区间查询就要变一下。还有题解是用二进制做得,可以学一下。#define_CRT_SECURE_NO_WARNINGS1#include<algorithm>#include<fstream>#inc......
  • kuangbin专题一 简单搜索 翻转(POJ-3279)
    FliptileTimeLimit:2000MS MemoryLimit:65536KDescriptionFarmerJohnknowsthatanintellectuallysatisfiedcowisahappycowwhowillgivemoremilk.HehasarrangedabrainyactivityforcowsinwhichtheymanipulateanM×Ngrid(1≤M≤15;1≤......