PAT 1087 All Roads Lead to Rome (30 分) -甲级

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 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 recommanded. 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 recommanded 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
#include <stdio.h>
#include <string>
#include <iostream>
#include <map>
#include <algorithm>
#include <vector>

using namespace std;

const int MAXV = 1010;
const int INF = 0x3fffffff;
int N, K;
string start;
map<string, int> StringToHappy, StringToNum;
map<int, string> NumToString;
vector<int> pre[MAXV];
int G[MAXV][MAXV], num[MAXV] = {0};
int d[MAXV], vis[MAXV] = {0};
int getHappy[MAXV] = {0};

void Dijkstra() {
    fill(d, d + MAXV, INF);
    d[1] = 0;
    getHappy[1] = 0;
    num[1] = 1;
    for (int i = 1; i <= N; ++i) {
        int u = -1, MIN = INF;
        for (int j = 1; j <= N; ++j) {
            if (vis[j] == 0 && d[j] < MIN) {
                u = j;
                MIN = d[j];
            }
        }
        if (u == -1) return;
        vis[u] = 1;
        for (int v = 1; v <= N; ++v) {
            if (vis[v] == 0 && G[u][v] != INF && d[u] + G[u][v] < d[v]) {
                d[v] = d[u] + G[u][v];
                num[v] = num[u];
                string nodes = NumToString[v];
                getHappy[v] = getHappy[u] + StringToHappy[nodes];
                if (!pre[u].empty()) {
                    pre[v] = pre[u];
                }
                pre[v].push_back(u);
            } else if (vis[v] == 0 && G[u][v] != INF && d[u] + G[u][v] == d[v]) {
                string nodes = NumToString[v];
                num[v] += num[u];
                if (getHappy[v] < getHappy[u] + StringToHappy[nodes]) {
                    getHappy[v] = getHappy[u] + StringToHappy[nodes];
                    pre[v] = pre[u];
                    pre[v].push_back(u);
                } else if (getHappy[v] == getHappy[u] + StringToHappy[nodes]) {
                    if (pre[v].size() > pre[u].size() + 1) {
                        pre[v] = pre[u];
                        pre[v].push_back(v);
                    }
                }
            }
        }
    }
}

int main() {
    cin >> N >> K >> start;
    StringToNum[start] = 1;
    NumToString[1] = start;
    for (int i = 2; i <= N; ++i) {
        string city;
        int happies;
        cin >> city >> happies;
        StringToHappy[city] = happies;
        StringToNum[city] = i;
        NumToString[i] = city;
    }
    fill(G[0], G[0] + MAXV * MAXV, INF);
    for (int j = 0; j < K; ++j) {
        string city1, city2;
        int cost, c1, c2;
        cin >> city1 >> city2 >> cost;
        c1 = StringToNum[city1];
        c2 = StringToNum[city2];
        G[c1][c2] = G[c2][c1] = cost;
    }
    Dijkstra();
    int dist = StringToNum["ROM"];
    printf("%d %d %d %d\n", num[dist], d[dist], getHappy[dist], getHappy[dist] / pre[dist].size());
    for (vector<int>::iterator it = pre[dist].begin(); it != pre[dist].end(); it++) {
        printf("%s->", NumToString[*it].c_str());
    }
    printf("ROM");
    return 0;
}
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页