PAT甲级 1087 All Roads Lead to Rome DFS+Dijkstra

1087 All Roads Lead to Rome

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

一个计算图最短路径的问题,只是该题的无向图具有点权值,且要求计算出最短路径有几条,最终输出的路线为点权值最大的路线(若仍有重合则输出平均点权值最大的路线)

计算最短路径的算法有Dijkstra算法、Bellman-Ford算法等等,选择哪种具体要根据图的存储方式而定

该题很显然需要使用深搜(DFS)确定最短路径的路线有几条,且为无向图,所以使用邻接矩阵存储更便捷

邻接矩阵存储无向图则使用Dijkstra算法,Floyd-Warshall算法也可以但会超时,我一般Bellman-Ford使用点边集或邻接表存储图

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <map>
#include <vector>

using namespace std;

#define SUM 201
#define INF 9999

struct City {
    string name;
    int happiness;
};

struct Path {//路线
    string pass_city;//经过的城市路线
    int sum_city;
    int sum_happiness;
    int aver_happiness;
};

int n, m;
City cities[SUM];
int My_map[SUM][SUM];//邻接矩阵
map<string, int> pos1;//城市名确定编号
map<int, string> pos2;//编号确定城市名
int low_cost;//最小花费
vector<Path> result;
int book[SUM];//深搜标记数组

void dfs(int id, int sum_cost, Path part) {
    if(pos2[id] == "ROM" && sum_cost == low_cost) {//到了罗马
        part.aver_happiness = part.sum_happiness / part.sum_city;
        result.push_back(part);
        return;
    }
    if(sum_cost >= low_cost) {//没有必要再搜索了
        return;
    }
    for(int i = 1; i <= n; i++) {
        if(book[i] == 0 && My_map[id][i] > 0) {
            book[i] = 1;
            Path new_part;
            new_part.pass_city = part.pass_city + "->" + pos2[i];
            new_part.sum_city = part.sum_city + 1;
            new_part.sum_happiness = part.sum_happiness + cities[i].happiness;
            dfs(i, sum_cost + My_map[id][i], new_part);
            book[i] = 0;
        }
    }
}

int get_low_cost() {//Dijkstra算法
    int dis[SUM];
    for(int i = 1; i <= n; i++) { //初始化各点距离源点距离
        dis[i] = My_map[1][i];
    }
    int book[SUM] = {0};
    book[1] = 1;
    for(int i = 1; i <= n - 1; i++) {
        int min_p = 1;
        int min_cost = 999;
        for(int i = 1; i <= n; i++) {
            if(book[i] == 0 && dis[i] < min_cost && dis[i] > 0) {
                min_p = i;
                min_cost = dis[i];
            }
        }
        book[min_p] = 1;
        for(int i = 1; i <= n; i++) {
            if(dis[i] > dis[min_p] + My_map[min_p][i]) {
                dis[i] = dis[min_p] + My_map[min_p][i];
            }
        }
    }
    return dis[pos1["ROM"]];//找到了最短路径
}

int cmp(const Path &a, const Path &b) {
    if(a.sum_happiness != b.sum_happiness) {
        return a.sum_happiness > b.sum_happiness;
    }
    return a.aver_happiness > b.aver_happiness;
}

int main() {
    cin >> n >> m >> cities[1].name;
    cities[1].happiness = 0;
    pos1[cities[1].name] = 1;
    pos2[1] = cities[1].name;
    for(int a = 1; a <= n; a++) {//初始化邻接矩阵
        for(int b = 1; b <= n; b++) {
            if(a != b) {
                My_map[a][b] = INF;
            } else {
                My_map[a][b] = 0;
            }
        }
    }
    for(int i = 2; i <= n; i++) {//输入点和点权
        cin >> cities[i].name >> cities[i].happiness;
        pos1[cities[i].name] = i;
        pos2[i] = cities[i].name;
    }
    for(int i = 1; i <= m; i++) {//输入边和边权
        string p1, p2;
        int cost;
        cin >> p1 >> p2 >> cost;
        My_map[pos1[p1]][pos1[p2]] = cost;
        My_map[pos1[p2]][pos1[p1]] = cost;
    }
    low_cost = get_low_cost();
    dfs(1, 0, {pos2[1], 0, 0, 0});//深搜计算路径数和相关权值
    sort(result.begin(), result.end(), cmp);
    int len = result.size();
    printf("%d %d %d %d\n", len, low_cost, result.front().sum_happiness, result.front().aver_happiness);
    printf("%s\n", result.front().pass_city.c_str());
    return 0;
}

感觉这题和乙级的多选题常见计分法一样,题目不难却很麻烦,好在不需要优化算法了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值