POJ 1502 MPI Maelstrom (Dijkstra algorithm + input processing), pojdijkstra

Source: Internet
Author: User

POJ 1502 MPI Maelstrom (Dijkstra algorithm + input processing), pojdijkstra

MPI Maelstrom
Time Limit:1000 MS   Memory Limit:10000 K
Total Submissions:5712   Accepted:3553

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 special features of our network that we shoshould 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,. 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. that is, the second line of input will contain in one entry, A (2, 1 ). the next line will contain in two entries, A (3, 1) and A (3, 2), and so on.

Output

Your program shocould 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

Source

East Central North America 1996



Question: Information Transmission: There are n transmission machines in total. First, we need to transmit data from the No. 1 transmission machine to n-1 transmission machines. The transmission takes time, given a strict time matrix of the lower triangle (in fact, the Section not including the diagonal line under the diagonal line), a [I] [j] represents the time required to transmit data from I to j, there is no impact between data transmission, that is, the first transmitter can transmit data to other transmitters at the same time. Calculate the shortest time required for all transmission tasks.


Analysis: A very bare single-source shortest path, and according to the question, the side can not be negative, then use Dijkstra directly. After obtaining the shortest transmission time from a transmitter numbered 1 to all transmitters, the maximum value of all shortest time is the shortest time for completing the transmission task.

PS: reading this question is still disgusting. Because x exists, we need to read the number as a string and convert it to int.

Remember: An infinite number cannot be too big. If it exceeds the data type range, a negative number will be calculated. Of course, the result is incorrect.



AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace std; # define INF 123456789 # define MAXN 100 + 2int V, E; int w [MAXN] [MAXN]; int vis [MAXN], dis [MAXN]; int input () {// handwritten reading function char str [10]; scanf ("% s", & str); if (! Strcmp (str, "x") return INF; // 'X' indicates that two points have no path, and INF indicates else {int ans = 0; int len = strlen (str ); for (int I = 0; I <len; I ++) {ans = ans * 10 + str [I]-'0';} return ans ;}} void dijkstra () {// dijkstra function memset (vis, 0, sizeof (vis); // Preprocessing for (int I = 1; I <= V; I ++) dis [I] = (I = 1? 0: INF); for (int I = 1; I <= V; I ++) {// dijkstra int x, m = INF; for (int y = 1; y <= V; y ++) if (! Vis [y] & dis [y] <= m) {x = y; m = dis [x];} vis [x] = 1; for (int y = 1; y <= V; y ++) dis [y] = min (dis [y], dis [x] + w [x] [y]); // relaxation operation} int main () {# ifdef sxk freopen ("in.txt", "r ", stdin); // This is a debugging statement, which can be ignored # endif // sxk while (scanf ("% d", & V )! = EOF) {for (int I = 1; I <= V; I ++) // read data for (int j = 1; j <= I; j ++) {if (I = j) w [I] [j] = 0; else w [j] [I] = w [I] [j] = input ();} dijkstra (); // process int ans =-INF; // find the maximum hop height for (int I = 1; I <= V; I ++) {if (ans <dis [I]) ans = dis [I];} printf ("% d \ n", ans);} 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.