Poj1502_mpi Maelstrom_ Shortest way:: Simple Dijkstra

Source: Internet
Author: User

MPI Maelstrom

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 9173 Accepted: 5613

Description BIT has recently taken delivery of their new supercomputer, a processor Apollo Odyssey distributed shared M Emory machine with a hierarchical communication subsystem. Valentine McKee ' s-advisor, Jack Swigert, has asked's to benchmark the new system.
"Since the Apollo is a distributed GKFX memory machine, memory access and communication times was not uniform, ' valent Ine told Swigert. ' Communication is fast between processors that share the same memory subsystem, but it's slower between processors that is not on the same subsystem. Communication between the Apollo and machines in we 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" 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" can do "fix that?"

' Yes, ' smiled Valentine. "There is." Once The first processor has sent-the message to another, those-can-then-send messages to-the-other hosts at the same Time. Then there would be a 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 is some particular features of our network that we should exploit. The interface cards we have an allow each processor to simultaneously send messages to all number of the other processors con Nected to it. However, the messages don ' t necessarily arrive at the destinations time--same are a there cost communication Ved. In general, we need-to-take-into-account the communication-costs for each link in our network topologies and plan Accordin Gly to minimize the total time required to do a broadcast. "

Input the input would describe the topology of a network connecting n processors. The first line of the input would 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 would 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 is sent directly from the 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, assume that the network is undirected (messages can go on either direction with equal overhead) A (I,J) = A (j,i). Thus only the entries on the (strictly) lower triangular portion of A would be supplied.

The input to your program is the lower triangular section of A. That's, the second line of input would contain one entry, A (2,1). The next line would contain the 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 of the other processors.

Sample Input

5
5 x x
10

Sample Output

35

Approximate test instructions:

From node 1th, messages are delivered to all other nodes, and messages can be delivered to an unlimited number of nodes. Also, the point at which a message is received can immediately deliver a message to countless points that do not receive a message. The smallest amount of time a message travels across the network.


General idea:

The Diegester algorithm is used to find the shortest path from the starting point to all points. The biggest one of these short circuits is the answer.


#include <cstdio> #include <iostream> #include <cstdlib> using namespace std;

#define _MAX 2147483647 int map [105][105];//map record int D [105];//start to all points of the known shortest-shorted bool Dis [105];//Mark Point is already a minimum int N;

	int main () {scanf ("%d", &n);
	Char str [10]; for (int i=2; i<=n; i++) for (int j=1; j<i; J + +) {scanf ("%s", str);//Point information Enter if (*str! = ' x ') as a string map[j][i] = Ma P[I][J] = atoi (str);//bidirectional side input only once//string to Integer}/******* initialize d*******/for (int i=1; i<=n; i++) if (Map[1][i]
		! = 0) D[i] = Map[1][i];
else d[i] = _max;
		/******* Diegester algorithm *******/for (int m=1; m<=n; m++) {/**** finds a current point nearest to the starting point to take advantage of ****/int Min = _max, k; for (int i=1; i<=n; i++) if (!  Dis[i] && D[i] < min) min = d[i], k = i;//If Dis is true, the description has been used dis[k] = 1;//can be exploited, indicating that the point has reached the minimum possible value, marked as true for (int I=1; i<=n;

	i++)//Traverse and Mark if (map[k][i]! = 0 && d[i] > D[k] + map[k][i]) d[i] = D[k] + map[k][i];
	} int Max = 0; for (int i=1; i<=n; i++) if (max<d[i]) Max = D[i];
	printf ("%d\n", Max);

return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.