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; 
    }
    
    
  • 相关阅读:
    委托经典--由浅入深讲解
    原生的AJAX
    asp.net传值
    flex做的圣杯布局
    弹性盒布局实例
    CSS3实现的几个小loading效果
    requireJS基本概念及使用流程(2)
    require.js的基本概念及使用流程(1)
    JSz中的静态方法和实例方法的分析
    前端性能优化的方法
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12447359.html
Copyright © 2011-2022 走看看