https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376

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 (≤) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - 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 c​1​​, c​2​​ 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 C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, 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

时间复杂度:$O(2 * N ^ 2)$

代码:

#include <bits/stdc++.h>
using namespace std; #define inf 0x3f3f3f3f
int N, M, C1, C2;
int cnt = 0;
int mp[550][550];
int team[550];
int vis[550], dis[550];
int see[550];
int amount[550]; void dijkstra(int act) {
memset(vis, 0, sizeof(vis));
memset(dis, inf, sizeof(dis)); dis[act] = 0;
int temp = act;
see[act] = 1;
amount[act] = team[act]; for(int i = 0; i < N; i ++) {
dis[i] = mp[act][i];
if(dis[i] != inf && i != act) {
amount[i] = amount[act] + team[i];
see[i] = 1;
}
} for(int i = 0; i < N - 1; i ++) {
int minn = inf;
for(int j = 0; j < N; j ++) {
if(dis[j] < minn && vis[j] == 0) {
minn = dis[j];
temp = j;
}
} vis[temp] = 1;
for(int k = 0; k < N; k ++) {
if(!vis[k] && dis[k] > dis[temp] + mp[temp][k]) {
dis[k] = dis[temp] + mp[temp][k];
amount[k] = amount[temp] + team[k];
see[k] = see[temp];
}
else if(!vis[k] && dis[k] == dis[temp] + mp[temp][k]) {
see[k] += see[temp];
if (amount[k] < amount[temp]+team[k])
amount[k] = amount[temp]+team[k];
}
}
}
return;
} int main() {
scanf("%d%d%d%d", &N, &M, &C1, &C2);
for(int i = 0; i < N; i ++)
scanf("%d", &team[i]); memset(mp, inf, sizeof(mp));
for(int i = 1; i <= M; i ++) {
int a, b, cost;
scanf("%d%d%d", &a, &b, &cost);
mp[a][b] = mp[b][a] = cost;
} dijkstra(C1);
printf("%d %d\n", see[C2], amount[C2]);
return 0;
}

  

PAT 甲级 1003 Emergency的更多相关文章

  1. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

  2. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  3. PAT 甲级 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  4. PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  5. PAT Advanced 1003 Emergency 详解

    题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...

  6. PAT Advanced 1003 Emergency (25) [Dijkstra算法]

    题目 As an emergency rescue team leader of a city, you are given a special map of your country. The ma ...

  7. PAT甲级1003题解——Dijkstra

    解题步骤: 1.初始化:设置mat[][]存放点之间的距离,vis[]存放点的选取情况,people[]存放初始时每个城市的人数,man[]存放到达每个城市的救援队的最多的人数,num[]存放到达每个 ...

  8. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  9. PAT 解题报告 1003. Emergency (25)

    1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...

随机推荐

  1. 关于对连接数据库时出现1130-host “**” is not allowed to connect to this MySql/mariadb server 的错误解决方法

    在完成mariadb的搭建后,在端口与防火墙均为正常的情况下,出现了1130- Host xxx is not allowed to connect to this MariaDb server 的情 ...

  2. python核心编程2 第十三章 练习

    13-3.对类进行定制.写一个类,用来将浮点型值转换为金额. class MoneyFmt(object): def __init__(self, value=0.0, flag='-'): self ...

  3. JetBrains 全家桶激活码

    原文地址:https://blog.csdn.net/usher_ou/article/details/77970172 RDNJ63FZWD-eyJsaWNlbnNlSWQiOiJSRE5KNjNG ...

  4. 【ospf-vlink虚拟连接】

    根据项目需求,搭建好如下拓扑图 配置rt1的环回 口地址及g0/0/0的ip地址 配置rt1的ospf 配置rt2的环回口地址和g0/0/1及g0/0/0的ip地址 \ 配置rt2的ospf 同理,配 ...

  5. Docker(一):概述

    Docker 是什么? Docker是一个开源的应用容器引擎,基于Go语言开发 并遵从Apache2.0协议开源. Docker 可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后 ...

  6. MIP缓存加速原理 MIP不仅仅只是CDN

    什么是MIP?我想我们现在都知道.可是你真的了解MIP吗?MIP加速原理是什么?MIP 是用 CDN 做加速的么?准确答案是:是,但不只是. 很多人并认为MIP百度排名会靠前,甚至权重会提高?作为一个 ...

  7. mac phpstorm 破解方法

    方法参考如下链接: 来源:http://www.cnblogs.com/zyliang/p/6148960.html

  8. elasticsearch 5.x 系列之六 文档索引,更新,查询,删除流程

    一.elasticsearch index 索引流程 步骤: 客户端向Node1 发送索引文档请求 Node1 根据文档ID(_id字段)计算出该文档应该属于shard0,然后请求路由到Node3的P ...

  9. ruby Time类与Date类

    Time类用于表示时间.时间除了表示年月日时分秒的信息外,还包含了表示地域时差的时区(time zone)信息.例如我们可以计算中国当前时间是国际协调时间的几点 Date类只用于表示年月日.因此,相对 ...

  10. 初识python 字符串 列表 字典相关操作

    python基础(一): 运算符: 算术运算: 除了基本的+ - * / 以外,还需要知道 :  // 为取整除 返回的市商的整数部分 例如: 9 // 2  ---> 4  , 9.0 //  ...