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

题意:计算从救援队当前所在的城市到目标城市的通路数,途中经过的城市会有救援人员,求不同的通路上救援人员的最大值。

思路:(边)最短路问题+ (顶点)最大价值

AC 代码:

#include<iostream>
#include<algorithm>
using namespace std; int n, m, c1, c2;
int e[510][510], weight[510], dis[510], num[510], w[510];
bool visit[510];
const int inf = 99999999; int main() {
scanf("%d%d%d%d",&n, &m, &c1, &c2);
for(int i = 0; i < n; ++i) {
scanf("%d", &weight[i]);
}
fill(e[0], e[0] + 510 * 510, inf);
fill(dis, dis + 510, inf);
int a, b, c;
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", &a, &b, &c);
e[a][b] = e[b][a] = c;
}
dis[c1] = 0;
w[c1] = weight[c1];
num[c1] = 1;
for (int i = 0; i < n; ++i) {
int u = -1, minn = inf;
for (int j = 0; j < n; ++j) {
if (visit[j] == false && dis[j] < minn) {
u = j;
minn = dis[j];
}
} if (u == -1) break;
visit[u] = true;
for (int v = 0; v < n; ++v) {
if (dis[u] + e[u][v] < dis[v]) {
dis[v] = dis[u] + e[u][v];
num[v] = num[u];
w[v] = w[u] + weight[v];
} else if (dis[u] + e[u][v] == dis[v]) {
num[v] = num[v] + num[u];
if (w[u] + weight[v] > w[v])
w[v] = w[u] + weight[v];
}
}
}
printf("%d %d", num[c2], w[c2]);
return 0;
}

2021-01-29

用DFS来做

python版:

# 信息读入
num_city, num_roads, cur_city, save_city = list(map(int, input().split()))
rescue = list(map(int, input().split()))
roads = [[] for _ in range(num_city)]
for _ in range(num_roads):
a = list(map(int, input().split()))
roads[a[0]].append((a[1], a[2]))
roads[a[1]].append((a[0], a[2])) # 定义几个变量
min_roads, max_rescue, min_distance = 0, 0, 99999
temp_distance, temp_rescue = 0, rescue[cur_city]
visited = {cur_city} def dfs(city):
global save_city, temp_distance, min_distance, temp_rescue, min_roads, max_rescue, visited
if city == save_city:
if temp_distance < min_distance:
min_distance = temp_distance
min_roads = 1
max_rescue = temp_rescue
elif temp_distance == min_distance:
min_roads += 1
if temp_rescue > max_rescue:
max_rescue = temp_rescue
return for next_city, distance in roads[city]:
if next_city not in visited:
visited.add(next_city)
temp_distance += distance
temp_rescue += rescue[next_city]
dfs(next_city)
temp_distance -= distance
temp_rescue -= rescue[next_city]
visited.remove(next_city) dfs(cur_city)
print(min_roads, max_rescue)

参考:

https://www.liuchuo.net/archives/2359

https://qsctech-sange.github.io/1003-Emergency.html#python3

1003 Emergency (25分)的更多相关文章

  1. 1003 Emergency (25分) 求最短路径的数量

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

  2. PAT 1003 Emergency (25分)

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

  3. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

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

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

  5. PAT 1003. Emergency (25)

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

  6. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

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

  7. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

  8. PAT 甲级 1003. Emergency (25)

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

  9. 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 ...

随机推荐

  1. Python数据读取函数

    1.读取mat数据 import scipy.io as sio data_mat = sio.loadmat(data.mat) 官方文档 获取的数据为字典型,其中"data"为 ...

  2. HashSet为什么可以有序输出?

    首先HashSet是不保证有序,而不是保证无序,因为在HashSet中,元素是按照他们的hashCode值排序存储的.对于单个字符而言,这些hashCode就是ASCII码,因此,当按顺序添加自然数或 ...

  3. 14. vue源码入口+项目结构分析

    一. vue源码 我们安装好vue以后, 如何了解vue的的代码结构, 从哪里下手呢? 1.1. vue源码入口 vue的入口是package.json 来分别看看是什么含义 dependences: ...

  4. CSS篇-dispaly、position、定位机制、布局、盒子模型、BFC

    display常用值 参考链接英文参考链接中文 // 常用值 none:元素不显示 inline:将元素变为内联元素,默认 block:将元素变为块级元素 inline-block:将元素变为内联块级 ...

  5. android 调用js,js调用android

    Java调用JavaScript   1.main.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <?xml v ...

  6. Vue.js 学习笔记之七:使用现有组件

    5.3 使用现有组件 在之前的五个实验中,我们所演示的基本都是如何构建自定义组件的方法,但在具体开发实践中,并非项目中所有的组件都是需要程序员们自己动手来创建的.毕竟在程序设计领域,"不要重 ...

  7. HDU-6862 Hexagon (2020HDU 多校 D8 H)

    1008 题意:半径为n的六边形(由半径为1的小六边形组成),从某一个小六边形出发有六个方向,找到一条转向次数最多的路径(用方向表示)遍历所有的六边形(一个六边形只访问一次). 题解:先画出n=3/4 ...

  8. 攻防世界 reverse BABYRE

    BABYRE   XCTF 4th-WHCTF-2017 int __cdecl main(int argc, const char **argv, const char **envp) { char ...

  9. RPC 框架设计

    RPC 框架设计 初识 RPC 服务化有什么好处? 防止代码拷贝 防止底层复杂性的扩散 防止公共库的耦合 保证 SQL 的质量,能够解除数据库的耦合 什么是 RPC RPC:Remote Proced ...

  10. [leetcode] 单调栈

    本文总结单调栈算法. 原问题 学习一个算法,我们需要清楚的是:这个算法最原始的问题背景是什么样的? 下一个更小元素 给定一个数组 nums,返回每个元素的下一个更小的元素的下标 res,即 res[i ...