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月前
1003 Emergency (25 分)
1003 Emergency (25 分)
|
机器学习/深度学习 算法
【PAT甲级】1003 Emergency
【PAT甲级】1003 Emergency
61 0
|
测试技术
华为机试HJ98:自动售货系统
华为机试HJ98:自动售货系统
122 1
|
计算机视觉
Crack Slide | hb省建筑市场监管公共服务平台滑块分析(一个从开始就失败的案例,0.1星)
Crack Slide | hb省建筑市场监管公共服务平台滑块分析(一个从开始就失败的案例,0.1星)
101 0
|
算法 C++
【PAT甲级 - C++题解】1072 Gas Station
【PAT甲级 - C++题解】1072 Gas Station
70 0
|
存储 算法 前端开发
Tech-Finger游戏搬砖套利系统开发技术方案丨Tech-Finger游戏搬砖套利系统开发(逻辑及详细)/源码功能/案例开发
智能合约的执行,是通过验证程序代码完成的。各个节点收到交易信息后,自动会对其进行签名验证,以确保交易的有效性。各验证节点对某一交易达成共识后,智能合约将自动执行,并通知交易当事人及全网。同时,智能合约自带的状态机会判断所属合约的状态。
PAT甲级 1008. Elevator (20分)
PAT甲级 1008. Elevator (20分)
78 0
PAT甲级 1005. Spell It Right(20分)
PAT甲级 1005. Spell It Right(20分)
53 0
|
人工智能 定位技术 Go
UPC——2020年春混合个人训练第二十五场(FG)
UPC——2020年春混合个人训练第二十五场(FG)
91 0
SAP PM 初级系列22 - IW38可以批量处理维修工单
SAP PM 初级系列22 - IW38可以批量处理维修工单
SAP PM 初级系列22 - IW38可以批量处理维修工单