PAT--1087. All Roads Lead to Rome

32 篇文章 1 订阅
8 篇文章 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 2Indeed 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
PKN GDN 1
HZH PRS 1

Sample Output:

3 3 195 97
HZH->PRS->ROM

题解

最短路径的条数,所有最短路径中点权之和最大、平均点权之和最大的路径。

#include <bits/stdc++.h>
using namespace std;

const int maxn = 210;
const int inf  = 0x3f3f3f3f;

map<string, int> m1;
map<int, string> m2;
int n, k;
int w[maxn], d[maxn], vis[maxn];
int g[maxn][maxn];
vector<int> pre[maxn];
string st, en;
int maxval, avgval;
int num_of_route;
vector<int> p, path;

void dfs(int v){
    if(v == 0){
        num_of_route++;
        p.push_back(v);
        int val = 0;
        for(int i = 0; i < p.size(); ++i) val += w[p[i]];
        int tmp_avgval = val / (p.size() - 1);
        if(val > maxval){
            maxval = val;  
            avgval = tmp_avgval;
            path = p;
        }else if(val == maxval && tmp_avgval > avgval){
            avgval = tmp_avgval;
            path = p;
        }
        p.pop_back();
        return;
    }
    p.push_back(v);
    for(int i = 0; i < pre[v].size(); ++i){
        dfs(pre[v][i]);
    }
    p.pop_back();
}

void solve(){
    fill(d, d + n, inf);
    d[0] = 0;
    for(int i = 0; i < n; ++i){
        int u = -1, mi = inf;
        for(int j = 0; j < n; ++j){
            if(!vis[j] && d[j] < mi) mi = d[u = j];
        }
        vis[u] = 1;
        for(int v = 0; v < n; ++v){
            if(!vis[v] && g[u][v] != inf){
                if(d[u] + g[u][v] < d[v]){
                    d[v] = d[u] + g[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(d[u] + g[u][v] == d[v]){
                    pre[v].push_back(u);
                }   
            }
        }
    }
    int et = m1[en];
    dfs(et);
    cout << num_of_route << " " << d[et] << " " << maxval << " " << avgval << endl;
    int space = 0;
    for(int i = path.size() - 1; i >= 0; --i){
        if(space++) cout << "->";
        cout << m2[path[i]];
    } 
    cout << endl;
}

int main(){

    fill(g[0], g[0] + maxn * maxn, inf);
    cin >> n >> k >> st;
    en = "ROM";

    m1[st] = 0;
    m2[0] = st;

    string s;
    for(int i = 1; i < n; ++i){
        cin >> s >> w[i];
        m1[s] = i, m2[i] = s;
    }
    string a, b; int cost;
    for(int i = 0; i < k; ++i){
        cin >> a >> b >> cost;
        g[m1[a]][m1[b]] = g[m1[b]][m1[a]] = cost;
    }

    solve();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值