PAT(A) - 1087. All Roads Lead to Rome (30)

本文介绍了一个寻找从任意城市到罗马的最优旅行路线的问题。通过深度优先搜索(DFS),找到所有可能的路线,并根据成本和快乐值对路线进行排序,最终确定了最优路线。

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


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

思路分析:我直接用的DFS,找到所有能到达ROM的路径,存到一个结构体里,然后进行排序,结构体的第一个结果就是最优的解了。


#include <cstdio>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>
#include <string>

#define MAX 200

using namespace std;

int MGraph[MAX][MAX];   // 邻接矩阵
int weight[MAX];        // 点权
map<string, int> stringToInt;
map<int, string> intToString;
int visit[MAX];

typedef struct {
    vector<string> path;
    int cost;
    int happy;
    int aveHappy;
} Result;

vector<string> temp;
vector<Result> result;

int cmp( Result a, Result b ) {
    if( a.cost != b.cost ) {
        return a.cost < b.cost ? 1 : 0;
    }
    if( a.happy != b.happy ) {
        return a.happy > b.happy ? 1 : 0;
    }
    return a.aveHappy > b.aveHappy ? 1 : 0;
}

void DFS( int n, string curNode, string end, int curCost, int curHappy, vector<string>& path ) {
    int idCur = stringToInt[curNode];
    visit[idCur] = 1;

    if( curNode == end ) {
        Result r;
        r.path = path;
        r.cost = curCost;
        r.happy = curHappy;
        //r.aveHappy = curHappy / temp.size();
        r.aveHappy = curHappy / path.size();
        result.push_back( r );
    }
    for( int i = 0; i < n; i++ ) {
        if( !visit[i] && MGraph[idCur][i] > 0 ) {
            //temp.push_back( intToString[i] );
            path.push_back( intToString[i] );
            visit[i] = 1;
            DFS( n, intToString[i], end, curCost + MGraph[idCur][i], curHappy + weight[i], path );
            visit[i] = 0;
            //temp.pop_back();
            path.pop_back();
        }
    }
}


// 这是第二个方法,也是DFS,是边DFS边判断是否是最优路径
int minCost = 0x3fffffff;
int maxHappy = 0;
int numCity = 0;
int numRoute = 0;
vector<string> bestPath;

void dfs(int n, string curNode, string end, int cost, int num, int happy, vector<string>& path){
    int idCur = stringToInt[curNode];

    if(curNode == end){
        if(minCost > cost){
            minCost = cost;
            maxHappy = happy;
            numCity = num;
            bestPath = path;
            numRoute = 1;
        }else if(minCost == cost){
            numRoute++;

            if(maxHappy < happy){
                maxHappy = happy;
                numCity = num;
                bestPath = path;
            }else if(maxHappy == happy){
                if(maxHappy / numCity < happy / num){
                    numCity = num;
                    bestPath = path;
                }
            }
        }
    }
    for(int i = 0; i < n; ++i){
        if(!visit[i] && MGraph[idCur][i] > 0){
            visit[i] = true;
            path.push_back(intToString[i]);
            dfs( n, intToString[i], end, cost + MGraph[idCur][i], num + 1, happy + weight[i], path );
            visit[i] = false;
            path.pop_back();
        }
    }
}

int main() {
    //freopen( "123.txt", "r", stdin );
    int n, m;
    string start;

    cin >> n >> m >> start;
    stringToInt[start] = 0;
    intToString[0] = start;
    weight[0] = 0;

    int happy;
    string city;
    for( int i = 1; i <= n - 1; i++ ) {
        cin >> city >> happy;
        stringToInt[city] = i;
        intToString[i] = city;
        weight[i] = happy;
    }

    string a, b;
    int w;
    for( int i = 0; i < m; i++ ) {
        cin >> a >> b >> w;
        int idA = stringToInt[a];
        int idB = stringToInt[b];
        MGraph[idA][idB] += w;
        MGraph[idB][idA] += w;
    }

    vector<string> path;
    DFS( n, start, "ROM", 0, 0, path );
    sort( result.begin(), result.end(), cmp );
/*
    // 输出中间结果信息
    for( int i = 0; i < result.size(); i++ ) {
        printf( "%d %d %d\n", result[i].cost, result[i].happy, result[i].aveHappy );
        for( int j = 0; j < result[i].path.size(); j++ ) {
            cout <<result[i].path[j] << "->";
        }
        cout << endl;
    }
*/

    // 查找最小花费路线的数量
    int min = result[0].cost;
    int num = 0;
    for( int i = 0; i < result.size(); i++ ) {
        if( result[i].cost == min ) {
            num++;
        }
        else break;
    }
    // 如下,我第一次写成了result.size(),导致一个5分的测试点过不去。
    // 题目要求的是输出最小花费路线的数量
    // 而result.size()是所有路线的数量
    //printf( "%d %d %d %d\n", result.size(), result[0].cost, result[0].happy, result[0].aveHappy );
    printf( "%d %d %d %d\n", num, result[0].cost, result[0].happy, result[0].aveHappy );
    cout << start;
    for( int i = 0; i < result[0].path.size(); i++ ) {
        cout << "->" << result[0].path[i];
    }

/*
    // 这是第二个方法
    vector<string> path;
    dfs( n, start, "ROM", 0, 0, 0, path);

    printf("%d %d %d %d\n", numRoute, minCost, maxHappy, maxHappy/numCity);
    printf( "%s", start.c_str() );
    for(size_t i = 0; i < bestPath.size(); ++i){
        printf("->%s", bestPath[i].c_str());
    }
*/

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值