【PAT 1030】Travel Plan 最短路径Dijkstra

本文介绍了一种基于Dijkstra算法的最短路径求解方法,用于解决旅行者寻找从起点到终点之间的最短且成本最低的路径问题。文章详细解释了输入输出规格,并通过示例演示了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1030. Travel Plan (30)

时间限制
400 ms
内存限制
32000 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40
题意:求城市间最短路径,若两路径距离相同则选择花费最少的,输出路线。

分析:典型的最短路径算法,无向图,采用Dijkstra。每次修改minDis[]时,更新其prev(上一节点),为后面输出路线使用。

代码:

#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <cstring>

using namespace std;

//此代码使用前,需删除下面两行+后面的system("PAUSE")
ifstream fin("in.txt");
#define cin fin

#define MAXNUM 500
const int INF = 0x7fffffff;

//struct HighWay
//{
//	short distance;
//	short cost;
//	HighWay(){distance=0;cost=0;}
//}hw[MAXNUM][MAXNUM];

int dist[MAXNUM][MAXNUM],costA[MAXNUM][MAXNUM];

struct LinkHW
{
	int distance;
	int cost;
	int prev;
	//LinkHW(){distance=0;cost=0;prev=0;}
}minDis[MAXNUM],mindis;

bool visited[MAXNUM]={false};
int n = 0;

void Dijkastra(int begin,int end)
{
	int count = 1;
	int i;
	visited[begin]=true;
	int centre = begin;
	int tcen = begin;
	int tdis,tcost;
	while(count<n)
	{
		mindis.distance = INF;
		mindis.cost = INF;

		for(i=0;i<n;i++)
		{			
			if(visited[i])continue;
			if(dist[centre][i] > 0)		//存在通路
			{
				tdis = dist[centre][i] + minDis[centre].distance;
				tcost = costA[centre][i] + minDis[centre].cost;
				if(minDis[i].distance==0 || tdis<minDis[i].distance || (tdis==minDis[i].distance && tcost<minDis[i].cost))		//此通路是否比目前路径更优
				{
					minDis[i].distance = tdis;
					minDis[i].cost = tcost;
					minDis[i].prev = centre;		//记录此最短路径下的前一节点
				}
			}
			if(minDis[i].distance<mindis.distance || (minDis[i].distance==mindis.distance && minDis[i].cost<mindis.cost))	//以centre中心的通路最优化处理,寻找下一个节点
			{
				mindis.distance = minDis[i].distance;
				mindis.cost =  minDis[i].cost;
				tcen = i;
			}
		}
		if(tcen == end)break;		//若走到目标点,则提前退出
		centre = tcen;
		visited[centre] = true;
		count++;
	}
}

int main()
{
   int m,s,d;
   cin>>n>>m>>s>>d;
   memset(dist,0,sizeof(int)*MAXNUM*MAXNUM);
   memset(costA,0,sizeof(int)*MAXNUM*MAXNUM);
   memset(minDis,0,sizeof(LinkHW)*MAXNUM);
   memset(visited,0,sizeof(bool)*MAXNUM);

   int c1,c2,dis,cost;
   int i;
   for(i=0;i<m;i++)
   {
		cin>>c1>>c2>>dis>>cost;
		dist[c1][c2] = dist[c2][c1] = dis;
		costA[c1][c2] = costA[c2][c1] = cost;
   }
   Dijkastra(s,d);
   int pre = d;
   vector<int> v;
   while(pre!=s)
   {
	   v.push_back(pre);
	   pre = minDis[pre].prev;
   }
   cout<<s;
   for(i=v.size()-1;i>=0;i--)
	{
		cout<<' '<<v[i];
	}
   cout<<' '<<minDis[d].distance<<' '<<minDis[d].cost<<endl;
   system("PAUSE");
   return 0;
}

问题:

代码提交后,最后一个测试用例报“内存超限”错误。


不知为何?

发现问题的亲,请回复告知我一声,谢了。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值