迷宫问题,最短路径

POJ1502 MPI Maelstrom Dijkstra

山楂果的dijstra算法应用,这里运用邻接表存款和储蓄图

迷宫难点

迷宫问题,最短路径。POJ 1502 MPI Maelstrom / UVA 432 MPI Maelstrom / SCU 1068 MPI Maelstrom / UVALive 5398 MPI Maelstrom /ZOJ 1291 MPI Maelstrom (最短路线卡塔尔国

题意

给出图,从点1起身,求到终极一个点的时间。

小片头曲:while(scanf(“%d”,&n)卡塔尔国提交时内部存款和储蓄器超过限度,改成while(scanf(“%d”,&n)!=EOF卡塔 尔(阿拉伯语:قطر‎就AC了,不领悟为什么

Description
概念一个二维数组:
int maze[5][5] = {
0, 1, 0, 0, 0,
0, 1, 0, 1, 0,
0, 0, 0, 0, 0,
0, 1, 1, 1, 0,
0, 0, 0, 1, 0,
};
它表示三个迷宫,在那之中的1象征墙壁,0象征能够走的路,只好横着走或竖着走,不能够斜着走,须要编程序寻觅从左上角到
右下角的最短路径。

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.”

思路

单源最短路,没什么好说的。留意读入的时候的技能。

dijstra算法应用:已知定点为输入,输入图中全数任何点到该定点的最短间隔。

Input
多少个5 × 5的二维数组,表示三个迷宫。数据保险有唯风华正茂解。

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.

代码

#include 
#include 
#include 
#include 
using namespace std;
const int INF = 1000000000;
const int maxn = 110;
int n;
int edge[maxn][maxn];
int d[maxn];
bool used[maxn];
void dijkstra(int s)
{
    d[s] = 0;
    while(true) {
        int v = -1;
        for(int i = 1 ; i <= n ; i ++) {
            if(!used[i]) {
                if(v == -1 || d[i] < d[v]) v = i;
            }
        }
        if(v == -1) break;
        used[v] = true;
        for(int i = 1 ; i <= n ; i ++) {
            d[i] = min(d[i],d[v]+edge[v][i]);
        }
    }
}
int main()
{
    //freopen("in.txt","r",stdin);
    fill(d,d+maxn,INF);
    memset(used,false,sizeof(used));
    for(int i = 0 ; i < maxn ; i ++) {
        fill(edge[i],edge[i]+maxn,INF);
        edge[i][i] = 0;
    }
    scanf("%d",&n);
    for(int i = 2 ; i <= n ; i ++) {
        for(int j = 1 ; j < i ; j ++) {
            int tmp;
            char getit;
            if(scanf("%d",&tmp))
                edge[i][j] = edge[j][i] = tmp;
            else scanf("%c",&getit);
        }
    }
    dijkstra(1);
    int ma = d[1];
    for(int i = 2 ; i <= n ; i ++) {
        ma = max(d[i],ma);
    }
    cout << ma << endl;
    return 0;
}

MPI Maelstrom Dijkstra 题意
给出图,从点1出发,求到结尾叁个点的光阴。 思路
单源最短路,没什么好说的。 注意读入的时候的手艺。…

具体做法:

Output
左上角到右下角的最短路线,格式如样例所示。

Output

Your program should output the minimum communication time required to
broadcast a message from the first processor to all the other
processors.

a.早先时,S只含有起点,即S={v},v的离开为0。U包括除v外的其它极点,即:U={别的极点},若v与U中极点u有边,则<u,v>正常常有权值,若u不是v的出边邻接点,则<u,v>权值为∞。

Sample Input
0 1 0 0 0
0 1 0 1 0
0 0 0 0 0
0 1 1 1 0
0 0 0 1 0

Sample Input

5
50
30 5
100 20 50
10 x x 10

发表评论

电子邮件地址不会被公开。 必填项已用*标注