PAT 1087. All Roads Lead to Rome

Posted A-Little-Nut

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PAT 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 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边探索边记录和修改信息。要注意最后输出的是he number of different routes with the least cost。

#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
const int inf=9999999;
int n,k,lastcost=inf,lasthp=0,hp=0,lastcnt=0,cnt=0,num=0;
map<string,int> check1;
string check2[205];
int happy[205]={0},G[205][205],visited[205]={0};
vector<int> temproute,route;
void dfs(int st,int cost){
     temproute.push_back(st);
     cnt++;    
     hp+=happy[st];  
     visited[st]=1;
     if(st==check1["ROM"]){
        if(cost<lastcost) num=1;
        else if(cost==lastcost) num++;
        if(cost<lastcost||(cost==lastcost&&hp>lasthp)||(cost==lastcost&&hp==lasthp&&cnt<lastcnt)){
           route=temproute;
           lastcost=cost;
           lasthp=hp;
           lastcnt=cnt;
        }
     }
     for(int i=0;i<n;i++)
        if(G[st][i]!=inf&&visited[i]!=1)
           dfs(i,cost+G[st][i]);
     visited[st]=0;   
     cnt--;
     hp-=happy[st];
     temproute.pop_back();
}
int main(){
    string s,city,c1,c2;
    int h,c,tag=0;
    fill(G[0],G[0]+205*205,inf);
    cin>>n>>k>>s;
    check1[s]=tag; check2[tag++]=s;
    for(int i=0;i<n-1;i++){
        cin>>city>>h;
        happy[tag]=h;
        check1[city]=tag;
        check2[tag++]=city;
    }
    for(int i=0;i<k;i++){
        cin>>c1>>c2>>c;
        G[check1[c1]][check1[c2]]=G[check1[c2]][check1[c1]]=c;
    }       
    dfs(check1[s],0);
    cout<<num<<" "<<lastcost<<" "<<lasthp<<" "<<lasthp/(lastcnt-1)<<endl;
    for(int i=0;i<route.size();i++)
        i>0?cout<<"->"<<check2[route[i]]:cout<<check2[route[i]];
    return 0;
}

以上是关于PAT 1087. All Roads Lead to Rome的主要内容,如果未能解决你的问题,请参考以下文章

1087. All Roads Lead to Rome (30)最短路——PAT (Advanced Level) Practise

PAT (Advanced Level) 1087. All Roads Lead to Rome (30)

PAT (Advanced Level) 1087 All Roads Lead to Rome

1087 All Roads Lead to Rome (30 分)难度: 一般 / Dijkstra

1087 All Roads Lead to Rome (30)

(学英语学算法)PAT甲级--All Roads Lead to Rome