[pat考试]1030. Travel Plan (30)

内容纲要

最短路与记录路径

1030. Travel Plan (30)

时间限制

400 ms

内存限制

65536 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
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<sstream>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<list>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
int price[505];
bool inq[505];
int mp[505][505];
int cost[505][505];
int path[505];
int pre[505];
int totalcost[505];
int main()
{
	//freopen("1.txt","r",stdin);
	int n,m,s,e;
	while (scanf("%d%d%d%d",&n,&m,&s,&e)+1)
	{
		int a,b,l,c;
		memset(mp,INF,sizeof(mp));
		memset(cost,INF,sizeof(cost));
		for(int i=0;i<m;i++)
		{
			scanf("%d%d%d%d",&a,&b,&l,&c);
			mp[a][b]=mp[b][a]=l;
			cost[a][b]=cost[b][a]=c;
		}
		memset(totalcost,INF,sizeof(totalcost));
		memset(price,INF,sizeof(price));
		memset(inq,0,sizeof(inq));
		queue<int>Q;
		Q.push(s);
		price[s] = 0; 
		totalcost[s]=0;
		while (!Q.empty())
		{
			int now=Q.front();
			Q.pop();
			inq[now]=false;
			for(int i=0;i<n;i++)
			{
				if(mp[now][i]!=INF)
				{
					int tmp=price[now]+mp[now][i];
					if(tmp<price[i])
					{
						price[i]=tmp;
						pre[i]=now;
						totalcost[i]=totalcost[now]+cost[now][i];
						if(!inq[i])
						{
							Q.push(i);
							inq[i]=true;
						}
							
					}
					else if(tmp==price[i])
					{
						if(totalcost[i]>totalcost[now]+cost[now][i])
						{
							pre[i]=now;
							totalcost[i]=totalcost[now]+cost[now][i];
						}
							
					}
				}
			}
		}
		int now=e;
		int pathh[505];
		int num=0;
		pathh[num++]=e;
		while(pre[now]!=s)
		{
			now=pre[now];
			pathh[num++]=now;
		}
		pathh[num++]=s;
		for(int i=num-1;i>=0;i--)
		{
			printf("%d ",pathh[i]);
		}
		printf("%d %d",price[e],totalcost[e]);
	}
	return 0;
}

发表回复