POJ 1502(最短路)

题目:

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

中文题目:

BIT公司最近推出了一台新的超级计算机,一台32处理器的阿波罗奥德赛分布式共享内存机,带有分层通信子系统。瓦伦丁·麦基的研究顾问杰克·斯威格特(JackSwigert)要求她对新系统进行基准测试。
瓦伦丁对斯威格特说:“由于阿波罗是一台分布式共享内存机器,内存访问和通信时间并不一致。”`共享相同内存子系统的处理器之间的通信速度较快,但不在同一子系统上的处理器之间的通信速度较慢。阿波罗号和我们实验室的机器之间的通讯速度还比较慢。

“阿波罗的信息传递接口(MPI)端口工作得如何?”斯威格问。

“不太好,”瓦朗蒂娜回答。`要从一个处理器向所有其他n-1处理器广播消息,他们只需执行n-1发送的序列。它真正地序列化了一些事情并扼杀了表演。

“你能做些什么来解决这个问题吗?”

“是的,”瓦朗蒂娜笑着说。“有。一旦第一个处理器将消息发送给另一个处理器,这两个处理器就可以同时向另外两个主机发送消息。然后将有四个主机可以发送,以此类推。

“啊,这样你就可以像二叉树那样做广播了!”

“不是真正的二叉树-我们的网络有一些特殊的特性,我们应该加以利用。我们拥有的接口卡允许每个处理器同时向连接到它的任意数量的其他处理器发送消息。然而,这些信息不一定同时到达目的地-这涉及到通信费用。一般来说,我们需要考虑到网络拓扑中每个链路的通信成本,并相应地进行规划,以尽量减少广播所需的总时间。

输入

输入将描述连接n个处理器的网络的拓扑结构。输入的第一行将是n,处理器数,例如1<=n<=100。

输入的其余部分定义了一个邻接矩阵。A.邻接矩阵是正方形的,大小为n×n。它的每个条目要么是整数,要么是字符x。A(i,j)的值表示直接从节点I发送消息到节点j的开销。A(i,j)的x值表示消息不能从节点I直接发送到节点j。

请注意,对于一个节点向自己发送消息不需要网络通信,因此A(i,i)=0表示1<=i<=n。此外,您可以假设网络是无向的(消息可以相同的开销向任意方向移动),这样A(i,j)=A(j,i)。因此,只有A的(严格的)下三角部分上的条目将被提供。

您的程序的输入将是A的下三角段,即第二行输入将包含一个条目,A(2,1)。下一行将包含两个条目,A(3,1)和A(3,2),等等。

输出量

您的程序应该输出从第一个处理器向所有其他处理器广播消息所需的最小通信时间。

样本输入

5
50
30 5
100 20 50
10 x x 10

样本输出

35

题意分析:

信息从第一个处理器开始向其他处理器传播信息,当一个处理器接收到信息后,紧接着又会向其他处理器传播信息,直到所有处理器都接收到信息。题目问的是从第一个处理器向其他处理器传播信息所需要的最小通信时间。

因为传播形式是向周围扩散的,所以答案为所有点接收到信息的最短时间中的最大值。

 

附上代码(Dijkstra算法):

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<stack>
#include<queue>
#include<vector>
#define ll long long
#define Max 999999
using namespace std;
int n;
int a[105][105];
int dis[105];
void dijk()
{
	int vis[105]={0};
	int k ,mini;
	for(int i = 1  ;i<=n;i++)
	{
		mini=Max;
		for(int j = 1 ; j<=n;j++)
		{
			if(!vis[j]&&dis[j]<mini)
			{
				mini=dis[j];
		    	k=j;
			}
		}
		vis[k]=1;
		for(int j=1 ;j<=n;j++)
		{
			if(dis[j]>dis[k]+a[k][j])
			{
				dis[j]=dis[k]+a[k][j];
			}
		}
	}
}
int main()
{
	scanf("%d",&n);
	int i,j,k,l,t;
	char ch[5];
	for(i=0;i<=n;i++)
	{
		a[i][i]=0;
		for(j=0;j<i;j++)
		    a[i][j]=a[j][i]=Max;
	}
	for(i=1,l=0,t=1;i<=n;i++)
	{
		a[i][i]=0;
		for(j=1;j<i;j++)
		{
			scanf("%s",ch);
			if(ch=="x")a[i][j]=a[j][i]=Max;
			else {
				k=strlen(ch);
				while(k--)
				{
				    l=l+(ch[k]-'0')*t;
				    t=t*10;
				}
				a[i][j]=a[j][i]=l;
				l=0;t=1;
			}
		}
	}
	for(i=1;i<=n;i++)
	{
		dis[i]=a[1][i];
	}
	dijk();
	int ans=0;
	for(i=1;i<=n;i++)
	{
		ans=max(ans,dis[i]);
	}
	cout<<ans<<endl;
    return 0;
}

 

 

 

 

 

 

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值