Dijsktra-BFS-prim算法-最短路径(java)

题目

Description

给出一个有向图(无负权值),求任意两点间的最短路径

Input

第一行为有向图中点的数量n(各点从0到n-1编号)第二行为边的数量m第三行为要求其间最短路径的两个点第四行起为m条边的信息,包括起点、终点和路径长度,以空格隔开
8
15
0 5

4 5 0.35

5 4 0.35

4 7 0.37

5 7 0.28

7 5 0.28

5 1 0.32

0 4 0.38

0 2 0.26

7 3 0.39

1 3 0.29

2 7 0.34

6 2 0.40

3 6 0.52

6 0 0.58

6 4 0.93

Output

求出输入中第三行两个点之间的最短路径长度并输出,比如上面的输入例子中,点0到点5间最短路径长度为0.73,则输出为:0.73

 

思路

要理解这个,要有prim算法的基础会更好理解。prim算法-邻接矩阵存储的无向图最小生成树(C++/java)

一维数组dist里存储的不在是到发节点的距离,而是到初始节点的距离,每次找到最小边连着的节点,对应dist更新为父节点的dist+最小边。

 

java实现

import java.text.DecimalFormat;
import java.util.Scanner;
public class Dijkstra {
    final static double INF = 55556.0;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int Vnum = sc.nextInt();//顶点数
        int Enum = sc.nextInt();//边数
        int stare = sc.nextInt();//起始点
        int end = sc.nextInt();//终点
        double AdjMatrix[][] = new double[Vnum][Vnum];//邻接矩阵
        for (int i = 0; i < Vnum; i++) {
            for (int j = 0; j < Vnum; j++) {
                AdjMatrix[i][j] = INF;
            }
        }
        for (int i = 0; i <Enum; i++) {
            int x = sc.nextInt();
            int y = sc.nextInt();
            double weight = sc.nextDouble();
            AdjMatrix[x][y] = weight;
            AdjMatrix[y][x] = weight;
        }
        
        double dist[] = new double[Vnum]; //用来记录到初始点的距离
        int path[] = new int[Vnum]; //用来记录父节点是谁
        int collected[] = new int[Vnum]; //用来记录节点是否收录 1表示已经收录  0表示未收录

        for (int i = 0; i < Vnum; i++) {
            dist[i] = AdjMatrix[stare][i];
            if(dist[i]<INF){
                path[i] = stare;
            }else{
                path[i] = -1;
            }
            collected[i] = 0;
        }

        dist[stare] = 0;
        collected[stare] = 1;

        while(true){
            int MinV = -1,V;
            double MinDist = INF;
            for (V = 0; V < Vnum; V++) {
                if(collected[V]==0 && dist[V]<MinDist){
                    MinDist = dist[V];
                    MinV = V;
                }
            }
            if(MinDist==INF){
                break;
            }
            collected[MinV] = 1;

            for (int i = 0; i < Vnum; i++) {
                if(collected[i]==0 && AdjMatrix[MinV][i]<INF){
                    if(dist[MinV]+AdjMatrix[MinV][i]<dist[i]){
                        dist[i] = dist[MinV]+AdjMatrix[MinV][i];
                        path[i] = MinV;
                    }
                }
            }

        }
        DecimalFormat df = new DecimalFormat("0.00");
        System.out.println(df.format(dist[end]));
        sc.close();
    }
}

温馨提示:要提交算法题,记得将类名改为Main。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

motu2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值