【PAT】A1030 Travel Plan (30分)(最短路徑--難度中等)

1030 Travel Plan (30分)

題目鏈接

Problem Description
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 + Dfs(二標化最短路徑問題)。
上上一篇(簡單)
上一篇(難度中等)
本題代碼:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
using namespace std;

typedef long long ll;
#define inf 1000000000
const int maxn = 505;
int Graph[maxn][maxn];
int cost[maxn][maxn];
bool visit[maxn] = {false};
int d[maxn];
vector<int >pre[maxn];

//題目要求
int N,M,S,D;
vector<int >temp, path;
int optValue = inf;

void Dijkstra(int s)
{
	fill(d, d+N, inf);
	d[s] = 0;
	for(int i=0;i<N;i++)
	{
		int u=-1, MIN=inf;
		for(int j=0;j<N;j++)
		{
			if(!visit[j] && d[j]<MIN)
			{
				MIN = d[j];
				u = j;
			}
		}
		if(u==-1) return;
		visit[u] = true;
		for(int v=0;v<N;v++)
		{
			if(!visit[v] && Graph[u][v]!=inf)
			{
				if(d[u] + Graph[u][v] < d[v]){
					d[v] = d[u] + Graph[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if(d[u] + Graph[u][v] == d[v])
				{
					pre[v].push_back(u);
				}
			}
		}
	}
}

void Dfs(int s)
{
	if(s == S)
	{
		temp.push_back(s);
		//計算最優
		ll value=0;
		int id = temp[0];
		for(int i=1;i<temp.size();i++)
		{
			int idnext = temp[i];
			value += cost[id][idnext];
			id = idnext;
		}
		if(value < optValue){
			optValue = value;
			path = temp;
		}
		temp.pop_back();
	}
	temp.push_back(s);
	for(int i=0;i<pre[s].size();i++)
		Dfs(pre[s][i]);
	temp.pop_back();
}

int main()
{
	fill(Graph[0], Graph[0] + maxn*maxn, inf);
	cin>>N>>M>>S>>D;
	int a, b;
	for(int i=0;i<M;i++)
	{
		cin>>a>>b;
		cin>>Graph[a][b]>>cost[a][b];
		Graph[b][a] = Graph[a][b];
		cost[b][a] = cost[a][b];
	}
	Dijkstra(S);
	Dfs(D);
	for(int i=path.size()-1;i>=0;i--)
		cout<<path[i]<<" ";
	cout<<d[D]<<" "<<optValue<<endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章