1030. Travel Plan

    xiaoxiao2021-03-25  104

    1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue

    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.

    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 题目: 求最短路径,路径长度相等的时候取cost最小的路径 思路&&解法: 通用dijkstra解体模板,所有路径保存在vector《vector《int》》里面 Code: #include <iostream> #include <string> #include <vector> #include <stdio.h> #include <algorithm> #include <math.h> #define MAXN 500 #define UNREACHABLE 1000000 int graph[MAXN][MAXN]; int weight[MAXN][MAXN]; int main() { int n, m, start_vex, des_vex; scanf("%d %d %d %d", &n, &m, &start_vex, &des_vex); if (n == 1) { printf("0 0 0"); system("pause"); return 0; } int d[MAXN]; int w[MAXN]; bool vis[MAXN]; std::fill(vis, vis+MAXN, false); std::fill(w, w+MAXN, UNREACHABLE); std::fill(d, d+MAXN, UNREACHABLE); std::fill(graph[0], graph[0]+MAXN*MAXN, UNREACHABLE); std::fill(weight[0], weight[0] + MAXN*MAXN, UNREACHABLE); for (int i = 0; i < m; i++) { int start, end, distance, cost; scanf("%d %d %d %d", &start, &end, &distance, &cost); graph[start][end] = graph[end][start] = distance; weight[start][end] = weight[end][start] = cost; } d[start_vex] = 0; std::vector<int> route[MAXN]; std::vector<std::vector<int>> all_route[MAXN]; for (int i = 0; i < n; i++) { all_route[i].push_back(route[i]); } for (int i = 0; i < n; i++) { int find_x = -1; int max_d = UNREACHABLE; for (int j = 0; j < m; j++) { if (!vis[j] && d[j] < max_d) { max_d = d[j]; find_x = j; } } vis[find_x] = true; if (find_x == -1) { continue; } route[find_x].push_back(find_x); for (int j = 0; j < all_route[find_x].size(); j++) { all_route[find_x][j].push_back(find_x); } for (int j = 0; j < n; j++) { if (!vis[j] && d[find_x] + graph[find_x][j] == d[j]) { all_route[j].push_back(route[find_x]); } else if (!vis[j] && d[find_x] + graph[find_x][j] < d[j]) { d[j] = d[find_x] + graph[find_x][j]; route[j] = route[find_x]; all_route[j] = all_route[find_x]; } } } int mark; int total_weight; int min_weight = UNREACHABLE; for (int i = 0; i < all_route[des_vex].size(); i++) { total_weight = 0; for (int j = 0; j < all_route[des_vex][i].size() -1; j++) { total_weight += weight[all_route[des_vex][i][j]][all_route[des_vex][i][j+1]]; } if (total_weight < min_weight) { min_weight = total_weight; mark = i; } } for (int i = 0; i < all_route[des_vex][mark].size(); i++) { printf("%d ", all_route[des_vex][mark][i]); } printf("%d %d", d[des_vex], min_weight); system("pause"); }

    转载请注明原文地址: https://ju.6miu.com/read-34030.html

    最新回复(0)