实现Dijkstra算法--最短路径

实现Dijkstra算法。

 

输入:

6 \\图中顶点个数

0

1

2

3

4

5

8  \\图中边的个数,有向边方式输入

0 2 10

0 4 30

0 5 100

1 2 5

2 3 50

3 5 10

4 3 20

4 5 60

 

输出:

1000:0   \\1000代表无穷大

10:2<-0

50:3<-4<-0

30:4<-0

60:5<-3<-4<-0

输入输出样例:1组

#1

  • 样例输入:
    6
    0
    1
    2
    3
    4
    5
    8
    0 2 10
    0 4 30
    0 5 100
    1 2 5
    2 3 50
    3 5 10
    4 3 20
    4 5 60
  • 样例输出:
    1000:0
    10:2<-0
    50:3<-4<-0
    30:4<-0
    60:5<-3<-4<-0
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct diotInformationl
    {
    		int isingather;
    		int from1_road_indexing;
    		int road_long;
    }diotInformation;
    int main()
    {
    	int n,i,j;
    	scanf("%d", &n);
    	//创建二维数组的邻接矩阵
    	int **juzhen=(int**)malloc(sizeof(int*)*n);
    	for (i = 0; i < n; i++) {
    		juzhen[i] = (int*)malloc(sizeof(int)*n);
    	}
    	for (i = 0; i < n; i++) {
    		for (j = 0; j < n; j++) {
    			juzhen[i][j] = 1000;
    		}
    	}
    	//读数
    	int *diotvalue=(int*)malloc(sizeof(int)*n);
    	for (i = 0; i < n; i++) {
    		scanf("%d", &diotvalue[i]);
    	}
    	//读路径
    	int a, b, longs,count;
    	scanf("%d", &count);
    	for (i = 0;i<count ;i++) {
    		scanf("%d %d %d", &a, &b, &longs);
    		juzhen[a][b] = longs;
    	}
    	//算法开始
    	//辅助数据结果
    	
    	int lastingather;
    	//初始化
    	diotInformation * di=(diotInformation*)malloc(sizeof(diotInformation)*n);
    	for (i = 0; i < n; i++) {
    		di[i].from1_road_indexing = -1;
    		di[i].isingather = 0;
    		di[i].road_long = 1000;
    	}
    	//开始迭代
    	di[0].from1_road_indexing = 0;
    	di[0].isingather = 1;
    	di[0].road_long = 0;
    	lastingather = 0;
    	for (i = 0; i < n - 1; i++) {
    		//对最后加入集合的那个点的所有路径进行遍历
    		for (j = 1; j < n; j++) {
    			//将是邻接点的点之间的路径信息进行更新
    			if (juzhen[lastingather][j] != 1000 ) {
    				if (di[lastingather].road_long + juzhen[lastingather][j] < di[j].road_long) {
    					di[j].road_long = di[lastingather].road_long + juzhen[lastingather][j];
    					di[j].from1_road_indexing = lastingather;
    
    				}
    				//di[j].road_long = di[lastingather].road_long + juzhen[lastingather][j] < di[j].road_long ? di[lastingather].road_long + juzhen[lastingather][j] : di[j].road_long;
    				//di[j].from1_road_indexing = di[lastingather].road_long + juzhen[lastingather][j] < di[j].road_long ? 
    				//printf_s("lastingather:%d,j:%d,%d,%d\n", lastingather, j, di[lastingather].road_long, juzhen[lastingather][j]);
    			}
    		}
    		//对集合进行遍历找出未加入集合点的最短路经的点,加入集合,并更新最后加入的点
    		int min = 1000;
    		int minindexing = 0;
    		for (j = 0; j < n; j++) {
    			if ((!di[j].isingather)&&di[j].road_long < min) {
    				min = di[j].road_long;
    				minindexing = j;
    
    			}
    		}
    		di[minindexing].isingather = 1;
    		//di[minindexing].from1_road_indexing = di[minindexing].road_long == di[lastingather].road_long + juzhen[lastingather][minindexing] ? lastingather: di[minindexing].from1_road_indexing;
    		//printf_s("%d,%d\n", minindexing, lastingather);
    		lastingather = minindexing;
    	}
    	//安路径长度由小到大输出
    	for (i = 1; i < n; i++) {
    		//printf_s("%d:%d\n", i, di[i].from1_road_indexing);
    		int callback = i;
    		printf("%d:", di[callback].road_long);
    		if (di[callback].road_long == 1000) {
    			printf("%d\n", 0);
    			continue;
    		}
    		for (;;) {
    			if (di[callback].from1_road_indexing == 0) {
    				printf("%d<-", diotvalue[callback]);
    				printf("%d\n", diotvalue[0]);
    				break;
    			}
    			else
    			{
    				printf("%d<-", diotvalue[callback]);
    				callback = di[callback].from1_road_indexing;
    			}
    		}
    	}
    	//printf_s("%d", juzhen[2][4]);
    	free(diotvalue);
    	free(di);
    	for (i = 0; i < n; i++) {
    		free(*(juzhen + i));
    	}
        return 0;
    }
    
    

     

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值