PAT 甲级 1003 Emergency
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, 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
时间复杂度:$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的更多相关文章
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
- 图论 - PAT甲级 1003 Emergency C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
- PAT 甲级 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- 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 ...
- PAT Advanced 1003 Emergency 详解
题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...
- 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 ...
- PAT甲级1003题解——Dijkstra
解题步骤: 1.初始化:设置mat[][]存放点之间的距离,vis[]存放点的选取情况,people[]存放初始时每个城市的人数,man[]存放到达每个城市的救援队的最多的人数,num[]存放到达每个 ...
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
随机推荐
- centos7-mongodb3.4.6集群的搭建
0.需要环境 安装包:mongodb-linux-x86_64-3.4.6.tgz 安装路径:/usr/mongodb 服务器: 192.168.177.131/132/133 mongos 2000 ...
- 【c学习-12】
/*枚举*/ #include void enumFunction(){ enum enum_var{"a","b",1,2}; enum{"c&qu ...
- Linux Shell常用命令(长期更新)
#判断某个字段是否匹配指定值 awk -F"," '{if($4=="value"){print $1} else {print $0}}' file.txt ...
- hadoop2.5.0 HA高可用配置
hadoop2.5.0 HA配置 1.修改hadoop中的配置文件 进入/usr/local/src/hadoop-2.5.0-cdh5.3.6/etc/hadoop目录,修改hadoop-env.s ...
- 中国农产品信息网站scrapy-redis分布式爬取数据
---恢复内容开始--- 基于scrapy_redis和mongodb的分布式爬虫 项目需求: 1:自动抓取每一个农产品的详细数据 2:对抓取的数据进行存储 第一步: 创建scrapy项目 创建爬虫文 ...
- python 迭代器 和生成器
迭代器 # 双下方法 # print([1].__add__([2])) # print([1]+[2]) # 迭代器 # l = [1,2,3] # 索引 # 循环 for # for i in l ...
- Leecode刷题之旅-C语言/python-20.有效的括号
/* * @lc app=leetcode.cn id=20 lang=c * * [20] 有效的括号 * * https://leetcode-cn.com/problems/valid-pare ...
- mysql5.7.19安装报错 无法定位程序输入点
https://blog.csdn.net/t876587201/article/details/79503688
- NO-ZERO(空格补全)
The NO-ZERO command follows the DATA statement REPORT Z_Test123_01. DATA: W_NUR(10) TYPE N. MOVE 50 ...
- PHP.45-TP框架商城应用实例-后台20-权限管理-RBAC表构造与代码生成
权限管理 三张主表{p39_privilege(权限).p39_role(角色).p39_admin(管理)} 两张中间表{p39_role_pri(角色-权限).p39_admin_role(管理- ...