PAT-A 真题- 1087. All Roads Lead to Rome

发布于 / PAT-甲级 / 0 条评论

原题干:

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

题目大意:第一行给出N个城市,K条路线,还有起点,接着给出N-1行城市的"幸福值"(点权),然后给出K行数据,格式为"城市1    城市2   距离"

要求算出距离最短的,从起点到罗马(ROM)的路线。如果这样最短的路线不唯一,求出途径幸福值最大的一条路线。如果路线仍然不唯一,求出平均幸福值最大的路线

输出的时候第一行输出四个数据,分别是"最短距离路线数   最短距离   途径幸福值   幸福值平均值"

这里所谓求幸福值最大值,也就是求点权最大值。求幸福值平均值最大的路线,其实就是求相同条件下途径城市最少的一条路线。

我们可以使用Dijkstra算法或者Dijkstra+DFS来做。相比之下,后者更为容易理解。

代码如下:

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;
#define MAXN 1050
#define INF 0x3fffffff
int CityCnt, RoadCnt;
map<string, int> CityID;
map<int, string> IDCity;
int Happy[MAXN] = {0};
int G[MAXN][MAXN];
vector<int> pre[MAXN];
int PathSum = 0;
int MinDistance[MAXN];

void Dijkstra(int s){
    bool visited[MAXN] = {false};
    fill(MinDistance, MinDistance + MAXN, INF);
    MinDistance[s] = 0;
    for(int i = 0; i < CityCnt; i++){
        //find the MIN-dis Point P
        int p = -1, MIN = INF;
        for(int j = 0; j < CityCnt; j++){
            if(visited[j] == false && MinDistance[j] < MIN)
                p = j, MIN = MinDistance[j];
        }
        if(p == -1) return;
        //found
        visited[p] = true;
        //use P as MID-point update J
        for(int j = 0; j < CityCnt; j++){
            if(visited[j] == false && G[p][j] != INF){
                //case:find a better way
                if(G[p][j] + MinDistance[p] < MinDistance[j]){
                    MinDistance[j] = G[p][j] + MinDistance[p];
                    pre[j].clear();
                    pre[j].push_back(p);
                }
                //case:find a equal-way
                else if(G[p][j] + MinDistance[p] == MinDistance[j]){
                    pre[j].push_back(p);
                }
            }
        }
    }
}

void Trave(vector<int> v){
    for(int i = v.size()-1; i >= 0; i--){
        cout << IDCity[ v[i] ];
        if(i != 0) cout << "->";
    }
    cout << endl;
}

vector<int> tmpPath, realPath;
int happysum = 0, cnt = INF;
int avg = 0;

void DFS(int v){
    tmpPath.push_back(v);
    if(v == 0){
        PathSum++;
        int allsum = 0, allcnt = tmpPath.size()-1;
        for(int i = 0; i < tmpPath.size(); i++){
            allsum += Happy[tmpPath[i]];
        }
        if(allsum > happysum){
            happysum = allsum, cnt = allcnt;
            realPath = tmpPath;
        }else if(allsum == happysum && allcnt < cnt){
            happysum = allsum, cnt = allcnt;
            realPath = tmpPath;
        }
    }
    else{
        for(int i = 0; i < pre[v].size(); i++)
            DFS(pre[v][i]);
    }
    tmpPath.pop_back();
}

int main(){    
    string StartCity, City1, City2;
    cin >> CityCnt >> RoadCnt >> StartCity;
    CityID[StartCity] = 0, IDCity[0] = StartCity;
    fill(G[0], G[0] + MAXN*MAXN, INF);
    for(int i = 1; i < CityCnt; i++){
        cin >> City1;
        CityID[City1] = i, IDCity[i] = City1;
        cin >> Happy[i];
    }
    for(int i = 0; i < RoadCnt; i++){
        cin >> City1 >> City2;
        int ID1 = CityID[City1], ID2 = CityID[City2];
        cin >> G[ID1][ID2];
        G[ID2][ID1] = G[ID1][ID2];
    }
    Dijkstra(0);
    DFS(CityID["ROM"]);
    cout << PathSum << " " << MinDistance[CityID["ROM"]] << " " ;
    cout << happysum << " " << happysum / cnt << endl;
    Trave(realPath);
    return 0;
}

转载原创文章请注明,转载自: 斐斐のBlog » PAT-A 真题- 1087. All Roads Lead to Rome
目前还没有评论,快来抢沙发吧~