【PAT甲级】1087 All Roads Lead to Rome

简介: 【PAT甲级】1087 All Roads Lead to Rome

1087 All Roads Lead to Rome

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


题意

从我们的城市到达罗马有许多不同的旅游路线。


请你在成本最低的旅游路线之中,找到使得游客获得最大幸福感的路线。如果该路线不唯一,则找到最大平均幸福感的路线,其中平均幸福感是不包括起点的。


第一行输入分别为城市数量 N NN ,路线数量 K KK ,以及起点。


接下来的 N − 1 N-1N−1 行输入每个城市的幸福感值。


接下来的 K KK 行则输入每条路的信息。


第一行输出四个整数,最小成本的路线数量,最小成本,幸福感,平均幸福感(只取整数部分)。


第二行,按照 City1->City2->...->ROM 的格式输出路线。


思路

这道题同样可以利用迪杰斯特拉算法去找最优解,具体思路如下:


1.先对所有城市进行映射,因为给定的城市名字为字符串类型,需要映射成整数,可以用一个哈希表 mp 存储每个城市映射后的下标。

2.输入每条路的信息,注意这里需要用到上面映射后的下标进行存储,之后的迪杰斯特拉同理。

3.在进行迪杰斯特拉算法的过程中,不断更新答案,找到最优路线。首先要去判断当前路径花费是否最小,如果存在另一条花费相等的路径则要进一步判断该路径获得的幸福感是否最大,如果还是出现相等的路径则再进一步判断该路径的结点数量是否最小,然后进行对应的更新操作。

4.输出最终结果,注意需要先将 pre 中的路线全部存到一个容器中再逆向输出。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 210;
int n, k;
//最小花费、最小花费路径数量、幸福感值、最小点数、最短路径的前一个点
int dist[N], cnt[N], hp[N], sum[N], pre[N];
int d[N][N], w[N];
bool st[N];
string city[N];
unordered_map<string, int> mp;
//迪杰斯特拉算法
void dijkstra()
{
    //初始化
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0, cnt[1] = 1;
    for (int i = 0; i < n; i++)
    {
        int t = -1;
        for (int j = 1; j <= n; j++)   //找到最短边权
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;
        for (int j = 1; j <= n; j++)   //更新信息
            if (dist[j] > dist[t] + d[t][j]) //如果最小花费可以更新
            {
                dist[j] = dist[t] + d[t][j];
                cnt[j] = cnt[t];
                hp[j] = hp[t] + w[j];
                sum[j] = sum[t] + 1;
                pre[j] = t;
            }
            else if (dist[j] == dist[t] + d[t][j])   //如果最小花费相等
            {
                cnt[j] += cnt[t];
                if (hp[j] < hp[t] + w[j])    //如果幸福感可以更新
                {
                    hp[j] = hp[t] + w[j];
                    sum[j] = sum[t] + 1;
                    pre[j] = t;
                }
                else if (hp[j] == hp[t] + w[j])  //如果幸福感也相等
                {
                    if (sum[j] > sum[t] + 1) //如果存在点数更小的路径
                    {
                        sum[j] = sum[t] + 1;
                        pre[j] = t;
                    }
                }
            }
    }
}
int main()
{
    cin >> n >> k >> city[1];
    mp[city[1]] = 1;
    //对所有城市进行映射
    for (int i = 2; i <= n; i++)
    {
        cin >> city[i] >> w[i];
        mp[city[i]] = i;
    }
    //输入所有边的信息
    memset(d, 0x3f, sizeof d);
    for (int i = 0; i < k; i++)
    {
        string a, b;
        int x, y, c;
        cin >> a >> b >> c;
        x = mp[a], y = mp[b];
        d[x][y] = d[y][x] = min(d[x][y], c);
    }
    dijkstra(); //寻找满足要求的路径
    //输出结果
    int T = mp["ROM"];
    cout << cnt[T] << " " << dist[T] << " " << hp[T] << " " << hp[T] / sum[T] << endl;
    vector<int> path;
    for (int i = T; i != 1; i = pre[i])  path.push_back(i);
    cout << city[1];
    for (int i = path.size() - 1; i >= 0; i--)
        cout << "->" << city[path[i]];
    cout << endl;
    return 0;
}


目录
相关文章
|
4天前
|
人工智能 自然语言处理 Shell
🦞 如何在 Moltbot 配置阿里云百炼 API
本教程指导用户在开源AI助手Clawdbot中集成阿里云百炼API,涵盖安装Clawdbot、获取百炼API Key、配置环境变量与模型参数、验证调用等完整流程,支持Qwen3-max thinking (Qwen3-Max-2026-01-23)/Qwen - Plus等主流模型,助力本地化智能自动化。
🦞 如何在 Moltbot 配置阿里云百炼 API
|
8天前
|
人工智能 API 开发者
Claude Code 国内保姆级使用指南:实测 GLM-4.7 与 Claude Opus 4.5 全方案解
Claude Code是Anthropic推出的编程AI代理工具。2026年国内开发者可通过配置`ANTHROPIC_BASE_URL`实现本地化接入:①极速平替——用Qwen Code v0.5.0或GLM-4.7,毫秒响应,适合日常编码;②满血原版——经灵芽API中转调用Claude Opus 4.5,胜任复杂架构与深度推理。
|
2天前
|
人工智能 JavaScript 应用服务中间件
零门槛部署本地AI助手:Windows系统Moltbot(Clawdbot)保姆级教程
Moltbot(原Clawdbot)是一款功能全面的智能体AI助手,不仅能通过聊天互动响应需求,还具备“动手”和“跑腿”能力——“手”可读写本地文件、执行代码、操控命令行,“脚”能联网搜索、访问网页并分析内容,“大脑”则可接入Qwen、OpenAI等云端API,或利用本地GPU运行模型。本教程专为Windows系统用户打造,从环境搭建到问题排查,详细拆解全流程,即使无技术基础也能顺利部署本地AI助理。
4256 5
|
2天前
|
人工智能 JavaScript API
零门槛部署本地 AI 助手:Clawdbot/Meltbot 部署深度保姆级教程
Clawdbot(Moltbot)是一款智能体AI助手,具备“手”(读写文件、执行代码)、“脚”(联网搜索、分析网页)和“脑”(接入Qwen/OpenAI等API或本地GPU模型)。本指南详解Windows下从Node.js环境搭建、一键安装到Token配置的全流程,助你快速部署本地AI助理。(239字)
2648 15
|
3天前
|
机器人 API 数据安全/隐私保护
只需3步,无影云电脑一键部署Moltbot(Clawdbot)
本指南详解Moltbot(Clawdbot)部署全流程:一、购买无影云电脑Moltbot专属套餐(含2000核时);二、下载客户端并配置百炼API Key、钉钉APP KEY及QQ通道;三、验证钉钉/群聊交互。支持多端,7×24运行可关闭休眠。
3098 4
|
3天前
|
人工智能 安全 Shell
在 Moltbot (Clawdbot) 里配置调用阿里云百炼 API 完整教程
Moltbot(原Clawdbot)是一款开源AI个人助手,支持通过自然语言控制设备、处理自动化任务,兼容Qwen、Claude、GPT等主流大语言模型。若需在Moltbot中调用阿里云百炼提供的模型能力(如通义千问3系列),需完成API配置、环境变量设置、配置文件编辑等步骤。本文将严格遵循原教程逻辑,用通俗易懂的语言拆解完整流程,涵盖前置条件、安装部署、API获取、配置验证等核心环节,确保不改变原意且无营销表述。
1775 3
|
12天前
|
JSON API 数据格式
OpenCode入门使用教程
本教程介绍如何通过安装OpenCode并配置Canopy Wave API来使用开源模型。首先全局安装OpenCode,然后设置API密钥并创建配置文件,最后在控制台中连接模型并开始交互。
5165 8
|
3天前
|
存储 安全 数据库
使用 Docker 部署 Clawdbot(官方推荐方式)
Clawdbot 是一款开源、本地运行的个人AI助手,支持 WhatsApp、Telegram、Slack 等十余种通信渠道,兼容 macOS/iOS/Android,可渲染实时 Canvas 界面。本文提供基于 Docker Compose 的生产级部署指南,涵盖安全配置、持久化、备份、监控等关键运维实践(官方无预构建镜像,需源码本地构建)。
2136 6
|
3天前
|
人工智能 应用服务中间件 API
刚刚,阿里云上线Clawdbot全套云服务!
阿里云上线Moltbot(原Clawdbot)全套云服务,支持轻量服务器/无影云电脑一键部署,可调用百炼平台百余款千问模型,打通iMessage与钉钉消息通道,打造开箱即用的AI智能体助手。
2320 18
刚刚,阿里云上线Clawdbot全套云服务!
|
3天前
|
人工智能 安全 应用服务中间件
首个 Clawdbot 全流程部署方案!真“AI 个人助理”来了!
GitHub爆火AI Agent Moltbot(原Clawdbot)上线即获7.6万+ Star!它能理解自然语言、调用工具、自动执行任务。阿里云轻量应用服务器推出“开箱即用”部署方案:预装环境、直连百炼大模型、支持钉钉等消息通道,5分钟一键启用,稳定、安全、低成本。
首个 Clawdbot 全流程部署方案!真“AI 个人助理”来了!

热门文章

最新文章