#1030. Travel Plan【单源最短路 + Dijkstra】

原题链接

Problem Description:

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 N N, M M M, S S S, and D D D, where N N N ( ≤ 500 \leq 500 500) is the number of cities (and hence the cities are numbered from 0 to N − 1 N−1 N1); M M M is the number of highways; S S S and D D D are the starting and the destination cities, respectively. Then M M 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

Problem Analysis:

本题是一道单源最短路问题,给定一个起点和终点,求最短路径长度及最小花费,由于最短路径不唯一,我们需要求出花费最小的那条路径,在更新最短路的同时更新一个 pre 记录路径即可。

本题数据范围较小,可以用邻接矩阵存图,然后用 朴素版 Dijkstra 解决。

Code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 510;

int n, m, S, T;
int dist[N], cost[N], pre[N];
int d[N][N], c[N][N];
bool st[N];

void dijkstra()
{
    
    
    memset(dist, 0x3f, sizeof dist);
    memset(cost, 0x3f, sizeof cost);
    
    dist[S] = 0, cost[S] = 0;
    for (int i = 0; i < n; i ++ )
    {
    
    
        int t = -1;
        for (int j = 0; j < n; j ++ )
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;
        
        for (int j = 0; j < n; j ++ )
            if (dist[j] > dist[t] + d[t][j])
            {
    
    
                dist[j] = dist[t] + d[t][j];
                cost[j] = cost[t] + c[t][j];
                pre[j] = t;
            }
            else if (dist[j] == dist[t] + d[t][j] && cost[j] > cost[t] + c[t][j])
            {
    
    
                cost[j] = cost[t] + c[t][j];
                pre[j] = t;
            }
    }
}

int main()
{
    
    
    cin >> n >> m >> S >> T;
    memset(d, 0x3f, sizeof d);
    memset(c, 0x3f, sizeof c);
    
    for (int i = 0; i < m; i ++ )
    {
    
    
        int a, b, x, y;
        cin >> a >> b >> x >> y;
        d[a][b] = d[b][a] = min(d[a][b], x);
        c[a][b] = c[b][a] = min(c[a][b], y);
    }
    
    dijkstra();
    
    vector<int> path;
    for (int i = T; i != S; i = pre[i]) path.push_back(i);
    
    cout << S;
    for (int i = path.size() - 1; i >= 0; i -- ) cout << ' ' << path[i];
    
    cout << ' ' << dist[T] << ' ' << cost[T] << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/geraltofrivia123/article/details/121032267