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