【PAT甲级】1003 Emergency

简介: 【PAT甲级】1003 Emergency

1003 Emergency

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


题意

题目给定一张城市之间的关系图,道路是双向连通的,现在需要找到从源点 S 到终点 T 的最短路径。


另外,每个城市内有不同数量的救援队,没走到一个城市,这个城市的救援队就会跟着你去支援终点 T ,要计算出从从源点 S 到终点 T 在走最短路的情况下能召集到救援队数量的最大值。


第一行给定四个数,分别是城市数量 N NN 、道路数量 M MM 、起点 S 和终点 T 。


第二行给定每个城市的救援队数量,城市编号为 0 ∽ N − 1 0 \backsim N-10∽N−1 。


接下来的 M MM 行,给定从一个城市到另一个城市的路径长度。


思路

这道题我们可以用迪杰斯特拉算法求最短路,这个算法大致的思想就是每次都找到图中最短的那条路径,然后更新于它相邻边的路进值,具体的讲解可以参考我之前的博客,那里有更详细的图解。

这道题只需在原有的迪杰斯特拉算法模板上,顺便去更新每个结点的最大数量以及最短路的数量即可。


代码

#include<bits/stdc++.h>
using namespace std;
const int N = 550;
int n, m, S, T;
int d[N][N];
int dist[N], sum[N], cnt[N], w[N];
bool st[N];
void dijkstra()
{
    //初始化
    memset(dist, 0x3f, sizeof dist);
    sum[S] = w[S], cnt[S] = 1, dist[S] = S;
    for (int i = 0; i < n; i++)
    {
        int t = -1;
        for (int j = 0; j < n; j++)    //找到一条最短路
            if (!st[j] && (t == -1 || dist[t] > dist[j]))
                t = j;
        st[t] = true;
        for (int j = 0; j < n; j++)    //更新信息
            if (dist[j] > dist[t] + d[t][j]) //如果出现更短的路
            {
                dist[j] = dist[t] + d[t][j];
                cnt[j] = cnt[t];
                sum[j] = sum[t] + w[j];
            }
            else if (dist[j] == dist[t] + d[t][j])   //如果发现相同的最短路
            {
                cnt[j] += cnt[t];
                sum[j] = max(sum[j], sum[t] + w[j]);
            }
    }
}
int main()
{
    cin >> n >> m >> S >> T;
    for (int i = 0; i < n; i++)    cin >> w[i];  //输入每个城市救援队数量
    //输入每条道路
    memset(d, 0x3f, sizeof d);
    while (m--)
    {
        int a, b, c;
        cin >> a >> b >> c;
        d[a][b] = d[b][a] = min(d[a][b], c);
    }
    dijkstra(); //迪杰斯特拉算法
    cout << cnt[T] << " " << sum[T] << endl;
    return 0;
}



目录
相关文章
|
Java 数据库 网络架构
Hystrix使用及其配置详解
Hystrix使用及其配置详解
2197 0
Hystrix使用及其配置详解
|
存储 SQL 人工智能
CCCF专栏 | 加密数据库技术:前沿与展望
本文将从数据安全防护的重大战略需求出发,聚焦数据安全搜索、加密数据库技术等前沿领域,深入探讨加密数据库的发展现状,揭示其设计过程中存在的安全性和性能方面的挑战,并提出未来关于加密数据库建设的一些愿景。
CCCF专栏 | 加密数据库技术:前沿与展望
|
SQL 存储 关系型数据库
MySQL创建数据表(CREATE TABLE语句)
MySQL创建数据表(CREATE TABLE语句)
2481 0
|
12月前
|
人工智能 运维 自然语言处理
别让 LLM 变成“甩锅发动机”——从安全、审计、隐私聊聊运维智能助手怎么落地
别让 LLM 变成“甩锅发动机”——从安全、审计、隐私聊聊运维智能助手怎么落地
853 117
|
算法 安全 量子技术
【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | 马特赛特旋转算法 | LCG 线性同余算法 | Python Random 模块
【Python】蒙特卡洛模拟 | PRNG 伪随机数发生器 | 马特赛特旋转算法 | LCG 线性同余算法 | Python Random 模块
1579 0
|
8月前
|
JSON 文字识别 API
图像内容理解-图像智能分析-图片内容理解API接口介绍
图像内容理解服务基于视觉大模型,支持人、物、行为、场景、文字等多维度识别,可生成一句话描述、分类标签及OCR文字信息。提供“提交请求”和“获取结果”两个API接口,适用于看图问答、视觉推理等场景。
1358 13
|
SQL 关系型数据库 MySQL
SQL命令行退出操作指南:轻松掌握不同数据库环境下的退出技巧
在数据库管理与开发过程中,经常需要通过SQL命令行工具(如MySQL的mysql客户端、PostgreSQL的psql、SQL Server的sqlcmd等)与数据库进行交互
2044 59
“燕缘 · 滴水湖”大学生AI+国际创业大赛 ——算力资源领取说明
本次大赛为每位参赛学生提供300元阿里云算力资源,通过赛事官网链接领取。领取流程包括:点击专属页面、扫码登录阿里云、完成代金券领取,并在“我的权益-可用券”中查看领取状态。注意代金券使用期限和额度,避免超额使用产生欠费。若已领取过,可直接使用。
|
Ubuntu Linux 数据安全/隐私保护
Ubuntu 安装教程(U 盘安装 Ubuntu 详细教程)
完成上述步骤后,Ubuntu将开始安装在你的电脑上。安装完成后,重启电脑,并按提示移除U盘。电脑将自动从硬盘启动进入新装的Ubuntu系统。现在你可以开始探索Ubuntu带来的全新体验了!
|
搜索推荐 区块链 开发者
【python程序打包教程】PyInstaller一键打包Python程序为独立可执行exe文件
【python程序打包教程】PyInstaller一键打包Python程序为独立可执行exe文件