#1087. All Roads Lead to Rome【单源最短路 + Dijkstra】

原题链接

Problem Description:

Indeed there are many different tourist routes from our city to Rome. You are supposed to find your clients the route with the least cost while gaining the most happiness.

Input Specification:

Each input file contains one test case. For each case, the first line contains 2 positive integers N N N ( 2 ≤ N ≤ 200 2\leq N\leq 200 2N200), the number of cities, and K K K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N − 1 N−1 N1 lines each gives the name of a city and an integer that represents the happiness one can gain from that city, except the starting city. Then K K K lines follow, each describes a route between two cities in the format City1 City2 Cost. Here the name of a city is a string of 3 capital English letters, and the destination is always ROM which represents Rome.

Output Specification:

For each test case, we are supposed to find the route with the least cost. If such a route is not unique, the one with the maximum happiness will be recommanded. If such a route is still not unique, then we output the one with the maximum average happiness – it is guaranteed by the judge that such a solution exists and is unique.

Hence in the first line of output, you must print 4 numbers: the number of different routes with the least cost, the cost, the happiness, and the average happiness (take the integer part only) of the recommanded route. Then in the next line, you are supposed to print the route in the format City1->City2->...->ROM.

Sample Input:

6 7 HZH
ROM 100
PKN 40
GDN 55
PRS 95
BLN 80
ROM GDN 1
BLN ROM 1
HZH PKN 1
PRS ROM 2
BLN HZH 2
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

Problem Analysis:

本题涉及到了众多最短路问题中的点:最短距离、最短路条数、最大点权和、最小点数。

但本质就是一个单源最短路问题,结合代码体会。

Code

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

using namespace std;

const int N = 210;

int n, m;
int w[N];
int d[N][N];
int dist[N], cnt[N], cost[N], sum[N], pre[N];
// 最短距离,最短路数量,最大点权(最大幸福感)
// 最小点数(经过城市数量),最短路径的前驱
bool st[N];

string city[N];
unordered_map<string, int> mp;

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

int main()
{
    
    
    cin >> n >> m;
    cin >> city[1];
    mp[city[1]] = 1;
    
    for (int i = 2; i <= n; i ++ )
    {
    
    
        cin >> city[i] >> w[i];
        mp[city[i]] = i;
    }
    
    memset(d, 0x3f, sizeof d);
    while (m -- )
    {
    
    
        string x, y;
        int c;
        cin >> x >> y >> c;
        int a = mp[x], b = mp[y];
        d[a][b] = d[b][a] = min(d[a][b], c);
    }
    
    dijkstra();
    
    int T = mp["ROM"];
    
    cout << cnt[T] << ' ' << dist[T] << ' ' << cost[T] << ' ' << cost[T] / sum[T] << endl;
    
    vector<int> path;
    
    for (int i = T; i != 1; i = pre[i]) path.push_back(i);
    
    cout << city[1];
    for (int i = path.size() - 1; i >= 0; i -- )
        cout << "->" << city[path[i]];
    cout << endl;
    return 0;   
}

猜你喜欢

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