PAT (Advanced Level) Practise 1087 All Roads Lead to Rome (30)

本文介绍了一个寻找从起点到罗马的最经济路线的问题,同时考虑了路线成本和途经城市的幸福感指数。通过Dijkstra算法实现路径查找,确保找到成本最低且幸福感最高的路线。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1087. All Roads Lead to Rome (30)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 recommended. 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 recommended 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

题意:一共有n个地点,告诉你起点,终点都为“ROM“,除起点以外,其他点都有一个权值,一共有m条边,每条边有个一个权值,现在走到终点边的权值和要最小,并找出有几种走法,若有多条,找出点的权值和最大的,若仍有多条,找出点的权值和平均值最大的。


#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;

#define LL long long
const int INF = 0x3f3f3f3f;

map<string,int>mp;
int x[205],n,m;
int s[205],nt[80009],e[80009],w[80009];
int vis[205],dis[205],sum[205],p[205],path[205],ave[205];
char s1[5],s2[5],ch[205][5];

struct node
{
    int id,k,dis;
    friend bool operator <(node a,node b)
    {
        return a.dis>b.dis;
    }
}pre,nt1;

void Dijkstra(int k)
{
    memset(vis,0,sizeof vis);
    memset(dis,INF,sizeof dis);
    memset(sum,0,sizeof sum);
    memset(ave,0,sizeof ave);
    dis[1]=0,path[1]=1,p[1]=-1;
    pre.id=1,pre.dis=0,pre.k=1;
    priority_queue<node>q;
    q.push(pre);
    while(!q.empty())
    {
        pre=q.top();
        q.pop();
        vis[pre.id]=1;
        if(pre.id==k) break;
        for(int i=s[pre.id];~i;i=nt[i])
        {
            int ee=e[i],ww=w[i];
            if(vis[ee]) continue;
            if(dis[ee]>dis[pre.id]+ww)
            {
                dis[ee]=dis[pre.id]+ww;
                p[ee]=pre.id;
                sum[ee]=sum[pre.id]+x[ee];
                path[ee]=path[pre.id];
                ave[ee]=sum[ee]/pre.k;
                nt1.k=pre.k+1,nt1.id=ee,nt1.dis=dis[ee];
                q.push(nt1);
            }
            else if(dis[ee]==dis[pre.id]+ww)
            {
                path[ee]+=path[pre.id];
                if(sum[ee]<sum[pre.id]+x[ee])
                {
                    sum[ee]=sum[pre.id]+x[ee];
                    ave[ee]=sum[ee]/pre.k;
                    p[ee]=pre.id;
                }
                else if(sum[ee]==sum[pre.id]+x[ee]&&ave[ee]<sum[ee]/pre.k)
                {
                    ave[ee]=sum[ee]/pre.k;
                    p[ee]=pre.id;
                }
            }
        }
    }
    printf("%d %d %d %d\n",path[k],dis[k],sum[k],ave[k]);
}

int main()
{
    while(~scanf("%d%d%s",&n,&m,ch[1]))
    {
        mp.clear();
        mp[ch[1]]=1;
        int cnt=1,ee;
        memset(s,-1,sizeof s);
        for(int i=2;i<=n;i++)
        {
            scanf("%s%d",ch[i],&x[i]);
            mp[ch[i]]=i;
            if(!strcmp(ch[i],"ROM")) ee=mp[ch[i]];
        }
        for(int i=1;i<=m;i++)
        {
            int ww;
            scanf("%s%s%d",s1,s2,&ww);
            int uu=mp[s1],vv=mp[s2];
            nt[cnt]=s[uu],s[uu]=cnt,e[cnt]=vv,w[cnt++]=ww;
            nt[cnt]=s[vv],s[vv]=cnt,e[cnt]=uu,w[cnt++]=ww;
        }
        Dijkstra(ee);
        int k=ee,flag=1;
        stack<int>s;
        while(k!=-1)
        {
            s.push(k);
            k=p[k];
        }
        while(!s.empty())
        {
            int pre=s.top();
            s.pop();
            if(flag) printf("%s",ch[pre]);
            else printf("->%s",ch[pre]);
            flag=0;
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值