PAT 1087 All Roads Lead to Rome

原题链接:1087 All Roads Lead to Rome (30分)
关键词:dijkstra+dfs 模板题

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

题目大意: 请你在成本最低的旅游路线之中,找到使得游客幸福感最强的路线。
分析: 加入了点权值的最短路径问题,添加string城市跟int之间的相互映射。
代码:

#include
#include
#include
#include
#include
#include
#include
using namespace std;

const int maxn = 210;
const int INF = 0x3f3f3f;
int n, k;
int cost[maxn][maxn];   //边的权值(路的花费)
int happy[maxn];    //幸福值数组
bool visit[maxn];
int d[maxn];    //最小成本
vector<vector<int> >pre;    //前序结点
vector<int>path;    //路径
int maxHappy = 0;   //这条路的幸福感
int cnt = 0;    //最小成本的路线数量

void dijkstra(int s){   //s源点
    fill(d, d+n, INF);
    d[s]=0;
    for(int i = 0; i < n; i ++ ){
        int u = -1, MIN = INF;
        for(int i = 0; i < n; i ++ ){
            if(visit[i] == false && d[i] < MIN){
                u = i;
                MIN = d[i];
            }
        }
        if(u == -1) return;
        visit[u] = true;
        for(int v = 0; v < n; v ++ ){
            if(visit[v] == false && cost[u][v]!=0){
                if(d[u] + cost[u][v] < d[v]){
                    d[v] = d[u] + cost[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }
                else if(d[u]+cost[u][v] == d[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
}

void dfs(int root, vector<int> temp, int hp){
    temp.push_back(root);
    hp += happy[root];
    if(root == 0){
        cnt++;
        if(hp > maxHappy){
            path = temp;
            maxHappy = hp;
        }
        else if(hp == maxHappy){
            if(temp.size() < path.size()){
                path = temp;
            }
        }
    return;
    }
    for(int i = 0; i < pre[root].size(); i ++ ){
        dfs(pre[root][i], temp, hp);
    }
}

int main()
{

    char begin[4];  //起点
    scanf("%d %d %s", &n, &k, begin);
    map<string,int>mp;  //城市 <-> 编号
    map<int,string>mp2; //编号 <-> 城市
    mp[begin]=0;
    mp2[0]=begin;
    pre.resize(n);
    for(int i = 1; i < n; i++){
        char city[4];   //城市
        int h;  //幸福值
        scanf("%s %d", city, &h);
        mp[city]=i;
        mp2[i]=city;
        happy[i]=h;
    }
    
    for(int i = 0; i < k; i ++ ){
        char c1[4],c2[4];
        int t;
        scanf("%s %s %d",c1, c2, &t);
        cost[mp[c1]][mp[c2]] = t;
        cost[mp[c2]][mp[c1]] = t;
    }
    
    dijkstra(0);
    vector<int>v;
    dfs(mp["ROM"], v, 0);
    printf("%d %d %d %d\n%s", cnt, d[mp["ROM"]], maxHappy, maxHappy/(path.size()-1), begin);
    for(int i = path.size()-2; i>=0; i--){
        cout << "->" << mp2[path[i]];
    }
    return 0;
}

你可能感兴趣的:(#,PAT甲级,最短路径,Dijkstra,dfs)