【PAT甲级A1087】All Roads Lead to Rome (30分)(c++)

1087 All Roads Lead to Rome (30分)

作者:CHEN, Yue
单位:浙江大学
代码长度限制:16 KB
时间限制:200 ms
内存限制:64 MB

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 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

题意:

给出起点城市和终点城市ROM,以及各个城市之间的花费(即边权),以及每个城市的幸福指数(即点权),要求输出一条边权最小且点权最大的路径,并统计边权最小的路径个数 (注意:不是只边权最小,点权最大的路径个数,单纯的边权最小的路径个数),以及最大点权,在边权最小且最大点权也相等的情况下,最优路径为平均点权最大的路径。

思路:

  • 首先,需要将人名转化为整型进行图的构建,利用两个map进行映射。
  • 其次,由于要统计边权最小路径的个数,所以并不能把最大点权的计算放在dijkstra的计算过程中,我们需要将每个点的前驱点记录下来。
  • 最后,利用dfs深度优先从ROM点遍历至起点,记录路径数,并将求得改路径的点权,当点权最大时记录路径,当点权相等时,选择路径结点数最小的路径记录。

参考代码:

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

using namespace std;
const int maxn = 201;
const int INF = 1e9;
int G[maxn][maxn] = {0}, d[maxn];
int happiness[maxn] = {0}, cnt = 0;
bool vis[maxn] = {false};
int n, m, h, c, minnode = INF, maxhappiness = -1;

vector<vector<int>> pre;
vector<int> path, temp;
unordered_map<string, int> mp1;
unordered_map<int, string> mp2;

void dij(int s) {
    fill(d, d + maxn, INF);
    d[s] = 0;
    for (int i = 0; i < n; i++) {
        int u = -1, MIN = INF;
        for (int j = 0; j < n; j++) {
            if (!vis[j] && d[j] < MIN) {
                u = j;
                MIN = d[j];
            }
        }
        if (u == -1)return;
        vis[u] = true;
        for (int v = 0; v < n; v++) {
            if (!vis[v] && G[u][v] != INF) {
                if (d[u] + G[u][v] < d[v]) {
                    d[v] = d[u] + G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                } else if (d[u] + G[u][v] == d[v])
                    pre[v].push_back(u);
            }
        }
    }
}

void dfs(int index, int depth) {
    if (index == 0) {
        temp.push_back(index);
        cnt++;
        int happytemp = 0;
        for (int i = temp.size() - 1; i >= 0; i--)
            happytemp += happiness[temp[i]];
        if (happytemp > maxhappiness) {
            maxhappiness = happytemp;
            minnode = depth;
            path = temp;
        } else if (happytemp == maxhappiness && depth < minnode) {
            minnode = depth;
            path = temp;
        }
        temp.pop_back();
        return;
    }
    temp.push_back(index);
    for (int i = 0; i < pre[index].size(); i++)
        dfs(pre[index][i], depth + 1);
    temp.pop_back();
}

int main() {
    fill(G[0], G[0] + maxn * maxn, INF);
    string st, st1, st2;
    cin >> n >> m >> st;
    pre.resize(n);
    mp1[st] = 0;
    mp2[0] = st;
    for (int i = 1; i < n; i++) {
        cin >> st >> h;
        happiness[i] = h;
        mp1[st] = i;
        mp2[i] = st;
    }
    for (int i = 0; i < m; i++) {
        cin >> st1 >> st2 >> c;
        int x = mp1[st1], y = mp1[st2];
        G[x][y] = G[y][x] = c;
    }
    dij(0);
    int ed = mp1["ROM"];
    dfs(ed, 0);
    printf("%d %d %d %d\n", cnt, d[ed], maxhappiness, maxhappiness / minnode);
    for (int i = path.size() - 1; i >= 0; i--) {
        if (i != path.size() - 1)printf("->");
        cout << mp2[path[i]];
    }
    return 0;
}

如有错误,欢迎指正

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值