Dijkstra算法应用 java c++版:MPI Maelstrom

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

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.

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

5
50
30 5
100 20 50
10 x x 10

Sample Output

35

四.最短路径:

Dijkstra算法:

Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不会更近,如果更近了就更新距离,这样把所有的点找遍之后就存下了起点到其他所有点的最短距离。

注意:

Dijkstra算法只能应用于不含负权值的图。因为在大多数应用中这个条件都满足,所以这种局限性并没有影响Dijkstra算法的广泛应用。

区分:

大家要注意把Dijkstra算法与寻找最小生成树的Prim算法区分开来。两者都是运行贪心法思想,但是Dijkstra算法是比较路径的长度,所以必须把起点到相应顶点之间的边的权重相加,而Prim算法则是直接比较相应边给定的权重。

#include <iostream>
#include <string.h>
using namespace std;
int map[100][100],n;
int dijstra(){
    int i,j,a=0,min,t;
    int closest[100],isV[100],lowcost[100];
    //初始化
    for(i=1;i<=n;i++){
        closest[i]=map[1][i];
        isV[i]=0;
    }
    closest[1]=0;		//因为start到start的距离为0,这里源点为1
//遍历每个点
    for(i=1;i<=n;i++){
        min=99999;
        for(j=1;j<=n;j++){
            if(!isV[j] && closest[j]<min){
                min=closest[j];
                t=j;
            }
        }
        isV[t]=1;
   //更新closest
        for(j=1;j<=n;j++)
            //********关键
            if(!isV[j] && closest[t]+map[t][j]<closest[j])
                closest[j]=closest[t]+map[t][j];
    }
    //一个循环找到最大值
    for(i=2;i<=n;i++)
        if(closest[i]>a) a=closest[i];
    return a;
}

int main(){
    char s[8],r[8];
    int i,j,k;
    while(~scanf("%d\n",&n)){
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
                if(i!=j)
                    map[i][j]=99999;
                else
                    map[i][j]=0;
        for(i=2;i<=n;i++)
            for(j=1;j<i;j++){
                scanf("%s",s);
                if(s[0]!='x')
                    map[i][j]=map[j][i]=atoi(s);	//将字符串转换为数字
            }
        printf("%d\n",dijstra());
    }
    return 0;
}
import java.util.Scanner;
public class Demo3 {
    public static class Dijkstra{
        public void getShortestPaths(int[][] adjMatrix) {
            int len = adjMatrix.length;
            int [] result = new int[111];
            boolean [] used = new boolean[111];
            used[0] = true;
            for(int i = 1;i < len;i++) {
                //result先保存第一个点到其他点的距离
                result[i] = adjMatrix[0][i];
                used[i] = false;
            }
            for(int i = 1;i < len;i++) {
                int min = Integer.MAX_VALUE;
                int k = 0;
                for(int j = 1;j < len;j++) {
                    if(!used[j] && result[j] != -1 && min > result[j]) {
                        min = result[j];
                        k = j;
                    }
                }
                used[k] = true;
                for(int j = 1;j < len;j++) {
                    if(!used[j]) {
                        if(adjMatrix[k][j] != -1 && (result[j] > min + adjMatrix[k][j] || result[j] == -1))
                            result[j] = min + adjMatrix[k][j];
                    }
                }
            }
            int an = 0;
            for (int i = 0; i < len; i++) {
                if (result[i]>an){
                    an = result[i];
                }
            }
            System.out.println(an);
        }
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            while (sc.hasNext()){
                int n = sc.nextInt();
                int [][]nums = new int[n][n];
                for (int i = 1; i < n; i++) {
                    for (int j = 0; j < i; j++) {
                        String c = sc.next();
                        if (c.equals("x")){
                            nums[i][j] = -1;
                        }else {
                            nums[i][j] =Integer.valueOf(c);
                        }
                        nums[j][i] = nums[i][j];
                    }
                }
                Dijkstra dijkstra = new Dijkstra();
                dijkstra.getShortestPaths(nums);
            }


        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值