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:

  1. 5 6 0 2
  2. 1 2 1 5 3
  3. 0 1 1
  4. 0 2 2
  5. 0 3 1
  6. 1 2 1
  7. 2 4 1
  8. 3 4 1

Sample Output:

  1. 2 4

思路

题意是,让你求C1->C2 最短路径的个数,然后输出你能调动救援队的最大值。

网上很多人是基于dijkstra做出来的,我只写了个简单的搜索。

用数组记录城市之间的道路长度。注意有个坑,可能出现自己救自己的情况(即 C1=C2)。

  1. #include <stdio.h>
  2. #include <string>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. #include <vector>
  6. #include <string.h>
  7. #include <algorithm>
  8. #include <limits.h>
  9. #include <cmath>
  10. #include <map>
  11. using namespace std;
  12. int m[505][505];
  13. int n, M, c1, c2;
  14. int num[505];
  15. int vis[505];
  16. int road_num;
  17. int max_n = - 1;
  18. int min_dis = INT_MAX;
  19. void dfs(int index, int sum, int dis){
  20. if(dis > min_dis) return;
  21. if(index == c2){
  22. if(dis < min_dis){
  23. min_dis = dis;
  24. road_num = 1;
  25. max_n = sum;
  26. }
  27. else if(dis == min_dis){
  28. road_num++;
  29. max_n = max(sum, max_n);
  30. }
  31. return;
  32. }
  33. for(int i = 0; i < n; i++){
  34. if(m[index][i] && !vis[i]){
  35. vis[i] = 1;
  36. dfs(i, sum + num[i], dis + m[index][i]);
  37. vis[i] = 0;
  38. }
  39. }
  40. }
  41. int main() {
  42. cin >> n >> M >> c1 >> c2;
  43. for(int i = 0; i < n; i++){
  44. cin >> num[i];
  45. }
  46. for(int i = 0; i < M; i++){
  47. int s = 0, e = 0, l = 0;
  48. cin >> s >> e >> l;
  49. m[s][e] = l;
  50. m[e][s] = l;
  51. }
  52. if(c1 != c2) vis[c1] = 1;
  53. dfs(c1, num[c1], 0);
  54. cout << road_num << " " << max_n;
  55. return 0;
  56. }

PAT 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)

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

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

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

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

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

  5. 1003 Emergency (25分)

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

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

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

  7. PAT 甲级 1003. Emergency (25)

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

  8. PAT 1003 Emergency[图论]

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

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

随机推荐

  1. C++-HDU1000,1001,1002-格式是真的坑

    #include <cstdio> int main(){ for(int a,b;~scanf("%d%d",&a,&b);printf(" ...

  2. 牛客多校第二场H Second Large Rectangle 单调栈or悬线法

    Second Large Rectangle 题意 给出n*m的01矩阵,问由1组成的第二大的矩阵的大小是多少? 分析 单调栈(or 悬线法)入门题 单调栈 预处理出每一个点的最大高度,然后单调栈每一 ...

  3. 红帽 Red Hat Linux相关产品iso镜像下载【百度云】【更新7.6】

    不为什么,就为了方便搜索,特把红帽EL 5.EL6.EL7 的各版本整理一下,共享出来.原文链接正式发布 7.6 :RedHat Enterprise Server 7.6 for x86_64:rh ...

  4. STL之pair类型

    C++ pair 类型 ---心怀虔诚,细细欣赏! 编程实践: Practice:编写程序读入一系列string和int型数据,将每一组存储在一个pair对象中,然后将这些pair对象存储在vecto ...

  5. java基础(十)之向上转型/向下转型

    向上转型:将子类的对象赋值给父类的引用. 向下转型:将父类的对象赋值给子类的引用. 向上转型 Student a = new Student(); Person zhang = a; 或者 Perso ...

  6. SpringMVC开发RESTful接口

    概念: 什么是REST? REST是Representational State Transfer的缩写.翻译为"表现层状态转化",restful是一种接口设计风格,它不是一个协议 ...

  7. nfs 动态文件挂载读写权限设置

    nfs 动态文件挂载读写权限设置 待办 ll 命令查看文件夹权限 参考设置共享文件夹https://www.linuxidc.com/Linux/2018-11/155331.htm

  8. Adobe PS

    1. ctrl + Tab  切换视图窗口 2.shift 拖拽图片,将 2 张图片放在一起 3.切换显示方式 /全屏/带有工具栏 快捷键:F 4. 缩小/放大工具 快捷键: alt + 鼠标滑轮 5 ...

  9. SpringMVC--提交表单

    今天使用AbstractCommandController做一个提交表单的样例 (1)首先,建立一个User.java package com.zk.domain; import java.util. ...

  10. hibernate和mybatis出现配置文件xml的文件报错Multiple annotations found at this line(转)

    hibernate中的xml配置文件Multiple annotations found at this line,出现这个红叉报错,直接是把 <?xml version="1.0&q ...