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]),比较先前最短路径下的救援队伍数和当前路径下的救援队伍数,输出最大值。

目录
相关文章
|
3月前
|
人工智能 算法 安全
IROS 2025 |从数字智能走向物理智能,“桃源”与真实世界机器人学习挑战赛启动,2大赛道等你来战
2025年10月,IROS (智能机器人与系统国际会议)期间,上海人工智能实验室(上海AI实验室)将举办物理世界中的多模态机器人学习研讨会,IROS 2025“桃源”与真实世界机器人学习挑战赛(机器人学习挑战赛)现已启动报名,欢迎全球创新者与挑战者参加。
551 0
|
2月前
|
存储 消息中间件 NoSQL
Redis数据结构:别小看这5把“瑞士军刀”,用好了性能飙升!
Redis提供5种基础数据结构及多种高级结构,如String、Hash、List、Set、ZSet,底层通过SDS、跳表等实现高效操作。灵活运用可解决缓存、计数、消息队列、排行榜等问题,结合Bitmap、HyperLogLog、GEO更可应对签到、UV统计、地理位置等场景,是高性能应用的核心利器。
|
7月前
|
机器学习/深度学习 人工智能 自然语言处理
这个模型让AI角色会说话还会演!MoCha:Meta联手滑铁卢大学打造对话角色视频生成黑科技
MoCha是由Meta与滑铁卢大学联合开发的端到端对话角色视频生成模型,通过创新的语音-视频窗口注意力机制实现精准的唇语同步和全身动作生成。
404 12
这个模型让AI角色会说话还会演!MoCha:Meta联手滑铁卢大学打造对话角色视频生成黑科技
|
UED 开发者
鸿蒙next版开发:ArkTS组件通用属性(运动模糊)
在HarmonyOS 5.0中,ArkTS引入了运动模糊功能,允许开发者为组件添加动态模糊效果,增强视觉表现。本文详细解读了运动模糊的属性和使用方法,并提供了示例代码。运动模糊可增强视觉效果、提升用户体验和实现动态效果,适用于多种场景。
355 2
|
缓存 测试技术 调度
PolarDB-X的TPC-H列存执行计划
本文从官方的角度逐条解析PolarDB-X在TPC-H列存执行计划的设计要点。这些要点不仅包含了各项优化的原理,还提供了相关的证明与代码实现,希望帮助读者更深入地理解PolarDB-X的列存优化器。
8310 26
|
存储 人工智能 算法
AI伦理学:建立可信的智能系统框架
【9月更文挑战第26天】随着AI技术的迅猛发展,其在各领域的应用日益广泛,但也带来了算法偏见、数据隐私泄露、就业替代等伦理和法律挑战。本文探讨AI伦理学的核心议题,包括数据隐私保护、算法公平性与透明度、机器决策责任归属及对就业市场的影响,并提出建立可信智能系统框架的建议,如强化法律法规、技术创新、建立监督机制、行业自律和公众教育,以确保AI技术的可持续发展和社会接受。
|
运维 监控 物联网
物联网卡:如何选择物联网卡流量套餐
选择物联网卡(IoT SIM卡)的流量套餐时,需要根据设备的具体使用场景、数据需求量、成本预算以及长期扩展性等多方面因素进行综合考虑。以下是一些建议步骤,帮助你做出合适的选择:
|
存储 算法 C语言
C语言手撕实战代码_二叉树_构造二叉树_层序遍历二叉树_二叉树深度的超详细代码实现
这段代码和文本介绍了一系列二叉树相关的问题及其解决方案。其中包括根据前序和中序序列构建二叉树、通过层次遍历序列和中序序列创建二叉树、计算二叉树节点数量、叶子节点数量、度为1的节点数量、二叉树高度、特定节点子树深度、判断两棵树是否相似、将叶子节点链接成双向链表、计算算术表达式的值、判断是否为完全二叉树以及求二叉树的最大宽度等。每道题目均提供了详细的算法思路及相应的C/C++代码实现,帮助读者理解和掌握二叉树的基本操作与应用。
352 2
|
关系型数据库 MySQL 数据库
mysql中tonumber函数使用要注意什么
在处理这类转换操作时,考虑周全,利用提供的高性能云服务器资源,可以进一步提升数据库处理效率,确保数据操作的稳定性和安全性,尤其是在处理大量数据转换和运算密集型应用时。
355 0
|
机器学习/深度学习 数据采集 搜索推荐
打造个性化新闻推荐系统
【8月更文挑战第31天】在这个信息爆炸的时代,个性化新闻推荐系统成为了连接用户与海量资讯的桥梁。本文将引导你通过Python编程语言和机器学习技术,搭建一个简单的新闻推荐模型。我们将从数据预处理开始,逐步深入到模型的训练与评估,最终实现一个能够根据用户兴趣推荐新闻的系统。无论你是编程新手还是有一定基础的学习者,这篇文章都将为你打开一扇通往智能推荐世界的大门。