MPI Maelstrom POJ - 1502 最短路Dijkstra

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

题意:有n个处理器,问从第一处理器发送信息给其他处理器所需最短时间中最大是多少,输入第一行表示有n个处理器然后建立一个n*nd矩阵,其中从i到i花费的时间为0,往下给除得是一个下三角型,比如样例,有5个处理器,第二行是A(2,1),第三行是A(3,1),A(3,2);最后一行是A(n,1)....A(n,n-1);如果输入的是‘x‘,表示两个处理之间无法进行通讯

思路:就是一个简单的最短路问题,在求出所有的从1到其他处理器的最短时间后,取其中最大的时间就是答案。

代码:
  1 #include <cstdio>
  2 #include <fstream>
  3 #include <algorithm>
  4 #include <cmath>
  5 #include <deque>
  6 #include <vector>
  7 #include <queue>
  8 #include <string>
  9 #include <cstring>
 10 #include <map>
 11 #include <stack>
 12 #include <set>
 13 #include <sstream>
 14 #include <iostream>
 15 #define mod 998244353
 16 #define eps 1e-6
 17 #define ll long long
 18 #define INF 0x3f3f3f3f
 19 using namespace std;
 20
 21 //ma存放从i到j两点之间的距离
 22 int ma[110][110];
 23 //dis存放从1到其他点之间的最短距离
 24 int dis[110];
 25 //vis标记第i个点的最短路是否已求出
 26 bool vis[110];
 27 //最短路算法
 28 void dijkstra(int n)
 29 {
 30     //初始化dis
 31     for(int i=1;i<=n;i++)
 32     {
 33         dis[i]=ma[1][i];
 34     }
 35     //初始化vis
 36     memset(vis,0,sizeof(vis));
 37     //第一个点不需要标记
 38     vis[1]=1;
 39     for(int i=1;i<=n-1;i++)
 40     {
 41         int v,mi=INF;
 42         //遍历寻找最短的时间
 43         for(int j=1;j<=n;j++)
 44         {
 45             if(!vis[j]&&dis[j]<mi)
 46             {
 47                 mi=dis[j];
 48                 v=j;
 49             }
 50         }
 51         //这个处理器被标记
 52         vis[v]=1;
 53         for(int j=1;j<=n;j++)
 54         {
 55             //取两个处理器之间时间短的
 56             dis[j]=min(dis[j],dis[v]+ma[v][j]);
 57         }
 58     }
 59 }
 60 //将字符串转换成数字
 61 int tio(string s)
 62 {
 63     int ans=0;
 64     int j=0;
 65     for(int i=s.length()-1;i>=0;i--)
 66     {
 67         ans+=(s[j]-‘0‘)*(int)pow(10,i);
 68         j++;
 69     }
 70     return ans;
 71 }
 72 int main()
 73 {
 74     int n,s;
 75     scanf("%d",&n);
 76     string str;
 77     //处理器之间的通讯初始情况应为无穷
 78     memset(ma,INF,sizeof(ma));
 79     ma[1][1]=0;
 80     for(int i=2;i<=n;i++)
 81     {
 82         //初始化矩阵的对角线
 83         ma[i][i]=0;
 84         for(int j=1;j<i;j++)
 85         {
 86             cin>>str;
 87             //判断两个处理器之间能否连接
 88             if(str!="x")
 89             {
 90                 //将字符串转换成数字
 91                 s=tio(str);
 92                 ma[i][j]=ma[j][i]=s;
 93             }
 94         }
 95     }
 96     dijkstra(n);
 97     int ans=0;
 98     //取所有最短时间中的最大数
 99     for(int i=1;i<=n;i++)
100     {
101         ans=max(ans,dis[i]);
102     }
103     printf("%d\n",ans);
104 }

原文地址:https://www.cnblogs.com/mzchuan/p/11484981.html

时间: 2024-10-13 14:33:55

MPI Maelstrom POJ - 1502 最短路Dijkstra的相关文章

MPI Maelstrom POJ 1502

http://poj.org/problem?id=1502 题意:这是一个下三角,上三角跟下三角图形一致,(若是完整的矩阵的话, 相当于 Map[i][j] 的距离为相对应的数值)这道题也一样. 不同的是 若 显示的为 ‘x’字母时, 说明Map[i][j]为正无穷, 两个点之间不通. 现在的问题是:求1到2, 1到3, .... 1到n 之中哪条路是最短的. **** 英语真的是太渣,表示看懂题意不是一般的难啊, 但是看懂题意后真的好简单 %>_<% .. 不要再考我英语了,我诚实的说四级

kuangbin专题专题四 MPI Maelstrom POJ - 1502

题目链接:https://vjudge.net/problem/POJ-1502 dijkstra板子题,题目提供下三角情况,不包含正对角线,因为有题意都为0,处理好输入,就是一个很水的题. 1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 #include <cstdio> 5 #include <string> 6 #include <vector&g

MPI Maelstrom POJ - 1502 floyd

#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> using namespace std; const int N=110; const int INF = 0x3f3f3f3f; char s[20]; int n; int f[N][N]; void init() { for(int i = 1 ; i <= n ; i++) for(int j = 1;

poj 1502 最短路+坑爹题意

链接:http://poj.org/problem?id=1502 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5249   Accepted: 3237 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed share

poj 1135 最短路 dijkstra

传送门 http://poj.org/problem?id=1135 建模分两部分:1.如果最后是关键牌倒下,那么找最短路中最长的就行--最远的倒下,其他的牌一定倒下,所以找最远的最短路 2.如果最后是普通牌倒下,那么找三角形,三角形周长的一半就是倒下的位置 到底是情况1还是情况2,自己在脑子模拟一下就能想到,还是那句话,最难倒下的倒下了,其他的一定都倒下了,若第二种情况的时间比第一种长,那么就是第二种,反之,第一种 上代码 /**********************************

poj1502 MPI Maelstrom(单源最短路)

题意:表面乍一看output是输出最小值,但仔细研究可以发现,这个最小值是从点1到所有点所花时间的最小值,其实是访问这些节点中的最大值,因为只有访问了最长时间的那个点才算访问了所有点.所以求最短路之后求最大值. #include <iostream> #include <cstring> #include <cstdio> using namespace std; const int maxn = 100 + 5; const int inf = 0x3f3f3f3f;

POJ 2387 最短路Dijkstra算法

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 33607   Accepted: 11383 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ 1502 MPI Maelstrom [最短路 Dijkstra]

传送门 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5711   Accepted: 3552 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierar

POJ 1502 MPI Maelstrom (Dijkstra 模板题)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5877   Accepted: 3654 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic