春游-PTA

春天阳光灿烂,小宋突然心血来潮想骑自行车去学校外面玩,但是到达目的地的路线不止一条,他想尽快的到达目的地,又能不需要骑太远的路, 你能帮助他吗?

输入格式:
输入包含一个测试数据,第一行有三个整数n(2 <= n <= 1000),途中可能经过的地点的个数,地点编号1~n;m(1 <= m <= 10000),为路径的条数;d(2 <= d <= n),目的地编号;其中学校为起点,默认为1。接下来m行: x y time dist , x y表示地点x,y是可以相互到达的,time,dist分别表示x到y或y到x的时间,距离。

输出格式:
按如下格式输出“花费的时间+空格+要骑的距离+空格+从学校到达目的地的路径”,路径中的两个地点之间以箭头(->)分隔。(具体见输出样例)

输入样例:
在这里给出一组输入。例如:

7 8 7
1 2 1 1
1 3 1 1
2 4 1 2
3 4 1 1
4 5 1 2
4 6 1 1
5 7 1 1
6 7 2 1

输出样例:
在这里给出相应的输出。例如:

4 5 1->3->4->5->7
本题就是最短路径的考察,不过需要理解一下题,题目所说的是,首先考虑时间最短,如果最短时间有多个,那么再考虑路径最短,下面上代码

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string.h>
#include<stdlib.h>
#include<cmath>
#include<cstdbool>
#include<algorithm>
using namespace std;
#define MVNum 10000
#define MV 1000
#define Maxint 32767
bool s[MVNum];
int path[MVNum];
typedef struct
{
	int time;
	int dist;
}trey;
trey D[MVNum];
trey arc[MVNum][MVNum];
typedef struct
{
	int vex[MV];
	int Vexnum, arcnum;
}AMGgraph;
int locate(AMGgraph G, int x)
{
	for (int i = 0; i < G.Vexnum; i++)
	{
		if (G.vex[i] == x)
		{
			return i;
		}
	}
	return -1;
}
void creat(AMGgraph& G, int* loc)
{
	cin >> G.Vexnum >> G.arcnum >> *loc;
	for (int i = 0; i < G.Vexnum; i++)
	{
		G.vex[i] = i;
	}
	for (int i = 0; i < G.Vexnum; i++)
	{
		for (int z = 0; z < G.Vexnum; z++)
		{
			arc[i][z].time = Maxint;
			arc[i][z].dist = Maxint;
		}
	}
	for (int i = 0; i < G.arcnum; i++)
	{
		int x, y;
		trey z;
		cin >> x >> y >> z.time >> z.dist;
		int x1 = x - 1;
		int y1 = y - 1;
		arc[x1][y1].time = z.time;
		arc[y1][x1].time = z.time;
		arc[x1][y1].dist = z.dist;
		arc[y1][x1].dist = z.dist;
	}
	*loc = (*loc) - 1;
}
void ShortestPath_DIJ(AMGgraph G)
{
	int n = G.Vexnum;
	for (int i = 0; i < G.Vexnum; i++)
	{
		s[i] = false;
		D[i].time = arc[0][i].time;
		if (D[i].time < Maxint)
		{
			path[i] = 0;
		}
		else
		{
			path[i] = -1;
		}
	}
	s[0] = true;
	D[0].time = 0;
	D[0].dist = 0;
	int v;
	for (int i = 0; i < n; i++)
	{
		trey min;
		min.dist = Maxint;
		min.time = Maxint;
		for (int w = 0; w < n; w++)
		{
			if (D[w].time <  min.time && !s[w])
			{
				v = w;
				min.time = D[w].time;
				min.dist = D[w].dist;
			}
		}
		for (int w = 0; w < n; w++)
		{
			if (D[w].time == min.time && D[w].dist < min.time && !s[w])
			{
				min.dist = D[w].dist;/*此处与迪杰斯特拉算法的差别就是,多一次判断,是否最短时间有多个,然后从最短时间里面找个路径最短的*/
				v = w;
			}
		}
		s[v] = true;
		for (int e = 0; e < n; e++)
		{
			if ((arc[v][e].time + D[v].time < D[e].time) && !s[e])
			{
				D[e].time = arc[v][e].time + D[v].time;
				path[e] = v;
			}
		}
	}
}
int main()
{
	int location;
	AMGgraph G;
	creat(G, &location);
	ShortestPath_DIJ(G);
	int a[1000];
	int count = 0;
	int time = 0;
	int dist = 0;
	int final = location + 1;
	while (path[location] != -1)
	{
		int ini = 0;
		a[count++] = path[location];
		ini = path[location];
		time += arc[ini][location].time;
		dist += arc[ini][location].dist;
		location = path[location];
	}
	cout << time << " " << dist << " ";
	for (int i = count - 1; i >= 0; i--)
	{
		
			cout << a[i]+1 << "->";
	
	}
	cout << final;
}

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值