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; 
    }
    
    
  • 相关阅读:
    Spring3整合Quartz实现定时作业
    伪静态URLRewrite学习笔记
    VC 获取系统特殊文件夹的路径如:系统目录,桌面等
    正反向代理
    过虚拟机检测
    PDB符号文件信息
    Win64 驱动内核编程-33.枚举与删除对象回调
    Windows 反调试技术——OpenProcess 权限过滤
    Win10如何开启蓝屏记录?Win10开启蓝屏信息记录的方法
    Win7 x64下进程保护与文件保护(ObRegisterCallbacks)
  • 原文地址:https://www.cnblogs.com/woxiaosade/p/12447359.html
Copyright © 2011-2022 走看看