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

目录
相关文章
|
18天前
|
人工智能
爆了爆了!Claude Fable 5 延期,codex 取消5小时使用限制,AI 外卖大战开打
Claude Fable 5 续期一周至 7/19,周额度暴涨 50%;OpenAI 回击取消所有计划使用限制。AI 大厂竞争白热化,用户赢麻了。
爆了爆了!Claude Fable 5 延期,codex 取消5小时使用限制,AI 外卖大战开打
|
23天前
|
人工智能 自然语言处理 搜索推荐
AI 技术在英语教育中的应用
AI正重塑英语教育:以语音交互、智能批改、个性化阅读词汇、全息学情画像及教学辅助五大能力,覆盖听、说、读、写、词全链路,解决开口难、反馈慢、个性化弱等痛点,赋能少儿启蒙、应试备考与成人学习,构建“AI训练+教师引导”新型教学模式。(239字)
|
域名解析 人工智能 运维
DataWorks AI助理:在钉钉让AI助理帮你盯任务、修问题
DataWorks AI助理支持定时巡检监控规则告警及指定任务异常,可对接钉钉等IM端实时推送、诊断并自动修复问题。用户在移动端即可完成告警接收、分析、确认与修复全流程,无需切换PC端,大幅提升运维效率。
306 0
|
10月前
|
程序员
程序员自设置的喝水闹钟程序,助力身体健康
长时间对着电脑、忙于工作或带娃,常常忘记喝水?Aipy 提醒喝水小工具,每45分钟弹出可爱提示,持续30秒无法关闭,强制提醒你补水,让喝水变得有趣又有仪式感!
|
存储 Java 关系型数据库
JPA1|学习笔记
快速学习JPA1
416 0
JPA1|学习笔记
|
机器学习/深度学习 数据采集 人工智能
【深度学习前沿应用】文本生成
【自然语言处理(NLP)】文本生成,基于百度飞桨开发,参考于《机器学习实践》所作。
433 1
【深度学习前沿应用】文本生成
|
测试技术
不同的子序列(LeetCode-115)
不同的子序列(LeetCode-115)
324 0
不同的子序列(LeetCode-115)
|
机器人
不同路径Ⅱ(LeetCode-63)
不同路径Ⅱ(LeetCode-63)
183 0
不同路径Ⅱ(LeetCode-63)
|
算法
LeetCode 135.分发糖果(贪心算法)
LeetCode 135.分发糖果(贪心算法)
331 0
LeetCode 135.分发糖果(贪心算法)
|
存储 算法 Java
Java数据结构与算法分析(二)稀疏数组
在介绍稀疏数组前我们先来引入一个需求,下面是一个五子棋的棋盘(15 * 15),玩到中途时想要保存离开,希望下次打开还可以继续玩。我们怎么实现呢?
292 0