PTA甲级 1087 All Roads Lead to Rome (C++)

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 ) N (2≤N≤200) N(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

Caution:

DFS 找到所有可能的路径然后再排序输出。

Solution:

// Talk is cheap, show me the code
// Created by Misdirection 2021-09-01 17:27:38
// All rights reserved.

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

using namespace std;

struct Route{
    vector<int> path;
    int len;
    int happiness;
    int avrageHappiness;

    Route(vector<int> p, int l, int h){
        path = p;
        len = l;
        happiness = h;
        avrageHappiness = h / (path.size() - 1);
    }
    ~Route(){}
};


int n, k;
// n - the number of cities
// k - the total number of routes between pairs of cities
int dist[205][205];
unordered_map<string, int> city2ID;
unordered_map<int, string> ID2city;
unordered_map<int, int> happiness;

vector<Route> ans;
vector<int> tmp;
unordered_map<int, bool> visited;


bool cmp(Route &a, Route &b){
    if(a.len != b.len) return a.len < b.len;
    if(a.happiness != b.happiness) return a.happiness > b.happiness;
    return a.avrageHappiness > b.avrageHappiness;
}

void dfs(int city, int nowLen, int nowHappy){    
    if(city == city2ID["ROM"]){
        ans.emplace_back(tmp, nowLen, nowHappy);
        return;
    }

    for(int i = 1; i <= n; ++i){
        if(dist[city][i] != 0 && visited[i] == false){
            tmp.push_back(i);
            visited[i] = true;
            dfs(i, nowLen + dist[city][i], nowHappy + happiness[i]);
            tmp.pop_back();
            visited[i] = false;
        }
    }
}

int main(){
    
    scanf("%d %d", &n, &k);

    string startCity;
    cin >> startCity;

    city2ID[startCity] = 1;
    ID2city[1] = startCity;

    string cityName;
    for(int i = 2; i <= n; ++i){
        cin >> cityName;
        city2ID[cityName] = i;
        ID2city[i] = cityName;

        int tmpHappy;
        scanf("%d", &tmpHappy);
        happiness[i] = tmpHappy;
    }
    // 城市 ID 转换成 int 后所有的计算都带入 int,而不是 string
    // 城市从 1 开始编号,1 代表出发城市

    for(int i = 0; i < k; ++i){
        string city1, city2;
        cin >> city1 >> city2;

        int id1 = city2ID[city1];
        int id2 = city2ID[city2];

        int tmpDis;
        scanf("%d", &tmpDis);

        dist[id1][id2] = tmpDis;
        dist[id2][id1] = tmpDis;
    }

    tmp.push_back(1);
    visited[1] = true;
    dfs(1, 0, 0);

    sort(ans.begin(), ans.end(), cmp);
    int sameDisCnt = 1;
    while(sameDisCnt < ans.size() && ans[sameDisCnt].len == ans[0].len) sameDisCnt++;

    printf("%d %d %d %d\n", sameDisCnt, ans[0].len, ans[0].happiness, ans[0].avrageHappiness);
    for(int i = 0; i < ans[0].path.size(); ++i){
        if(ans[0].path[i] == city2ID["ROM"]) printf("ROM\n");
        else printf("%s->", ID2city[ans[0].path[i]].c_str());
    }
    
    return 0;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

负反馈循环

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值