zoukankan      html  css  js  c++  java
  • 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
    ",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; 
    }
    
  • 相关阅读:
    TeeChart的X轴,使用伪装的时间
    线程池
    修练8年C++面向对象程序设计之体会
    使用RESTClient插件数据模拟(GET,POST)提交
    :施密特建议尾随年轻的专业人士了解技术公司
    社会保障系列1《介绍》
    Centos根据系统VPS安装SendMail组件使WordPress支持E-mail
    Codeforces Round #107 (Div. 2)---A. Soft Drinking
    [Unity3D]Unity3D游戏开发3D选择场景中的对象,并显示轮廓效果强化版
    怎么样ubuntu 64 11.04 在执行32位程序
  • 原文地址:https://www.cnblogs.com/adios/p/12582544.html
Copyright © 2011-2022 走看看