POJ 1502 dijkstra算法
来源:互联网 发布:淘宝网跳蚤街 编辑:程序博客网 时间:2024/06/11 03:29
MPI Maelstrom
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5027 Accepted: 3080
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.''
``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.
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
55030 5100 20 5010 x x 10
Sample Output
35
#include<iostream>#include<algorithm>#include <stdlib.h>#include <string.h>#include <stdio.h>#include <fstream>using namespace std;int map[105][105];int flag[105];int length[105];int dijkstra(int n){ int q,w,m,max=0; memset(flag,0,sizeof(flag)); memset(length,0,sizeof(length)); for(q=1;q<=n;q++)length[q]=map[1][q]; length[q]=0; flag[q]=1; for(q=1;q<n;q++) { int min=99999,index; for(w=1;w<=n;w++) { if(flag[w]==0&&length[w]<=min){min=length[w];index=w;} } flag[index]=1; for(w=1;w<=n;w++) { if(map[index][w]<99999&&flag[w]==0&&min+map[index][w]<length[w])length[w]=min+map[index][w]; } } for(int i=1;i<=n;i++) { if(length[i]>max)max=length[i]; } return max;}int main(){ ifstream fin("ha.txt"); int n,temp; char bitch[10]; cin>>n; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { map[i][j]=99999; map[i][i]=0; } } for(int i=2;i<=n;i++) { for(int j=1;j<=i-1;j++) { cin>>bitch; if(bitch[0]=='x')map[i][j]=map[j][i]=99999; else { temp=atoi(bitch); map[i][j]=temp; map[j][i]=temp; } } } cout<<dijkstra(n);}
0 0
- POJ 1502 dijkstra算法
- POJ-1502 MPI Maelstrom (裸dijkstra算法)
- POJ 1502 单源最短路(Dijkstra算法)
- poj-1502(最短路dijkstra算法)
- POJ 3268 dijkstra算法
- poj-1062 dijkstra算法
- POJ 3268 Dijkstra算法
- POJ 2502 Subways(Dijkstra算法)
- POJ-1847 Tram (dijkstra算法)
- Dijkstra算法变异 poj 1797
- POJ 1502 MPI Maelstrom (Dijkstra算法+输入处理)
- 单源最短路-Dijkstra算法 (poj 1502)
- poj 1502 MPI Maelstrom 单源最短路+dijkstra算法
- POJ 1502 MPI Maelstrom Dijkstra算法 atoi函数
- POJ 1502 dijkstra()
- poj 1502 MPI dijkstra
- poj 1502 Dijkstra
- POJ 3268 Silver Cow Party(Dijkstra算法)
- The BUFFERDIR connection property is invalid 异常处理
- rtsp交互命令简介及过程参数描述
- 自定义图文混排视图MyImageTextView
- 查看硬件mac物理地址的几个网点
- Java 使用接口还是抽象?
- POJ 1502 dijkstra算法
- android 广播
- RandomAccess接口可以判断List对象是否支持快速随机访问(数组的get(i)访问)
- iterator和指针区别
- SVN Working copy is too old
- ACE_Thread_Manager(v3.12)
- nodejs+express4.0+mongodb安装方法 for Linux, Mac
- java IO总结之字节流操作文件读写(高效)
- 中断处理的tasklet(小任务)机制和workqueue(工作队列)机制