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

 采用了两种方法,Dijkstra与floyd算法

注意,Dijkstra是快的,即使Floyd简单,如测试点3,Floyd需要163ms,5倍多的差距。

测试点提示内存(KB)用时(ms)结果得分
0sample 最便宜的路不是最短路;输出2条最短路中最便宜的1762

答案正确

12 / 12
1最小N和M3402

答案正确

5 / 5
2最大N,有多条等长等价的道路22124

答案正确

4 / 4
3最大N和M, 随机完全图223643

答案正确

4 / 4

 Dijkstra

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define INFITY 10000

typedef struct ENode Edge;
struct ENode{
	int Length;
	int Price;
};

typedef struct GNode *MGraph;
struct GNode{
	int Nv;
	int Ne;
	Edge G[502][502];
};

void Build_Graph(MGraph M);
void Dijkstra(MGraph M, int Start);
int FindMin(Edge dist[], bool Visited[], int Nv);


bool Visited[502];
Edge dist[502];

int main()
{
	MGraph M;
	int Start, End;
	M = (MGraph)malloc(sizeof(struct GNode));
	scanf("%d%d%d%d", &M ->Nv, &M ->Ne, &Start, &End);
	Build_Graph(M);
	Dijkstra(M, Start);
	printf("%d %d\n", dist[End].Length, dist[End].Price);
	return 0;
}

void Build_Graph(MGraph M)
{
	int i, j;
	int start, end, length, price;
	for(i = 0; i < M ->Nv; i++){
		for(int j = 0; j < M ->Nv; j++){
			M ->G[i][j].Length = INFITY;
			M ->G[i][j].Price = INFITY;
		}
	}
	for(i = 0; i < M ->Ne; i++){
		scanf("%d%d%d%d", &start, &end, &length, &price);
		M ->G[start][end].Length = length;
		M ->G[start][end].Price = price;
		M ->G[end][start].Length = length;
		M ->G[end][start].Price = price;
	}
}
void Dijkstra(MGraph M, int Start)
{
	int i;
	int Min;
	for(i = 0; i < M ->Nv; i++){
		dist[i].Length  = M ->G[Start][i].Length;
		dist[i].Price  = M ->G[Start][i].Price;
		Visited[i] = false;
	}
	Visited[Start] = true;
	while(1){
		Min = FindMin(dist, Visited, M ->Nv);
		if(Min == -1) break;
		Visited[Min] = true;
		for(i = 0; i < M ->Nv; i++){
			if(!Visited[i] && dist[i].Length > dist[Min].Length + M ->G[Min][i].Length){
				dist[i].Length = dist[Min].Length + M ->G[Min][i].Length;
				dist[i].Price = dist[Min].Price + M ->G[Min][i].Price;
			}else if(!Visited[i] && (dist[i].Length == dist[Min].Length + M ->G[Min][i].Length)){
				if(dist[i].Price > dist[Min].Price + M ->G[Min][i].Price){
					dist[i].Price = dist[Min].Price + M ->G[Min][i].Price;
				}
			}
		}
	}
	return;
}
int FindMin(Edge dist[], bool Visited[], int Nv)
{
	int Min = INFITY;
	int Point = -1;
	for(int i = 0; i < Nv; i++){
		if(!Visited[i]){
			if(dist[i].Length < Min){
				Min = dist[i].Length;
				Point = i;
//				printf("dist[%d].length = %d, Point = %d\n", i, dist[i].Length, Point);
			}
		}
	}
//	printf("我决定了, 返回%d\n", Point);
	return Point;
}

Floyd

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

#define INFITY 10000

typedef struct ENode Edge;
struct ENode{
	int Length;
	int Price;
};

typedef struct GNode *MGraph;
struct GNode{
	int Nv;
	int Ne;
	Edge G[501][501];
};

void Build_Graph(MGraph M);
void floyd(MGraph M);

int main()
{
	MGraph M;
	int Start, End;
	M = (MGraph)malloc(sizeof(struct GNode));
	scanf("%d%d%d%d", &M ->Nv, &M ->Ne, &Start, &End);
	Build_Graph(M);
	floyd(M);
	printf("%d %d\n", M ->G[Start][End].Length, M ->G[Start][End].Price);
	return 0;
}

void Build_Graph(MGraph M)
{
	int i, j;
	int start, end, length, price;
	for(i = 0; i < M ->Nv; i++){
		for(int j = 0; j < M ->Nv; j++){
			M ->G[i][j].Length = INFITY;
			M ->G[i][j].Price = INFITY;
		}
		M ->G[i][j].Length = 0;
		M ->G[i][j].Price = 0;
	}
	for(i = 0; i < M ->Ne; i++){
		scanf("%d%d%d%d", &start, &end, &length, &price);
		M ->G[start][end].Length = length;
		M ->G[start][end].Price = price;
		M ->G[end][start].Length = length;
		M ->G[end][start].Price = price;
	}
}
void floyd(MGraph M)
{
	for(int k = 0; k < M ->Nv; k++){
		for(int i = 0; i < M ->Nv; i++){
			for(int j = 0; j < M ->Nv; j++){
				if(M ->G[i][j].Length > M ->G[i][k].Length + M ->G[k][j].Length ){
					M ->G[i][j].Length = M ->G[i][k].Length + M ->G[k][j].Length;
					M ->G[i][j].Price = M ->G[i][k].Price + M ->G[k][j].Price;
				}	
				else if(M ->G[i][j].Length == M ->G[i][k].Length + M ->G[k][j].Length){
					if(M ->G[i][j].Price > M ->G[i][k].Price + M ->G[k][j].Price){
						M ->G[i][j].Price = M ->G[i][k].Price + M ->G[k][j].Price;	
					}
				}
			}
		}
	}
	return ;
}

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值