MOOC 数据结构 07-图6 旅游规划

有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。

输入格式:

输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2≤N≤500)是城市的个数,顺便假设城市的编号为0~(N−1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。

输出格式:

在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。

输入样例:

4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20

输出样例:

3 40

 思路和小结:这是一道标准的有权图的单源最短路径问题,核心算法是【Diskstra】,复杂点在于边的权重有两个,根据题目意思主要考虑其中一个,当这一个的值相等时,再去考虑另一个。

先来复习下【Dijkstra算法】:

3个数组  最短路径值dist[],具体路径path[],是否已得出最短路径collected[]

第一步:初始化   顶点          0      1      2      3               6

                             dist          ∞      ∞      ∞     ∞     ∞     ∞     ∞

                             path        -1      -1     -1     -1    -1    -1    -1

                             collected  F       F      F      F     F     F     F 

第二步:根据给出源点S,   ①更新S的三个数组的值 dist[S] = 0;collected[S] = true;②更新 与S相连每个顶点V 的三个数组的值 dist[V] = <S,V>,path[V] = S; 

        比如源点2,与2相连有4,6,权重分别为12,86:

              顶点        0      1       2      3     4        5      6

             dist          ∞      ∞      0      ∞     12     ∞     86

             path        -1      -1     -1     -1     2      -1      2

            collected  F       F      T      F     F       F      F 

第三步: 进入循环 → 找出 dist值最小 且 collected == false 的顶点V(找到顶点4) → 更新collected[V]=true → 对V的每个相连的顶点W(比如0,1),作判断 → 没有被处理过(collected[W]==false) → 有更短的路径(dist[V]+<V,W>  < dist[W]) → 更新W的最短路径值(dist[W] = dist[V] + <V,W>) 和路径 (path[W] = V) → 继续循环

循环结束条件:找不到符合要求的顶点V(dist[V] != ∞ && collected[V] == false)

如<4,0> = 32 <4,1> = 8;那么第一轮循环走完

              顶点        0      1       2      3     4       5      6

             dist        44     20      0      ∞    12     ∞     86

             path        4            -1     -1     2      -1      2

            collected  F       F      T      F     T       F      F 

接着找到的顶点是 1(collected[1]==false && dist[1]最小) 

【Dijkstra算法】是基础,需要牢固掌握;诸君共勉。

 解题:题目有多个权重,不妨将这些权重放在一个结构体中,需要用哪个时就取用哪个

typedef struct _WeightType{
	int Distant;
	int Cost;
}WeightType;

 我用邻接矩阵和邻接表都写了下,对于稀疏图和稠密图,时间和空间上差别还是蛮大的

邻接矩阵:

邻接表 

完整代码如下: 

邻接矩阵

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 500
#define INFINITY 250000
#define ERROR -1
//顶点
typedef int Vertex;
//权重
typedef struct _WeightType{
	int Distant;
	int cost;
}WeightType;
//边
typedef struct _Edge{
	Vertex V1,V2;
	WeightType Weight;
}Edge;
//邻接矩阵图
typedef struct _MGraph{
	int Nv;
	int Ne;
	WeightType G[MAXSIZE][MAXSIZE];
}MGraph;

MGraph* BuildGraph( int N,int M );

void InsertEdge( MGraph* Graph,Edge E );

void TourismPlanning( MGraph* Graph,Vertex Start,Vertex Destination );

void Dijkstra( MGraph* Graph,Vertex Start,WeightType dist[],Vertex path[],bool collected[] );

Vertex FindMinDist( MGraph* Graph,WeightType dist[],bool collected[] );

int main()
{
	int N,M;
	Vertex Start,Destination;
	
	scanf("%d %d %d %d",&N,&M,&Start,&Destination);
	
	MGraph* Graph = BuildGraph( N, M);
	
	TourismPlanning( Graph,Start,Destination );
	
	return 0;
}
//建图
MGraph* BuildGraph( int N,int M )
{
	MGraph* Graph = (MGraph*)malloc(sizeof(MGraph));
	Graph->Nv = N;
	Graph->Ne = M;
	//初始化邻接矩阵
	Vertex V,W;
	for(V=0;V<Graph->Nv;V++){
		for(W=0;W<Graph->Nv;W++){
			
			if(V==W){
				Graph->G[V][W].Distant = 0;
				Graph->G[V][W].cost = 0;
			}else{
				Graph->G[V][W].Distant = INFINITY;
				Graph->G[V][W].cost = INFINITY;
			}
		}
	}
	//插入边
	Edge E;
	int i;
	for(i=0;i<Graph->Ne;i++){
		
		scanf("%d %d %d %d",&E.V1,&E.V2,&E.Weight.Distant,&E.Weight.cost);
		
		InsertEdge( Graph,E );
	}
	//完成建图
	return Graph;
}

void InsertEdge( MGraph* Graph,Edge E )
{
	Graph->G[E.V1][E.V2] = Graph->G[E.V2][E.V1] = E.Weight;
}

void TourismPlanning( MGraph* Graph,Vertex Start,Vertex Destination )
{//dijkstra算法的3个数组
	WeightType dist[Graph->Nv];
	Vertex path[Graph->Nv];
	bool collected[Graph->Nv];
	//根据给出的源点,初始化3个数组
	Vertex V;
	for(V=0;V<Graph->Nv;V++){
		
		dist[V] = Graph->G[Start][V];
		if(dist[V].Distant != INFINITY){
			
			path[V] = Start;
		}else{
			
			path[V] = -1;
		}
		
		collected[V] = false;
	}
	
	Dijkstra( Graph,Start,dist,path,collected );
	
	printf("%d %d\n",dist[Destination].Distant,dist[Destination].cost);
	
}
//核心Dijkstra算法
void Dijkstra( MGraph* Graph,Vertex Start,WeightType dist[],Vertex path[],bool collected[] )
{
	collected[Start] = true;
	dist[Start].Distant = 0;
	dist[Start].cost = 0;
	
	Vertex V,W;
	while(1){
		V = FindMinDist( Graph,dist,collected );
		
		if(V == ERROR) break; //循环退出条件
		
		collected[V] = true;
		
		for(W=0;W<Graph->Nv;W++){
			
			if(collected[W]==false && Graph->G[V][W].Distant < INFINITY){
				
				if(dist[V].Distant + Graph->G[V][W].Distant < dist[W].Distant){
					
					dist[W].Distant = dist[V].Distant + Graph->G[V][W].Distant;
					dist[W].cost = dist[V].cost + Graph->G[V][W].cost;
					path[W] = V;
					
				}else if(dist[V].Distant + Graph->G[V][W].Distant == dist[W].Distant){
					
					if(dist[V].cost + Graph->G[V][W].cost < dist[W].cost){
						
						dist[W].cost = dist[V].cost + Graph->G[V][W].cost;
						path[W] = V;
					}
				}
			}
		}	
	}	
}

Vertex FindMinDist( MGraph* Graph,WeightType dist[],bool collected[] )
{
	Vertex V,MinV;
	int MinDist = INFINITY;
	
	for(V=0;V<Graph->Nv;V++){
		
		if(collected[V]==false && dist[V].Distant<MinDist){
			
			MinDist = dist[V].Distant;
			MinV = V;
		}
	}
	
	if(MinDist < INFINITY){
		return MinV;
	}else{
		return ERROR;
	}
}

邻接表:

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 500
#define INFINITY 250000
#define ERROR -1
 
typedef int Vertex;

typedef struct _WeightType{
	int Distant;
	int Cost;
}WeightType;

typedef struct _Edge{
	Vertex V1,V2;
	WeightType Weight;
}Edge;

typedef struct _AdjVNode AdjVNode;
struct _AdjVNode{
	Vertex AdjV;
	WeightType Weight;
	AdjVNode* Next;
};

typedef struct _LGraph{
	int Nv;
	int Ne;
	AdjVNode** G;
}LGraph;

LGraph* CreateGraph( int N,int M );

void BuildGraph( LGraph* Graph );

void InsertEdge( LGraph* Graph,Edge E );

void TourismPlanning( LGraph* Graph,Vertex Start,Vertex Dedtination );

void Dijkstra( LGraph* Graph,Vertex Start,WeightType dist[],Vertex path[],bool collected[] );

Vertex FindMinDist( LGraph* Graph,WeightType dist[],bool collected[] );

int main()
{
	int N,M;
	Vertex Start,Destination;
	
	scanf("%d %d %d %d",&N,&M,&Start,&Destination);
	
	LGraph* Graph = CreateGraph( N,M );
	
	BuildGraph( Graph );
	
	TourismPlanning( Graph,Start,Destination );
	
	return 0;
}

LGraph* CreateGraph( int N,int M )
{
	LGraph* Graph = (LGraph*)malloc(sizeof(LGraph));
	
	Graph->Nv = N;
	Graph->Ne = M;
	Graph->G = malloc(Graph->Nv*sizeof(AdjVNode*));
	
	Vertex V;
	for(V=0;V<Graph->Nv;V++){
		
		Graph->G[V] = NULL;
	}
	
	return Graph;
}

void BuildGraph( LGraph* Graph )
{
	Edge E;
	int i;
	for(i=0;i<Graph->Ne;i++){
		
		scanf("%d %d %d %d",&E.V1,&E.V2,&E.Weight.Distant,&E.Weight.Cost);
		
		InsertEdge( Graph,E );
	}
}

void InsertEdge( LGraph* Graph,Edge E )
{
	AdjVNode* NewNode;
	
	NewNode = (AdjVNode*)malloc(sizeof(AdjVNode));
	NewNode->AdjV = E.V1;
	NewNode->Weight = E.Weight;
	NewNode->Next = Graph->G[E.V2];
	Graph->G[E.V2] = NewNode;
	
	NewNode = (AdjVNode*)malloc(sizeof(AdjVNode));
	NewNode->AdjV = E.V2;
	NewNode->Weight = E.Weight;
	NewNode->Next = Graph->G[E.V1];
	Graph->G[E.V1] = NewNode;
}

void TourismPlanning( LGraph* Graph,Vertex Start,Vertex Destination )
{
	WeightType dist[Graph->Nv];
	Vertex path[Graph->Nv];
	bool collected[Graph->Nv];
	
	Vertex V;
	for(V=0;V<Graph->Nv;V++){
		
		dist[V].Distant = dist[V].Cost = INFINITY;
		path[V] = -1;
		collected[V] = false;
	}
	
	Dijkstra( Graph,Start,dist,path,collected );
	
	printf("%d %d\n",dist[Destination].Distant,dist[Destination].Cost);
	
}

void Dijkstra( LGraph* Graph,Vertex Start,WeightType dist[],Vertex path[],bool collected[] )
{
	dist[Start].Distant = dist[Start].Cost = 0;

	AdjVNode* p;	
	for(p=Graph->G[Start];p;p=p->Next){
			
		dist[p->AdjV] = p->Weight;
		path[p->AdjV] = Start;
	}
	
	collected[Start] = true;
	
	Vertex V;
	while(1){
		V = FindMinDist( Graph,dist,collected );
		
		if(V == ERROR) break;
		
		collected[V] = true;
		
		for(p=Graph->G[V];p;p=p->Next){
			
			if(collected[p->AdjV]==false){
				if(dist[V].Distant + p->Weight.Distant < dist[p->AdjV].Distant){
					
					dist[p->AdjV].Distant = dist[V].Distant + p->Weight.Distant;
					dist[p->AdjV].Cost = dist[V].Cost + p->Weight.Cost;
					path[p->AdjV] = V;
					
				}else if(dist[V].Distant + p->Weight.Distant == dist[p->AdjV].Distant){
					
					if(dist[V].Cost + p->Weight.Cost < dist[p->AdjV].Cost){
						
						dist[p->AdjV].Cost = dist[V].Cost + p->Weight.Cost;
						path[p->AdjV] = V;
					}
				}
			}
		}
	}
}

Vertex FindMinDist( LGraph* Graph,WeightType dist[],bool collected[] )
{
	int MinDist = INFINITY;
	
	Vertex V,MinID = ERROR;
	for(V=0;V<Graph->Nv;V++){
		
		if(collected[V]==false && dist[V].Distant<MinDist){
			
			MinDist = dist[V].Distant;
			MinID = V;
		}
	}
	
	return MinID;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值