【PAT】A1087 All Roads Lead to Rome 【Dijkstra算法】

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的最短路线,如果有多个最短路线,依次“幸福值>幸福平均值”的优先级,选择它们最大的那一条。然后输出最短路线个数,最短距离,幸福指数,平均幸福指数。

思路

这题和A1003非常相似,多了输出路径和几个指标。可以先参考A1003题解
本题主要就是一个Dijkstra算法,这里不再赘述。关于路线的条数将从起点到所有点的初始道路数置为1,当点u找到一条更短的路径v->u时,此时我们只能从u移动到v,那么road[u] = road[v],当点u找到一条等长路劲v->u时,此时我们到v的路径多了u这一个分支,所以road[u] += road[v](到u可能也有若干路径, 所以这里不是加1)。对于幸福值,我们需要hAmount和cityCnt两个数组去存储。这里也是在路径等长的时候判断hAmount能否变大,是则更新路线,否则再考虑幸福值平均值(有了总量和城市数我们就可以算)是否更大,是则更新路线。
最后遍历路径,为了少写个循环,我就将edgeTo每个元素初始化为-1了,也可以将其初始化为edgeTo[u] = u,遍历写法略有不同而已。

代码

#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <queue>
#include <limits.h>
#define MAX_N 200
using namespace std;
struct Edge{
    int to;
    int cost;
    Edge(int to, int cost) : to(to), cost(cost){};
};
struct Node{
    int to;
    int cost;
    Node(int to, int cost) : to(to), cost(cost){};
};
bool operator < (Node a, Node b){
    return a.cost > b. cost;// 这里不要写错,我们需要定义的是升序的优先队列
}
int h[MAX_N];


// 将名称转换为下标
map<string, int> nameToIndex;
string names[MAX_N];
int cnt = 0;
int getIndex(string s){
    if(nameToIndex.find(s) == nameToIndex.cend()){
        nameToIndex[s] = cnt;
        names[cnt] = s;
        return cnt++;
    }else{
        return nameToIndex[s];
    }
}
int main(){
    // 读取N,K和起点
    int N, K;
    string s;
    cin >> N >> K >> s;
    int source = getIndex(s);
    
    // 读取各个城市的幸福指数
    for(int i = 1, t; i < N; i++){
        cin >> s >> t;
        h[getIndex(s)] = t;
    }
    
    // 读取道路
    vector<vector<Edge>> G(N);
    string s1, s2;
    for(int i = 0, cost, u, v; i < K; i++){
        cin >> s1 >> s2 >> cost;
        u = getIndex(s1);
        v = getIndex(s2);
        G[u].push_back(Edge(v, cost));
        G[v].push_back(Edge(u, cost));
    }
    
    
    // 初始化相关信息
    int target = getIndex("ROM");
    bool marked[MAX_N];
    int edgeTo[MAX_N], hSum[MAX_N], road[MAX_N], distTo[MAX_N], cityCnt[MAX_N];
    fill(marked, marked + N, false);
    fill(edgeTo, edgeTo + N, -1);
    fill(hSum, hSum + N, 0);
    fill(road, road + N, 1);
    fill(distTo, distTo + N, INT_MAX);
    fill(cityCnt, cityCnt + N, 0);

    // 初始化优先队列
    priority_queue<Node> pq;
    pq.push(Node(source, 0));
    distTo[source] = 0;
    
    // Dijkstra求最短路
    int u, v;
    while (!pq.empty()) {
        u = pq.top().to;
        pq.pop();
        if(marked[u]) continue;
        if(u == target) break;// 找到终点,退出,可以减少循环次数
        marked[u] = true;
        for(Edge& e : G[u]){
            v = e.to;
            if(marked[v]) continue;
            if(distTo[u] + e.cost < distTo[v]){
                distTo[v] = distTo[u] + e.cost;
                edgeTo[v] = u;
                hSum[v] = h[v] + hSum[u];
                cityCnt[v] = cityCnt[u] + 1;
                road[v] = road[u];
                pq.push(Node(v, distTo[v]));
            }else if(distTo[u] + e.cost == distTo[v]){
                if(hSum[u] + h[v] > hSum[v] ||
                   (hSum[u] + h[v] == hSum[v] && (hSum[u] + h[v]) / (cityCnt[u] + 1) > hSum[v] / cityCnt[v])
                   ){
                    edgeTo[v] = u;
                    hSum[v] = h[v] + hSum[u];
                    cityCnt[v] = cityCnt[u] + 1;
                }
                road[v] += road[u];
            }
        }
    }
    
    // 循环获取路径
    vector<int> path;
    for(int x = target; x != -1; x = edgeTo[x]){
        path.push_back(x);
    }
    
    // 输出结果和路径
    printf("%d %d %d %d\n", road[target], distTo[target], hSum[target], hSum[target] / cityCnt[target]);
    for(int i = (int)path.size() - 1; i > 0; i--){
        printf("%s->", names[path[i]].c_str());
    }
    printf("%s", names[path[0]].c_str());
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值