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. FP side-effects

    https://medium.com/javascript-scene/master-the-javascript-interview-what-is-a-pure-function-d1c076be ...

  2. 阿里配置docker镜像专属地址

    阿里配置docker镜像专属地址 待办 https://www.jianshu.com/p/6b416dff0691

  3. date时间比较

    比较前先要对时间判断不能为空 int result = tk.getCloseTime().compareTo(tk.getPlanEndTime()); result =  1: 代表 closeT ...

  4. Seekbar扩大点击区域

    //扩大点击区域private void enlargeSeekBar() { mContentView.setOnTouchListener(new OnTouchListener() {//mCo ...

  5. 在VS2017中配置VLD(Visual Leak Detector)内存泄漏检测工具

    首先在官方下载VLD 下载地址: https://kinddragon.github.io/vld/ 此版本为V2.5.1,为最后发布版本,下载后安装.加入你的安装路径为:VLD_Path,后面会用到 ...

  6. 计算几何-Ang-Rad-Vector

    This article is made by Jason-Cow.Welcome to reprint.But please post the article's address. 旋转,跳跃,梦境 ...

  7. at org.apache.hadoop.hbase.tmpl.master.BackupMasterStatusTmplImpl.renderNoFlush(BackupMasterStatusTm

    at org.apache.hadoop.hbase.tmpl.master.BackupMasterStatusTmplImpl.renderNoFlush(BackupMasterStatusTm ...

  8. map文章

    STL map常用操作简介 http://www.kuqin.com/cpluspluslib/20071231/3264.html STL中map用法详解 http://www.kuqin.com/ ...

  9. rancher 方式创建nfs-client 存储类流程

    rancher 方式创建nfs-client 存储类流程 待办 https://www.iamle.com/archives/2514.html

  10. java后台接受不到vue传的参数

    @RequestMapping(value = "/delBelowImg") @Transactional public R delBelowFile(@RequestParam ...