POJ 1502 MPI Maelstrom「建议收藏」

POJ 1502 MPI Maelstrom「建议收藏」POJ 1502 MPI Maelstrom

大家好,又见面了,我是你们的朋友全栈君。

MPI Maelstrom
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 3896   Accepted: 2330

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

 

Floyd:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

const int N=110;
const int INF=0x3f3f3f3f;

int map[N][N],dis[N],vis[N];
int n;

int main(){

    //freopen("input.txt","r",stdin);

    char str[20];
    while(~scanf("%d",&n)){
        int i,j,k;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                map[i][j]=(i==j?0:INF);
        for(i=2;i<=n;i++)
            for(j=1;j<i;j++){
                scanf("%s",str);
                if(str[0]=='x')
                    map[i][j]=map[j][i]=INF;
                else
                    map[i][j]=map[j][i]=atoi(str);
            }
        for(k=1;k<=n;k++)
            for(i=1;i<=n;i++)
                for(j=1;j<=n;j++)
                    if(map[i][j]>map[i][k]+map[k][j])
                        map[i][j]=map[i][k]+map[k][j];
        int ans=-INF;
        for(int i=2;i<=n;i++)
            if(ans<map[1][i])
                ans=map[1][i];
        printf("%d\n",ans);
    }
    return 0;
}

 

 

Dijkstra:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>

using namespace std;

const int N=110;
const int INF=0x3f3f3f3f;

int map[N][N],dis[N],vis[N];
int n;

void Dijkstra(int src){
    int i;
    for(i=1;i<=n;i++){
        dis[i]=map[src][i];
        vis[i]=0;
    }
    dis[src]=0;
    vis[src]=1;
    int j,k,tmp;
    for(i=1;i<n;i++){
        tmp=INF;
        for(j=1;j<=n;j++)
            if(!vis[j] && tmp>dis[j]){
                tmp=dis[j];
                k=j;
            }
        if(tmp==INF)
            break;
        vis[k]=1;
        for(j=1;j<=n;j++)
            if(!vis[j] && dis[j]>dis[k]+map[k][j])
                dis[j]=dis[k]+map[k][j];
    }
}

int main(){

    //freopen("input.txt","r",stdin);

    char str[20];
    while(~scanf("%d",&n)){
        int i,j;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                map[i][j]=(i==j?0:INF);
        for(i=2;i<=n;i++)
            for(j=1;j<i;j++){
                scanf("%s",str);
                if(str[0]!='x')
                    map[i][j]=map[j][i]=atoi(str);
            }
        Dijkstra(1);
        int ans=-INF;
        for(i=2;i<=n;i++)
            if(ans<dis[i])
                ans=dis[i];
        printf("%d\n",ans);
    }
    return 0;
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/107094.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • opencv的imread函数_opencv imwrite

    opencv的imread函数_opencv imwrite近日,开始学习图像处理,思前想后决定以opencv作为实验基础。遂完成图片读取和显示功能。Imread作为常用的图像读取函数,虽然简单,但是参数的选择非常重要,直接影响到后期处理。同时在调试学习过程中也可以学习到图像处理的知识。0函数原型   Matimread(constString&filename,intflags=IMREAD_COLOR);

    2022年10月14日
    2
  • 汉字转拼音(较完整)「建议收藏」

    汉字转拼音(较完整)「建议收藏」转至:https://www.cnblogs.com/jwfgsf/articles/1348668.htmlpublicclassPingYin{#region//gb2312中的汉字编码//01-09区为特殊符号。//16-55区为一级汉字,按拼音排序。//56-87区为二级汉字,按部首/笔画排序。//每个汉字及符号以两个字节来表示…

    2022年6月21日
    29
  • 补码运算中的溢出_二进制补码运算溢出判断

    补码运算中的溢出_二进制补码运算溢出判断当两个以补码表示的负数相加时,会遇到两个问题。第一是两个负数的符号位相加,1+1后,本位为零,似乎负数相加变成了正数;其二是两个负数的数值部分之和,如果不向符号位进位,是不是就说明运算结果没有溢出?但不进位最终将导致两个负数相加成了正数,显然是错误的,这该怎么解释?如果两个以补码表示的负数的数值部分之和向符号位进位,会使运算结果依然为负数,那么这个运算结果是正确的吗?下面我们分析一下这个问题:

    2022年9月22日
    1
  • pycharm 中切换虚拟环境的总结

    pycharm 中切换虚拟环境的总结一、理清思路太重要了1、首先了解对应虚拟环境的运行机制你才能找到正确的解决方案2、分享我个人遇到的问题及解决方案首先安装好了django框架后,在cmd里面能正常运行,但是在Pycharm里面总是不能运行成功,老是报没有激活的问题。首先我们来看在cmd中成功运行的界面:在cmd里面我们切换至对应工程的路径输入命令:pythonmanage.pyrunserver127.0.0.1:8888,我们可以看到Startingdevelopmentserverathttp://127.0

    2022年8月28日
    2
  • PyTorch学习之六个学习率调整策略

    PyTorch学习之六个学习率调整策略PyTorch学习率调整策略通过torch.optim.lr_scheduler接口实现。PyTorch提供的学习率调整策略分为三大类,分别是有序调整:等间隔调整(Step),按需调整学习率(MultiStep),指数衰减调整(Exponential)和余弦退火CosineAnnealing。自适应调整:自适应调整学习率ReduceLROnPlateau。自定义调整:自定义调整学习率…

    2022年6月8日
    42
  • 学生选课管理系统 选课信息管理系统管理端「建议收藏」

    学生选课管理系统 选课信息管理系统管理端「建议收藏」学生选课信息管理系统管理端面向对象程序设计——课程设计(c++)必须使用vs,因为devc++会报错。程序详情见下面代码块或访问https://download.csdn.net/download/zhanjuex/12733258一、项目名称:学生选课信息管理系统管理端二、项目功能:(一)实现课程信息打印、查询、录入、删除、修改功能。(二)实现学生信息打印、查询、录入、删除、修改功能。(三)课程信息、学生信息交互,实现选课管理端根据学生已有学分进行选课。(包括帮助学生选课或删除学生已选课

    2022年10月9日
    3

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号