poj 1502 MPI Maelstrom

题目链接:

  http://poj.org/problem?id=1502

题目大意:

  有一个信息传递系统,含有n个处理器,传递信息的方式是:刚开始编号为1的处理器拥有信息,他可以传给下一个处理器,然后这两个拥有信息的处理器可以同时向下传递给其他两个处理器,拥有信息的四个处理器再依次如此传递,直到所有处理器都接受到信息的最短时间是多少?

解题思路:

  把传递路径画出来,看出可以转化成求从1到其他位置的最短路径中的最长路径,刚看到题目感觉没什么思路,但是建立出来模型以后用dijkstra就好啦!

 1 #include <vector>
 2 #include <queue>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <cstdlib>
 6 #include <iostream>
 7 #include <algorithm>
 8 using namespace std;
 9
10 #define maxn 110
11 #define INF 0x3f3f3f3f
12
13 int map[maxn][maxn], dist[maxn], n;
14
15 void init ();
16 void dijkstra ();
17
18 int main ()
19 {
20     while (scanf ("%d", &n) != EOF)
21     {
22         char s[20];
23         init ();
24         for (int i=1; i<=n; i++)
25             for (int j=1; j<i; j++)
26             {
27                 scanf ("%s", s);
28                 if (s[0] <= ‘9‘ && s[0] >= ‘0‘)
29                 {
30                     map[i][j] = 0;
31                     for (int k=0; s[k]; k++)
32                         map[i][j] = map[i][j] * 10 + s[k] - ‘0‘;
33                     map[j][i] = map[i][j];//路径是双向的
34                 }
35             }
36         dijkstra ();
37         int mini = 0;
38         for (int i=2; i<=n; i++)
39             mini = max(mini, dist[i]);
40         printf ("%d\n", mini);
41     }
42     return 0;
43 }
44
45 void init ()
46 {
47     int i, j;
48     for (i=0; i<maxn; i++)
49         for (j=0; j<maxn; j++)
50             if (i == j)
51                 map[i][j] = 0;//自身到达自身所用花费为零
52             else
53                 map[i][j] = INF;
54 }
55
56 void dijkstra ()
57 {
58     bool vis[maxn];
59     memset (vis, false, sizeof(vis));
60     for (int i=1; i<=n; i++)
61         dist[i] = map[1][i];
62
63     vis[1] = true;
64
65     for (int i=1; i<n; i++)
66     {
67         int temp = INF, index;
68         for (int j=2; j<=n; j++)
69         {
70             if (!vis[j] && temp > dist[j])
71             {
72                 index = j;
73                 temp = dist[j];
74             }
75         }
76
77         vis[index] = true;
78
79         for (int j=2; j<=n; j++)
80             if (!vis[j] )
81                 dist[j] = min(dist[j] , dist[index] + map[index][j]);
82     }
83 }
时间: 2024-12-21 01:34:49

poj 1502 MPI Maelstrom的相关文章

poj 1502 MPI Maelstrom(最短路)

poj 1502 MPI Maelstrom 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 Swige

POJ 1502 MPI Maelstrom (Dijkstra 模板题)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5877   Accepted: 3654 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

poj 1502 MPI Maelstrom Dijkstra算法的简单运用 ,呵呵,,我估计有很多人都没看懂什么意思,我也看了很久

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5831   Accepted: 3621 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ 1502 MPI Maelstrom (最短路)

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6329   Accepted: 3925 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

POJ 1502 MPI Maelstrom [最短路 Dijkstra]

传送门 MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5711   Accepted: 3552 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierar

(简单) POJ 1502 MPI Maelstrom,Dijkstra。

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 be

POJ 1502 MPI Maelstrom【floyd】

题目大意:求点1到所有点最短路径的最大值 思路:水题,单源最短路,网上解题清一色dijkstra,但是点数小于100显然floyd更简洁嘛 #include<cstdio> #include<string.h> #define maxn 101 #define inf 100000 using namespace std; int read(){ int f=1,x=0;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='x

POJ 1052 MPI Maelstrom

MPI Maelstrom Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5547   Accepted: 3458 Description BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchic

(poj)1502 MPI Maelstrom

题目链接:http://poj.org/problem?id=1502 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 adviso