您的位置:首页 > 其它

【PAT 1030】Travel Plan 最短路径Dijkstra

2013-12-12 16:20 489 查看
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;
}


问题:

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

不知为何?

发现问题的亲,请回复告知我一声,谢了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: