PAT甲级 1003. Emergency(25分)

简介: PAT甲级 1003. Emergency(25分)

1003. Emergency(25分)


As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.


Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.


Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.


Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1
结尾无空行


Sample Output:

2 4
结尾无空行

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
struct node
{
    int vertex;
    int dist;
};
class mycomparison
{
public:
    bool operator()(const node &a, node &b)
    {
        return a.dist > b.dist;
    }
};
int main()
{
    // cin
    int N, M, c1, c2;
    cin >> N >> M >> c1 >> c2;
    int rescueteams[N];
    for (int i = 0; i < N; i++)
    {
        cin >> rescueteams[i];
    }
    int G[N][N];
    for (int i = 0; i < N; i++)
    {
        for (int j = 0; j < N; j++)
        {
            G[i][j] = 0;
        }
    }
    for (int i = 0; i < M; i++)
    {
        int start, end, weight;
        cin >> start >> end >> weight;
        G[start][end] = weight;
        G[end][start] = weight;
    }
    vector<bool> visit(N, false);
    vector<int> teams(N, 0);
    vector<int> dist(N, 1e9);
    vector<int> num(N, 1);
    priority_queue<node, vector<node>, mycomparison> q;
    node tmp;
    // Dijkstra
    visit[c1] = true;
    dist[c1] = 0;
    teams[c1] = rescueteams[c1];
    for (int i = 0; i < N; i++)
    {
        if (G[c1][i])
        {
            dist[i] = G[c1][i];
            teams[i] = teams[c1] + rescueteams[i];
            tmp.dist = dist[i];
            tmp.vertex = i;
            q.push(tmp);
        }
    }
    while (!q.empty())
    {
        int minv = q.top().vertex;
        visit[minv] = true;
        q.pop();
        for (int i = 0; i < N; i++)
        {
            if (!visit[i] && G[minv][i] && dist[minv] + G[minv][i] <= dist[i])
            {
                if (dist[minv] + G[minv][i] == dist[i])
                {
                    teams[i] = max(teams[i], teams[minv] + rescueteams[i]);
                    num[i]++;
                }
                else
                {
                    teams[i] = teams[minv] + rescueteams[i];
                }
                dist[i] = dist[minv] + G[minv][i];
                tmp.dist = dist[i];
                tmp.vertex = i;
                q.push(tmp);
            }
        }
    }
    cout << num[c2] << " " << teams[c2];
    return 0;
}

本题主要考察Dijkstra算法,我通过最小堆实现。这道题卡了挺久,主要是没有很好的理解题意。题目要输出的是最短路径数量和最大救援队伍数。对应要修改Dijkstra模板的部分为 dist[minv] + G[minv][i] <= dist[i],在判断通过当前最短路径点经过当前点时是否小于等于当前点原先储存的最短路径。特别要注意等于的情况,如果等于,最短路径数量加一,救援队伍数 max(teams[i], teams[minv] + rescueteams[i]),比较先前最短路径下的救援队伍数和当前路径下的救援队伍数,输出最大值。

目录
相关文章
|
5月前
|
人工智能
爆了爆了!Claude Fable 5 延期,codex 取消5小时使用限制,AI 外卖大战开打
Claude Fable 5 续期一周至 7/19,周额度暴涨 50%;OpenAI 回击取消所有计划使用限制。AI 大厂竞争白热化,用户赢麻了。
爆了爆了!Claude Fable 5 延期,codex 取消5小时使用限制,AI 外卖大战开打
|
26天前
|
存储 弹性计算 人工智能
阿里云轻量应用服务器2核2G200M峰值带宽到底多少钱?38元、68元、79元、459元哪个准?
阿里云轻量应用服务器2核2G在同一页面上会同时出现38元、68元、79元、459元四个价格,对应四类账号身份。38元与68元均为新用户价,前者每日10点、15点两场限量抢购、限购1次、不可叠券、续费恢复常规价,后者无需抢购;79元适用于买过其他云产品但未开通过服务器的产品新用户;459元为老用户常规价。身份按账号有无云产品付费记录判定,同一实名主体下的账号会合并判定,该配置为200M峰值带宽。
阿里云轻量应用服务器2核2G200M峰值带宽到底多少钱?38元、68元、79元、459元哪个准?
|
2月前
|
人工智能 弹性计算 双11
2026年阿里云双11大促活动:大模型、Token Plan、AI建站、云服务器优惠活动汇总
2026年阿里云双11围绕「模型能力+订阅额度+建站工具+云资源组合」四层结构展开:大模型侧推出Qwen3.8-Flash、Wan3.0-Video与Qwen3.8-Max,并有全模型通享低至4.5折抵扣(包月10元起、包季直省55%)与达量返还;Token Plan个人版79元/月起、团队版150元/月起,套餐支持Qwen3.8-Max;AI建站含万小智3.0体验版9.9元/月与秒悟Meoo套餐25元/月起;云服务器从轻量38元/年到13.8万元GPU实例,ECS 99元与199元新购续费同价。本文系统梳理各板块权益、计费逻辑与限制条件,供不同使用强度的个人、团队与企业测算选型。
|
3月前
|
存储 人工智能 开发框架
【新版】阿里云 无影云电脑 产品功能介绍及配置价格表
传统本地PC、物理工作站面临硬件迭代快、算力固定、数据分散难以管控、异地办公设备受限、AI智能体7×24小时持续运行硬件成本高等现实痛点。无影云电脑(Elastic Desktop Service,EDS)是托管式云上虚拟桌面服务,把完整计算机运行环境部署在云端算力底座,计算、存储全部运行在云端,本地终端只负责画面接收、键鼠输入输出,本地终端不会留存业务原始数据,实现数据不落地的安全办公模式。产品分为个人版、企业版两大产品线,覆盖个人学习办公、远程开发、图形设计渲染、企业批量办公、AI智能体云端工位、RPA自动化等多元场景,底层搭载自研ASP自适应流化协议,兼顾画面流畅度与网络弱网适配能力,配
685 0
域名解析 人工智能 运维
849 0
|
4月前
|
人工智能 JSON 算法
AI 漫剧量产实战:StableDiffusion 帧稳画算法 + ComfyUI 自动质检流水线搭建(附完整代码 + 配置)
本文针对Stable Diffusion漫剧量产中的人设漂移、画风跳变、色彩断层等时序不稳问题,提出“帧特征校验+自动瑕疵检测+闭环重渲染”标准化流水线。通过结构化Prompt、ComfyUI节点固化、人脸特征向量比对及OpenClaw锁色等工程化方案,实现AI漫剧长序列风格一致、稳定落地。含完整工具包与可运行代码。(239字)
674 0
|
5月前
|
人工智能 自然语言处理 搜索推荐
AI 技术在英语教育中的应用
AI正重塑英语教育:以语音交互、智能批改、个性化阅读词汇、全息学情画像及教学辅助五大能力,覆盖听、说、读、写、词全链路,解决开口难、反馈慢、个性化弱等痛点,赋能少儿启蒙、应试备考与成人学习,构建“AI训练+教师引导”新型教学模式。(239字)
|
5月前
|
传感器 监控 JavaScript
工业24V电源稳压方案:PW8600/PW75XX/PW2312B/PW2815选型对比
本指南详解24V稳压芯片选型:PW8600(80V/100mA超低功耗LDO)、PW75XX(36V/150mA高精度LDO)、PW2312B(60V/0.6A小封装DC-DC)、PW2815(80V/1.5A工业级DC-DC),覆盖LDO与开关方案,兼顾效率、噪声与可靠性。
1040 0
|
6月前
|
人工智能 缓存 自然语言处理
阿里云百炼Token Plan标准/高级/尊享坐席怎么选?Token Plan额度、计费与成本效益全面分析
阿里云百炼Token Plan是面向团队的AI大模型订阅服务,以Credits作为统一计量积分,覆盖文本生成、图像生成、代码开发等全场景模型调用,兼容主流AI编程与智能体工具,提供团队管理、预算管控、数据安全等企业级能力。该方案采用包月订阅制,提供标准、高级、尊享三档坐席,用户按月付费获取固定Credits额度,所有模型调用、工具使用、上下文缓存均按统一规则折算Credits扣除,告别传统按量付费的账单波动,实现AI使用成本的精准可控。本文将从资费额度、Token消耗规则、核心权益、性价比四个维度,全面对比三大坐席差异,为不同使用强度的团队提供清晰的选型参考。
777 0
|
8月前
|
机器学习/深度学习 人工智能 自然语言处理
AI范式迁移,从“数据驱动”到“认知自主”的跨越
当2026年全球AI领域的焦点从“参数规模比拼”转向“认知能力突破”,一个清晰的信号已然显现:人工智能正经历一场深刻的范式迁移,从依赖海量数据喂养的“被动学习”,迈向具备自主认知、逻辑推理与场景适配的“主动智能”。
400 0