1087 All Roads Lead to Rome (30 分)——甲级(Dijkstra复杂变形+记录路径+map)

14 篇文章 0 订阅
9 篇文章 0 订阅

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

题目大意:给出一个城市地图,包括起点、每个城市的幸福指数、两个城市间的距离。现要求输出:1.从起点到终点ROM的最短路径长度的条数;2.最短路径长度;3.最短路径下的最大幸福指数;4.最大幸福指数下的最大平均幸福指数。

os:题目看起来很好理解,条件还是有点多的,应该是PAT中Djkstra变形中很复杂的了。

思路:

  • 城市由字符串给出,需要用map将城市和编号对应起来,方便后续Dijkstra的操作;同时,在输出路径时要找到编号对应的城市。所以应该有两个map数组:citytoi[ ]、itocity[ ];
  • e[ ][ ]存图、point[ ]存点权、标记数组vis[ ];
  • dis[ ]表示到源点最短路径、happy[ ]表示最短路径下的最大幸福指数、step[ ]表示最大幸福指数下走的步数、num[ ]表示最短路径条数、path[ ]记录前驱结点;
  • Dijkstra的时候考虑三个因素。第一因素:路径长度最短。第二因素:幸福指数最大。第三因素:平均幸福指数最大;
  • 递归函数输出路径(或者栈)。

ps:输入点权的时候不包括起点,计算幸福指数的时候也就不考虑起点。

代码如下:

#include <iostream>
#include <cstdio>
#include <string>
#include <map>
#define maxn 205
#define inf 0x3f3f3f3f
using namespace std;
map<string, int>citytoi;
map<int, string>itocity;
int e[maxn][maxn], point[maxn], vis[maxn];/* 边权、点权、标记 */
int dis[maxn], happy[maxn], step[maxn], num[maxn], path[maxn];/* Dijkstra操作数组 */
int n, k;
string st;
void show(int i)
{
    if(path[i] == -1)
    {
        cout << itocity[i];
        return ;
    }
    show(path[i]);
    cout << "->" << itocity[i];
}
int main()
{
    cin >> n >> k >> st;
    int i, j;
    for(i=1; i<=n; i++)/* 初始化邻接矩阵 */
    {
        e[i][i] = 0;
        vis[i] = 0;
        for(j=i+1; j<=n ;j++)
            e[i][j] = e[j][i] = inf;
    }

    citytoi[st] = 1;
    itocity[1] = st;/* 为方便起见,起点的编号置为1 */
    for(i=2; i<=n; i++)
    {
        string s;
        int h;
        cin >> s >> h;
        if(s == st) point[1] = h;
        else
        {
            citytoi[s] = i;
            itocity[i] = s;
            point[i] = h;
        }
    }

    while(k--)
    {
        string u, v;
        int d;
        cin >> u >> v >> d;
        int a = citytoi[u];
        int b = citytoi[v];
        e[a][b] = e[b][a] = d;
    }

    for(i=1; i<=n; i++) dis[i] = e[1][i];
    happy[1] = point[1];
    step[1] = 0;
    num[1] = 1;
    path[1] = -1;

    for(i=0; i<n; i++)
    {
        int k = -1;
        int min = inf;
        for(j=1; j<=n; j++)
            if(!vis[j] && min > dis[j])
            {
                min = dis[j];
                k = j;
            }
        if(k == -1) break;
        vis[k] = 1;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dis[j] > dis[k] + e[k][j])/* 第一因素 */
            {
                num[j] = num[k];/* 路径长度不同,条数更新 */
                dis[j] = dis[k] + e[k][j];
                happy[j] = happy[k] + point[j];
                step[j] = step[k] + 1;
                path[j] = k;
            }
            else if(!vis[j] && dis[j] == dis[k] + e[k][j])
            {
                num[j] += num[k];/* 路径长度相同,条数相加 */
                if(happy[j] < happy[k] + point[j])/* 第二因素 */
                {
                    happy[j] = happy[k] + point[j];
                    step[j] = step[k] + 1;
                    path[j] = k;
                }
                else if(happy[j] == happy[k] + point[j])
                {
                    if(step[j] > step[k] + 1)/* 第三因素 */
                    {
                        step[j] = step[k] + 1;
                        path[j] = k;
                    }
                }
            }
        }
    }
    int endi = citytoi["ROM"];/* 先计算终点编号 */
    printf("%d %d %d %d\n", num[endi], dis[endi], happy[endi], happy[endi] / step[endi]);
    show(endi);
    return 0;
}

总结

Dijkstra变形题已经做了一些了,大致就是以下一些变形:

  1. 输入不是直接给出编号,而是以字符串或者其他形式给出城市。考虑map容器;
  2. 边权不是只有距离,也会有花费、时间等等。同时也不是只有边权,也会有点权:城市人数、幸福指数等等。根据因素优先级层层递进;
  3. 求解不是只求最短路径长度,也会有最短路径条数、路径的输出等等。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值