• PAT 1030 Travel Plan (双权最短路径+输出路径)


    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.

    思路

    双权最短路径

    代码

    #include <stdio.h>
    #include <string>
    #include <stdlib.h>
    #include <iostream>
    #include <vector>
    #include <string.h>
    #include <algorithm>
    #include <cmath>
    #include <map>
    #include <queue>
    #include <stack>
    #include <functional>
    #include <limits.h> 
    using namespace std;
    const int DIST = 2139062143;
    int N, M, s, e;
    int maze[500 + 1][500 + 1]; //记录距离 
    int cost[500 + 1][500 + 1]; //记录花销 
    int path[500 + 1]; //记录路径 
    int dist[500 + 1];
    int sum_cost[500 + 1];
    void dijkstra(){
    	bool visit[500 + 1];
    	memset(visit, 0, sizeof(visit));
    	for(int i = 0; i < N; i++){
    		dist[i] = maze[s][i];
    		sum_cost[i] = cost[i][s]; 
    		path[i] = s;
    	}
    	visit[s] = 1;
    	for(int k = 0; k < N; k++){
    		int mind = DIST;
    		int pre = s;
    		for(int i = 0; i < N; i++){
    			if(!visit[i] && dist[i] < mind){
    				mind = dist[i];
    				pre = i;
    			}
    		}
    		visit[pre] = 1;
    		for(int i = 0; i < N; i++){
    			if(!visit[i]){
    				if(dist[i] > maze[i][pre] + dist[pre]){
    					path[i] = pre;
    					dist[i] = maze[i][pre] + dist[pre];
    					sum_cost[i] = cost[i][pre] + sum_cost[pre];
    				}
    				else if(dist[i] == (maze[i][pre] + dist[pre]) && sum_cost[i] > cost[i][pre] + sum_cost[pre]){
    					path[i] = pre;
    					sum_cost[i] = cost[i][pre] + sum_cost[pre];
    				}
    			}
    		}
    	}
    }
    
    void Path(int pos){
    	if(pos != s)	Path(path[pos]);
    	cout << pos << " " ;
    }
    int main() {
    	memset(maze, 127, sizeof(maze));
    	memset(cost, 127, sizeof(cost));
    	int c1, c2, dist1, cost1;
    	scanf("%d%d%d%d", &N, &M, &s, &e);
    	for(int i = 0; i < M; i++){
    		scanf("%d%d%d%d", &c1, &c2, &dist1, &cost1);
    		maze[c1][c2] = dist1;
    		maze[c2][c1] = dist1;
    		cost[c1][c2] = cost1;
    		cost[c2][c1] = cost1;
    	}
    	dijkstra();
    	Path(e);
    	cout << dist[e] << " " << sum_cost[e] << endl;
    	return 0; 
    }
    
    
  • 相关阅读:
    Kaffeine Player:固守丰富的媒体播放器
    GIMP 2.2.15
    基于终真个常用工具
    运用 GNOME Specimen 检查字体
    LINA:让 Linux 使用法度圭表标准在 Windows 和 Mac OS X 上运转
    Openbox 3.4 公布
    DB2 9 运用拓荒(733 查验)认证指南,第 1 局部: 数据库器材与编程步伐(1)
    Wammu-挪动电话治理器
    会计人员必去十大网站(最新)
    判断一个数组的长度用 Length 还是 SizeOf ?
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12447359.html
Copyright © 2020-2023  润新知