All Roads Lead to Rome(DJ易错点)

原题链接:
https://pintia.cn/problem-sets/994805342720868352/problems/994805379664297984
题目描述:
                                                                                                         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
思路:这道题我最开始就想到用dj,但是这道题还要求最短路径的数目,我又想到之前的那个先广搜求最短路,然后再深搜求最优解的那个题了,难道这道题也是先dj,再dfs?
其实不用这么麻烦,只需要在更新到某点的最短路时把路径的条数更新即可,就是如果更新了最短路径,num[v]=num[u],如果路径长度想等,num[v]+=num[u],这就是松弛的好处了
真的强,再也不直接在结构体里面修改数据了...
AC代码:

#include<iostream>
#include<set>
#include<algorithm>
#include<string.h>
#include<string>
#include<cmath>
#include<map>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 100005;
struct  edge{
    int t,w, nxt;
}e[maxn];
int hd[maxn], tot;
void add(int f, int t,int w) {
    e[++tot].t = t;
    e[tot].w = w;
    e[tot].nxt = hd[f];
    hd[f] = tot;
}
int pos(string& name) {
    return ((name[0] - 'A') * 26 * 26) + (name[1] - 'A') * 26 + name[2] - 'A';
}
struct node{
    int d, val,id,len;
    bool operator<(node b)const {
        if (d != b.d)return d > b.d;
        else if (val != b.val)return val < b.val;
        else return len > b.len;
    }
};
int n, m,st, ed;
int val[maxn];
bool vis[maxn];
int pre[maxn];
int dis[maxn];
int num[maxn];
int hap,l;
void print(int u) {
    printf("%c%c%c", u / (26 * 26) + 'A', (u / 26) % 26 + 'A', u % 26 + 'A');
}
void Dj() {
    priority_queue<node>Q;
    memset(dis, 0x3f, sizeof(dis));
    dis[st] = 0;
    num[st] = 1;
    Q.push({ 0,0,st,0 });
    while (!Q.empty()) {
        node t = Q.top(); Q.pop();
        if (vis[t.id])continue;
        vis[t.id] = 1;
        if (t.id == ed) {
            l = t.len;
            hap = t.val; return;
        }
        int u = t.id;
        for (int i = hd[u]; i; i = e[i].nxt) {
            int v = e[i].t;
            if (dis[u] + e[i].w < dis[v]) {
                    dis[v] = dis[u] + e[i].w;
                    pre[v] = u;
                    node p = { dis[v],t.val + val[v],v,t.len + 1 };
                    num[v] = num[u];
                    Q.push(p);
                }
            else if (dis[u] + e[i].w == dis[v]) {
                node p = { dis[v],t.val + val[v],v,t.len + 1 };
                Q.push(p);
                num[v] += num[u];
            }
        }
    }
}
vector<int>path;
void getpath() {
    int t = ed;
    while (1) {
        path.push_back(t);
        if (t == st)break;
        t = pre[t];
    }
    reverse(path.begin(), path.end());
}
int main() {
    //freopen("test.txt", "r", stdin);
    scanf("%d%d", &n, &m);
    string s,e="ROM";
    cin >> s;
    st = pos(s); ed = pos(e);
    for (int i = 0; i < n-1; i++) {
        cin >> s;
        scanf("%d", &val[pos(s)]);
    }
    string a, b; int cost;
    for (int i = 0; i < m; i++) {
        cin >> a >> b; scanf("%d", &cost);
        add(pos(a), pos(b),cost);
        add(pos(b), pos(a),cost);
    }
    Dj();
    getpath();
    cout << num[ed] << " " << dis[ed] << " " << hap<<" "<<hap/l << endl;
    for (int i = 0; i < path.size(); i++) {
        if (i != 0)printf("->");
        print(path[i]);
    }
    return 0;
}

 

posted @ 2021-03-03 22:10  cono奇犽哒  阅读(126)  评论(0)    收藏  举报