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个处理器,传递信息的方式是:刚开始编号为1的处理器拥有信息,他可以传给下一个处理器,然后这两个拥有信息的处理器可以同时向下传递给其他两个处理器,拥有信息的四个处理器再依次如此传递,直到所有处理器都接受到信息,则最短时间为。
思路:套用Dijkstra即可,注意要将字符转化为数字
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<cstring>
#include<cstdio>
#include<cstdio>
#include<cmath>
#include<queue>
using namespace std;
const int MAX = 105;
const int INF = 0x3f3f3f3f;
struct A{
int arcs[MAX][MAX];//权值-->路径长度
int arcnum; //边的数目
int vexnum; //顶点的数目
}G;
bool visit[MAX]; //顶点的访问情况
int dis[MAX]; //出发点到其余点的最短距离
int v;
//单源最短路Dijkstra算法
//求start到其他点的最短路长度
int Dijkstra(int start){
for(int i=1 ; i<=G.vexnum ; i++){
dis[i] = G.arcs[start][i];
visit[i] = false;
}
visit[start] = true;
//再依次添加n-1个点,更新最短路
for(int i=1 ; i<=G.vexnum-1 ; i++){
int m = INF;
for(int j=1 ; j<=G.vexnum ; j++){
//找到距离最小的
if(!visit[j] && dis[j]<m){
m = dis[j];
v = j;
}
}
//找到最短距离的点
visit[v] = true;
for(int j=1 ; j<=G.vexnum ; j++){
//更新起始点到未访问点的最短距离
dis[j] = min(dis[j],dis[v]+G.arcs[v][j]);
}
}
int ans=0;
for(int i=1 ; i<=G.vexnum ; i++){
ans = max(ans,dis[i]);
}
return ans;
}
int main(void){
cin>>G.vexnum;
//初始化图
for(int i=1 ; i<=G.vexnum ; i++){
for(int j=1 ; j<=G.vexnum ; j++){
G.arcs[i][j] = (i==j)?0:INF;
}
}
//输入权值(路径长度)
for(int i=2 ; i<=G.vexnum ; i++){
for(int j=1 ; j<i ; j++){
char str[10];
cin>>str;
//将输入的字符串转化为整数
if(str[0] != 'x'){
G.arcs[i][j]=G.arcs[j][i]=atoi(str);
}
}
}
cout<<Dijkstra(1)<<endl;
return 0;
}