ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

PTA_20_07_图6 _旅游规划

2021-12-12 15:33:53  阅读:119  来源: 互联网

标签:distance 20 07 int graph tmpcity PTA cost tmpcity2


PTA_20_07_图6 _旅游规划

题目描述

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

输入格式

输入说明:输入数据的第1行给出4个正整数NMSD,其中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算法,应该采用邻接矩阵实现。

本题是在原来的Dijkstra算法进行改进,基本的流程跟原先算法一样

但是新加了一个判断费用的数组,并添加了比较花费金额大小的条件

代码实现

#include<stdio.h>
#include<iostream>

using namespace std;

#define MAXNUM 10
#define INFINITY 65535
struct CNode
{
	int numcity;
	int numhigh;
	int startcity;
	int endcity;
	int distance[MAXNUM][MAXNUM];
	int cost[MAXNUM][MAXNUM];
};
typedef struct CNode* Cgraph;
int c[MAXNUM];//记录花费的数组
int d[MAXNUM];//记录距离的数组

void CreateGraph(Cgraph graph)
{
	int i, j;
	int tmpcity, tmpcity2, roaddist, roadcost;
	cin >> graph->numcity >> graph->numhigh >> graph->startcity >> graph->endcity;
	for (i = 0; i < graph->numcity; i++)
	{
		for (j = 0; j < graph->numcity; j++)
		{
			if (i == j)
			{
				graph->distance[i][j] = 0;
				graph->cost[i][j] = 0;
			}
			else
			{
				graph->distance[i][j] = INFINITY;
				graph->cost[i][j] = INFINITY;
			}
		}
	}
	for (i = 0; i < graph->numhigh; i++)
	{
		cin >> tmpcity >> tmpcity2 >> roaddist >> roadcost;
		graph->distance[tmpcity][tmpcity2] = roaddist;
		graph->distance[tmpcity2][tmpcity] = graph->distance[tmpcity][tmpcity2];
		graph->cost[tmpcity][tmpcity2] = roadcost;
		graph->cost[tmpcity2][tmpcity] = graph->cost[tmpcity][tmpcity2];
	}
	
}


int Shorttravel(Cgraph graph)
{
	int i, j, k, min;
	int final[MAXNUM];
	for (i = 0; i < graph->numcity; i++)
	{
		final[i] = 0;
		(d)[i] = graph->distance[graph->startcity][i];
		(c)[i] = graph->cost[graph->startcity][i];
	}
	(d)[graph->startcity] = 0;
	(c)[graph->startcity] = 0;
	final[graph->startcity] = 1;

	//开始主循环,计算起始点到各点的最短路径
	for (i = 0; i < graph->numcity; i++)
	{
		min = INFINITY;//当前所知离原始点最短的路径
		for (j = 0; j < graph->numcity; j++)
		{
			if (!final[j] && (d)[j] < min)
			{
				k = j;
				min = (d)[j];//j离原始点更近
			}
		}
		final[k] = 1;
		for (j = 0; j < graph->numcity; j++)
		{
			if (!final[j])
			{//注意这里起始点设置的是k,k代表最小点的位置,一下就是对每一个进行遍历
				if (graph->distance[k][j] + (d)[k] < (d)[j])
				{
					(d)[j] = graph->distance[k][j] + (d)[k];
					(c)[j] = (c)[k] + graph->cost[k][j];
				}
				else if (graph->distance[k][j] + (d)[k] == (d)[j] && (c)[j] > (c)[k] + graph->cost[k][j])
				{
					(c)[j] = (c)[k] + graph->cost[k][j];
				}
			}
		}

	}
	return 0;

}




int main()
{
	Cgraph graph;
	graph = (Cgraph)malloc(sizeof(struct CNode));
	CreateGraph(graph);
	Shorttravel(graph);
	int i;
	i = graph->endcity;
	
	cout << d[i] <<" " << c[i];
}

标签:distance,20,07,int,graph,tmpcity,PTA,cost,tmpcity2
来源: https://blog.csdn.net/sxycylq/article/details/121888182

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有