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 ...
随机推荐
- 分布式id生成
2016年08月09日 14:15:21 yuanyuanispeak 阅读数:318 编辑 一.需求缘起 几乎所有的业务系统,都有生成一个记录标识的需求,例如: (1)消息标识:message-id ...
- Webpack4 学习笔记六 多页面配置和devtool
webpack 多页配置 webpack可以配置单页应用, 也可以配置多页应用. 区别在于, 单页应用entry入口只有一个, 而多页应用入口有多个 webpack配置: const path = r ...
- MySql Connector/C++8结果集处理Demo
#include <iostream> #include <exception> #include <mysqlx/xdevapi.h> using std::co ...
- ABAP术语-BAPI (Business Application Programming Interface)
BAPI (Business Application Programming Interface) 原文:http://www.cnblogs.com/qiangsheng/archive/2007/ ...
- oracle约束约束状态和设计习惯
oracle约束状态有几个项目,会让人迷惑,分别是: enable/disable--是否启用/禁用 validate/invalidate--确认/不确认 deferrable/not deferr ...
- 【nat---basic,napt,easy ip】
display nat :显示nat 信息 debugging nat :对nat进行调试 reset nat session:擦除nat连接配置 basic-nat:公网->私网(一对一) n ...
- angularjs Directive自定义指令详解
作用:需要用Directive有下面的情景: 1.使你的Html更具语义化,不需要深入研究代码和逻辑即可知道页面的大致逻辑. 2. 抽象一个自定义组件,在其他地方进行重用. 3.使用公共代码,减少重复 ...
- redis 带入的挖矿病毒 qW3xT.2 wnTKYg 解决方法
最近我的阿里云ecs 老是收到 云盾态势感知系统检测到异常 top -c 后发现一个 疑似病毒 /tmp/qW3xT.2 看到网友们的解决方案 试过之后效果不错,可以用的 知道wnTKYg是什么鬼之 ...
- doT.js使用介绍
doT.js特点是快,小,无依赖其他插件,压缩版仅有4K大小. doT.js详细使用介绍 使用方法: 1 2 3 4 5 6 7 {{ }} 模板 开始标记 结束标记 {{= }} 赋值 {{~ ...
- 8.1 编写USB鼠标驱动程序,并测试
学习目标:编写USB鼠标驱动程序,并测试(将USB鼠标的左键当作L按键,将USB鼠标的右键当作S按键,中键当作回车按键). 一.怎么写USB设备驱动程序?步骤如下: 1. 首先先定义全局变量usb_d ...