1087. All Roads Lead to Rome (30)

本文介绍了一个寻找从特定城市到罗马的旅游路线的问题,旨在找到成本最低且能带来最大快乐值的路线。通过使用最短路径算法并考虑不同路径带来的快乐值,文章提供了一种解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1087. All Roads Lead to Rome (30)
时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B
判题程序 Standard 作者 CHEN, Yue

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 (2<=N<=200), the number of cities, and K, the total number of routes between pairs of cities; followed by the name of the starting city. The next N-1 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 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 recommended. 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 recommended 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

#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <cfloat>
#include <queue>
#include <map>

using namespace std;
const int MaxN = 210;
const int INF = INT32_MAX;
int st = 0, endc, mincost = INF, tmpcost = 0, maxhappy = -1, tmphappy = 0,averhappy = 0;
int N, K, G[MaxN][MaxN], cost[MaxN], isInq[MaxN], roadN[MaxN];
int happy[MaxN];
vector<int>pre[MaxN], tmppath, path;
map<string, int>cityidx;
map<int, string>cityname;

int getCityidx(string name) {
    static int cityN = 0;
    if (cityidx.find(name) != cityidx.end()) return cityidx[name];
    else {
        cityidx[name] = cityN;
        cityname[cityN] = name;
        return cityN++;
    }
}

string getcityName(int idx) {
    return cityname[idx];
}



void SFPA(int st) {
    fill(cost, cost + N + 2, INF);
    memset(roadN, 0, N + 2);
    cost[st] = 0; roadN[st] = 1;
    queue<int>que; que.push(st);
    while (que.size()) {
        int u = que.front(); que.pop(); isInq[u] = false;
        for (int v = 0; v < N; ++v) {
            if (G[u][v] != INF) {
                if (cost[v] > cost[u] + G[u][v]) {
                    cost[v] = cost[u] + G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                    roadN[v] = roadN[u];
                    if (!isInq[v]) {
                        que.push(v);
                        isInq[v] = true;
                    }
                }
                else if (cost[v] == cost[u] + G[u][v]) {
                    pre[v].push_back(u);
                    roadN[v] += roadN[u];
                }
            }
        }
    }
}


void DFS(int v) {
    tmppath.push_back(v);
    if (v == st) {
        int passcityN = tmppath.size() - 1;
        tmphappy = 0; tmpcost = 0;
        for (int i = tmppath.size() - 1; i > 0; --i) {
            int u = tmppath[i],v = tmppath[i-1];
            tmpcost += G[u][v];
            tmphappy += happy[v];
        }

        if (tmpcost < mincost) {
            mincost = tmpcost;
            maxhappy = tmphappy;
            averhappy = tmphappy / passcityN;
            path = tmppath;
        }
        else if (tmpcost == mincost) {
            if (tmphappy > maxhappy) {
                maxhappy = tmphappy;
                averhappy = tmphappy / passcityN;
                path = tmppath;
            }
            else if (tmphappy == maxhappy && tmphappy / passcityN > averhappy) {
                averhappy = tmphappy / passcityN;
                path = tmppath;
            }
        }
        tmppath.pop_back();
        return;
    }

    for (int i = 0; i < pre[v].size(); ++i)DFS(pre[v][i]);
    tmppath.pop_back();
}


void print() {
    cout << roadN[endc] << " " << cost[endc] << " " << maxhappy << " " << averhappy << endl;
    for (int i = path.size() - 1; i >= 0; --i) {
        cout << getcityName(path[i]);
        if (i != 0)cout << "->";
    }
}

int main() {
#ifdef _DEBUG
    freopen("data.txt", "r+", stdin);
#endif // _DEBUG
    std::ios::sync_with_stdio(false);

    string scity;
    cin >> N >> K >> scity;
    fill(G[0], G[0] + (N + 2) * MaxN, INF);
    st = getCityidx(scity);
    endc = getCityidx("ROM");
    for (int i = 1; i < N; ++i) {
        string cityname; int happ, cidx;;
        cin >> cityname >> happ;
        cidx = getCityidx(cityname);
        happy[cidx] = happ;
    }

    for (int i = 0; i < K; ++i) {
        string c1, c2; int c, cidx1, cidx2;
        cin >> c1 >> c2 >> c;
        cidx1 = getCityidx(c1); cidx2 = getCityidx(c2);
        G[cidx1][cidx2] = G[cidx2][cidx1] = c;
    }

    SFPA(st);
    DFS(endc);
    print();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值