pat甲 1087

A1087 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

解题思路

本题求从某城市到城市"ROM"的最短路径,若最短路径有多条则取幸福值最大的那条
①:由于城市是用字符串给出的,需要将用map<string,int> string_int和 map<int,string> int_string将城市按数字编号
②:套用Dijkstra加DFS模型(算法笔记),由于题目要求输出最短路径的条数,要用数 组num[v]表示从起点到点v的最短路劲数,当d[v]<d[u]+G[u][v]时,点v继承点u的 最短路径数,当d[v]=d[u]+G[u][v]时,点v最短路径数等于点v本来的最短路径数加 上点u的最短路径数即num[v]+=num[u]
③:根据题目条件输出即可_

AC代码

#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int INF = 1000000000;
const int maxn = 210;

int n, m, cnt = 0;                                                    //cnt统计有多少个城市,从0开始给出现的城市编号
string st;                                                            //起点,终点
int G[maxn][maxn], d[maxn], num[maxn], happy[maxn], maxHappiness = 0; //邻接矩阵,最短距离,最短路径的条数
bool vis[maxn];                                                       //是否访问过
map<string, int> string_int;                                          //城市名->数字序号
map<int, string> int_string;                                          //数字序号->城市名
vector<int> pre[maxn], tempPath, ansPath;

void change(string s) //将城市名字按数字顺序编号
{
    if (string_int.find(s) == string_int.end())
    {
        string_int[s] = cnt++;
        int_string[string_int[s]] = s;
    }
}

void Dijkstra(int s)
{
    memset(vis, false, sizeof(vis));
    fill(num, num + maxn, 0);
    fill(d, d + maxn, INF);
    d[s] = 0;
    num[s] = 1; //起点到起点最短路径数初始化为1
    for (int i = 0; i < n; i++)
    {
        int u = -1, min = INF;
        for (int j = 0; j < n; j++)
        {
            if (vis[j] == false && d[j] < min)
            {
                u = j;
                min = d[j];
            }
        }
        if (u == -1)
            return;
        vis[u] = true;
        for (int v = 0; v < n; v++)
        {
            if (vis[v] == false && G[u][v] != INF)
            {
                if (d[v] > d[u] + G[u][v])
                {
                    d[v] = d[u] + G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                    num[v] = num[u];
                }
                else if (d[v] == d[u] + G[u][v])
                {
                    pre[v].push_back(u);
                    num[v] += num[u]; //刚开始错在num[v]=num[u]+1;
                }
            }
        }
    }
}

void DFS(int v)
{
    if (v == string_int[st])
    {
        tempPath.push_back(v);
        int sumHappiness = 0;
        for (int i = 0; i < tempPath.size() - 1; i++)
        {
            sumHappiness += happy[tempPath[i]];
        }
        if (sumHappiness > maxHappiness)
        {
            ansPath = tempPath;
            maxHappiness = sumHappiness;
        }
        tempPath.pop_back();
        return;
    }
    tempPath.push_back(v);
    for (int i = 0; i < pre[v].size(); i++)
    {
        DFS(pre[v][i]);
    }
    tempPath.pop_back();
}

int main()
{
    string s1, s2, s3;
    int happiness;
    fill(G[0], G[0] + maxn * maxn, INF);
    cin >> n >> m >> st;
    change(st);
    for (int i = 1; i < n; i++)
    {
        cin >> s1 >> happiness;
        change(s1);
        happy[string_int[s1]] = happiness;
    }
    for (int i = 0; i < m; i++)
    {
        cin >> s2 >> s3 >> happiness;
        change(s2);
        change(s3);
        G[string_int[s2]][string_int[s3]] = happiness;
        G[string_int[s3]][string_int[s2]] = happiness;
    }
    Dijkstra(string_int[st]);
    DFS(string_int["ROM"]);
    printf("%d %d %d %d\n", num[string_int["ROM"]], d[string_int["ROM"]], maxHappiness, maxHappiness / (ansPath.size() - 1));
    for (int i = ansPath.size() - 1; i >= 0; i--)
    {
        if (i != 0)
            cout << int_string[ansPath[i]] << "->";
        else
            cout << int_string[ansPath[i]];
    }
    return 0;
}

本文章使用limfx的vscode插件快速发布