【PAT】1087. All Roads Lead to Rome (30)【深度优先搜索】

题目描述

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 FORMAT

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.

翻译:每个输入文件包含一组测试数据。对于每组输入数据,第一行包括两个正整数N(2<=N<=200),代表城市数量,和K,代表城市之间的道路总数;接着是起始城市的名字。接着N-1行每行给出城市名字和一个代表可以在该城市获得的快乐数量的整数,除了起始城市。接着K行,每行按照“City1 City2 Cost”的格式描述一条两个城市之间的道路。城市名字为一个三位英文字母,并且目的地总是ROM代表Rome。

OUTPUT FORMAT

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

翻译:对于每组输入数据,我们需要找出最小花费的路线。如果这种路线不唯一,则选择收获快乐最多的路线。如果这种路线仍不唯一,则我们输出平均收获快乐最大的路线——数据保证经过判断后这样的解存在且唯一。
因此输出第一行,你需要输出4个数字:最小花费的不同路线数,选择路线的花费,快乐值,和平均快乐值(仅保留整数)。第二行,你需要按照”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算法和深度优先搜索解决,这次选择了深度优先搜索。当累计的cost大于当前最大cost就直接return,否则判断happiness大小,如果仍然相等则判断平均happiness大小。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<string>
#include<map>
#include<algorithm>
#define INF 99999999
using namespace std;
int N,K,Des;
map<string,int>city;
string City[210];
int happiness[210];
int G[210][210];
int ccount=0,C=INF,H=0,St=0,tempC=0,tempH=0,tempSt=0;
int visited[210];
int ans[210],acount;
void findPath(){
    acount=0;
    int temp=Des;
    while(temp!=0){
        ans[acount++]=temp;
        temp=visited[temp];
    }
}
void dfs(int s){
    if(tempC>C)return ;
    if(s==Des){
        if(tempC<=C){
            if(tempC<C)ccount=1;
            else ccount++;
            if(tempC<C||(tempC==C&&(tempH>H||(tempH==H&&St>tempSt)))){ 
                H=tempH;
                St=tempSt;
                C=tempC;
                findPath();
            } 
        }
        return ;
    }
    for(int i=2;i<=N;i++){
        if(!visited[i]&&G[s][i]){
            visited[i]=s;
            tempC+=G[s][i];
            tempH+=happiness[i];
            tempSt++;
            dfs(i);
            visited[i]=0;
            tempC-=G[s][i];
            tempH-=happiness[i];
            tempSt--;
        }
    }
}
int main(){
    scanf("%d%d",&N,&K);
    string c;
    cin>>c;
    city[c]=1;
    City[1]=c;
    int h;
    for(int i=2;i<=N;i++){
        cin>>c>>h;
        if(c=="ROM")Des=i;
        city[c]=i;
        City[i]=c;
        happiness[i]=h;
    }
    string pre,pro;
    int Cost;
    for(int i=0;i<K;i++){
        cin>>pre>>pro>>Cost;
        G[city[pre]][city[pro]]=G[city[pro]][city[pre]]=Cost;
    }
    dfs(1);
    cout<<ccount<<" "<<C<<" "<<H<<" "<<H/St<<endl; 
    for(int i=acount-1;i>=0;i--){
        cout<<City[ans[i]];
        if(i!=0)cout<<"->";
    }
    cout<<endl;
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值