最短路径之Dijkstra算法

前言:基于上上一篇文章的续尾,我们得为了来减少时间的复杂度,所以我们来开始介绍本文的主题Dijkstra算法。


 

思想:本节介绍的求最短路径的算法是一种基于贪心策略的算法,每次新扩展一个路程最短的点,更新与其相邻的点的路程。当所有边权都为正时,由于不会存在一个路程更短的扩展过的点,所以这个点得路程永远不会再被改变,因而保证了算法的正确性。

时间复杂度:时间复杂度为 n 的平方,因为我们要先查找每一次最短边确定为离远点最近的确定点。这里就需要花费时间为 n ,然后总个数又为 n。 所以复杂度为 n 方。

理解了这个算法的思想后其实也就学会了 我们也不用讲太多直接上代码

 

import java.util.Scanner;
//求出单点到各个点得距离
//案例
//6 9
//1 2 1
//1 3 12
//2 3 9
//2 4 3
//3 5 5
//4 3 4
//4 5 13
//4 6 15
//5 6 4

//0 1 8 4 13 17
public class Dijkstra {
	public static void main(String[] args) {                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int m = scanner.nextInt();
		int [][]arrays = new int[n+1][n+1];
		int []books = new int[n+1];
		int []dis = new int[n+1];
		//初始化临街矩阵
		for(int i = 1 ;i <= n; i++) {
			for(int k = 1;k <= n; k++) {
				if(i == k) {
					arrays[i][k] = 0;
				}else {
					arrays[i][k] = Integer.MAX_VALUE;
				}
			}
		}
		//录入数据进入临界矩阵
		for(int i = 0 ; i < m ; i++){
			int tempx = scanner.nextInt();
			int tempy = scanner.nextInt();
			int tempw = scanner.nextInt();
			arrays[tempx][tempy] = tempw;
		}
		//初始化第一排数据
		for(int i = 1; i <=n; i++) {
			dis[i] = arrays[1][i];
		}
		books[1] = 1;
		
		for(int i = 1;i<=n-1;i++) {		
			int min = Integer.MAX_VALUE;
			int temp = 0;
			for(int j = 1; j <= n ;j++) {
				if(books[j]==0&&dis[j]<min) {
					min = dis[j];
					temp = j;
				}
			}
			books[temp] = 1;
			for(int g = 1; g <= n;g++) {
				if(arrays[temp][g]<Integer.MAX_VALUE) {
					if(dis[g]>arrays[temp][g]+dis[temp]) {
						dis[g] = arrays[temp][g]+dis[temp];
					}
				}
			}
		}
		for(int i = 1 ; i <= n;i++) {
			System.out.print(dis[i]+" ");
		}
		scanner.close();
	}
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值