PAT 1030 Travel Plan(30分)

1030 Travel Plan(30分)

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 integersN,M,S, andD, whereN(≤500) is the number of cities (and hence the cities are numbered from 0 toN−1);Mis the number of highways;SandDare the starting and the destination cities, respectively. ThenMlines 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函数进行第二标尺的选择。
  • 注意:
    • Dijkstra函数中进行优化路径时先清除该结点先前的前驱,在加入新的前驱u
    • dfs函数中,tempPath的倒序输出才是路径的正序。当v == st时记得先加入叶节点,计算完毕后在去除叶节点,避免后来的序列出错。

代码

方法一

  • Dijkstra算法中先记录下所有最短路径。然后dfs从这些最短路径中选出花费最小的路径。
#pragma warning(disable:4996)
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 510;
const int INF = 0x3f3f3f3f;

int G[maxn][maxn], d[maxn], weight[maxn][maxn];
bool vis[maxn];
vector<int> pre[maxn];

int n, m, st, endd;

void Dijkstra(int s)
{
	fill(d, d + maxn, INF);
	d[s] = 0;

	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++)
		{
			if (vis[j] == false && d[j] < MIN)
			{
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1)
		{
			return;
		}
		vis[u] = true;
		for (int v = 0; v < n; v++)
		{
			if (vis[v] == false && G[u][v] != INF)
			{
				if (d[u] + G[u][v] < d[v])
				{
					d[v] = d[u] + G[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				}
				else if (d[u] + G[u][v] == d[v])
				{
					pre[v].push_back(u);
				}
			}
		}
	}
}

int optionValue = INF;
vector<int> path, tempPath;

void dfs(int v)
{
	if (v == st)
	{
		tempPath.push_back(v);
		int value = 0;
		int id, idNext;
		for (int i = tempPath.size() - 1; i > 0; i--)
		{
			id = tempPath[i], idNext = tempPath[i - 1];
			value += weight[id][idNext];
		}
		if (value < optionValue)
		{
			optionValue = value;
			path = tempPath;
		}
		tempPath.pop_back();
		return;
	}

	tempPath.push_back(v);
	for (int i = 0; i < pre[v].size(); i++)
	{
		dfs(pre[v][i]);
	}
	tempPath.pop_back();
}

int main()
{
	//freopen("test.txt", "r", stdin);
	scanf("%d %d %d %d", &n, &m, &st, &endd);
	int c1, c2, dis, cost;
	fill(G[0], G[0] + maxn * maxn, INF);
	for (int i = 0; i < m; i++)
	{
		scanf("%d %d %d %d", &c1, &c2, &dis, &cost);
		G[c1][c2] = G[c2][c1] = dis;
		weight[c1][c2] = weight[c2][c1] = cost;
	}

	Dijkstra(st);
	dfs(endd);
	for (int i = path.size() - 1; i >= 0; i--)
	{
		printf("%d ", path[i]);
	}
	printf("%d %d\n",d[endd], optionValue);

	return 0;
}

方法二

  • 使pre数组总是保持最优路径。
#pragma warning(disable:4996)
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

const int maxn = 510;
const int INF = 0x3f3f3f3f;

int G[maxn][maxn], d[maxn], weight[maxn][maxn], w[maxn];
bool vis[maxn];
int pre[maxn];

int n, m, st, endd;

void Dijkstra(int s)
{
	fill(d, d + maxn, INF);
	fill(w, w + maxn, INF);
	for (int i = 0; i < n; i++) pre[i] = i;
	d[s] = 0;
	w[s] = 0;

	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INF;
		for (int j = 0; j < n; j++)
		{
			if (vis[j] == false && d[j] < MIN)
			{
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1)
		{
			return;
		}
		vis[u] = true;
		for (int v = 0; v < n; v++)
		{
			if (vis[v] == false && G[u][v] != INF)
			{
				if (d[u] + G[u][v] < d[v])
				{
					d[v] = d[u] + G[u][v];
					w[v] = w[u] + weight[u][v];
					pre[v] = u;
				}
				else if (d[u] + G[u][v] == d[v] && w[u] + weight[u][v] < w[v])
				{
					w[v] = w[u] + weight[u][v];
					pre[v] = u;
				}
			}
		}
	}
}

void dfs(int st, int v)
{
	if (v == st)
	{
		printf("%d ", st);
		return;
	}
	dfs(st, pre[v]);
	printf("%d ", v);
}

int main()
{
	//freopen("test.txt", "r", stdin);
	scanf("%d %d %d %d", &n, &m, &st, &endd);
	int c1, c2, dis, cost;
	fill(G[0], G[0] + maxn * maxn, INF);
	for (int i = 0; i < m; i++)
	{
		scanf("%d %d %d %d", &c1, &c2, &dis, &cost);
		G[c1][c2] = G[c2][c1] = dis;
		weight[c1][c2] = weight[c2][c1] = cost;
	}

	Dijkstra(st);
	dfs(st, endd);
	printf("%d %d", d[endd], w[endd]);

	return 0; 
}
posted @ 2020-03-27 17:02  稀有的猪  阅读(110)  评论(0编辑  收藏  举报