PAT Advanced 1087 All Roads Lead to Rome

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

題目大意:

在給定的圖中找到回家(ROM)的最短路徑,還是單源最短路(dijkstra)的問題, 其中可能存在多條最短的路徑,如果存在,需要選擇邊權之和最小的路徑, 如果邊權和的值相同情況下,選擇平均邊權最小的路徑。

注意:

  1. 輸入的時候點是按照編號給定的,那麼我們需要建立一個點名稱到數字下標的映射,同樣地,當輸出路徑信息的時候,需要一個由數字下標對應點的名稱。
  2. 依次輸出最短路徑的 條數,邊權之和, 點權之和, 平均點權之和 以及最優路線,最短路徑的條數添加cnt計數變量,如果走到dfs最深處(ROM),即葉子結點,則認爲一條路徑已經遍歷完成。

解題代碼

#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>

using namespace std;

const int N = 210;
vector<int> pre[N];
vector<int> temp, ans;
map<string, int> ctoi;
map<int, string> itoc;
int g[N][N], dist[N], wei[N];
bool vis[N];
int n, m, des, cnt;
int cost, happiness, average;
string st;

void dijkstra(){
    memset(dist, 0x3f, sizeof dist);
    dist[0] = 0;
    for (int i = 0; i < n; i++){
        int t = -1;
        for (int j = 0; j < n; j++)
            if (!vis[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        if (t == -1) return;
        for (int j = 0; j < n; j++){
           if (dist[j] > dist[t] + g[t][j]){
              dist[j] = dist[t] + g[t][j];
              pre[j].clear();
              pre[j].push_back(t);
           } else if (dist[j] == dist[t] + g[t][j]){
              pre[j].push_back(t);
           }
        }
        vis[t] = true;
    }
}
void dfs(int cur){
    if (cur == 0){
        cnt ++;
        temp.push_back(cur);
        int v = 0, avg = 0;
        for (int i = temp.size() - 1; i >= 0; i--)  v += wei[temp[i]];
        avg = v / (temp.size() - 1);
        if (v > happiness){
            happiness = v;
            ans = temp;
            average = avg;
        } else if (v == happiness && avg > average){
            ans = temp;
            average = avg;
        }
        temp.pop_back();
        return;
    }
    temp.push_back(cur);
    for (int i = 0; i < pre[cur].size(); i++) dfs(pre[cur][i]);
    temp.pop_back();
}
int main(){
    cin >> n >> m >> st;
    ctoi[st] = 0;
    itoc[0] = st;
    int a;
    string s1, s2;
    for (int i = 1; i <= n - 1; i++){
        cin >> s1 >> wei[i];
        ctoi[s1] = i;
        itoc[i] = s1;
    }
    memset(g, 0x3f, sizeof g);
    while(m --){
        cin >> s1 >> s2 >> a;
        int t1 = ctoi[s1], t2 = ctoi[s2];
        g[t1][t2] = g[t2][t1] = a;
    }
    dijkstra();
    des = ctoi["ROM"];
    dfs(des);
    printf("%d %d %d %d\n", cnt, dist[des], happiness, average);
    for (int i = ans.size() - 1; i >= 0; i --){
        if (i) printf("%s->",itoc[ans[i]].c_str());
        else  printf("%s", itoc[ans[i]].c_str());
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章