zoukankan      html  css  js  c++  java
  • 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; 
    }
    
    
  • 相关阅读:
    ubuntu下如何安装hg(mercurial)?
    vi启动时报错:YouCompleteMe unavailable: requires Vim 7.4.1578+如何处理?
    linux中如何配置vim的别名为vi?
    linux shell中如何让$就表示为$呢?
    redhat 7.6下如何更新YUM源(仓库)?
    redhat下如何查看red hat版本号?
    javascript快速入门11--正则表达式
    javascript快速入门10--运算符,语句
    javascript快速入门9--引用类型
    javascript快速入门7--ECMAScript语法基础
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12447359.html
Copyright © 2011-2022 走看看