1003 Emergency (25)(25 分)

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

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

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
    //转自:https://www.cnblogs.com/549294286/p/3571448.html
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4.  
  5. using namespace std;
  6.  
  7. #define INF 0x3f3f3f3f
  8. #define MX 501
  9.  
  10. int mp[MX][MX];
  11. int v[MX];
  12. int dist[MX];
  13. int amount[MX];
  14. int teams[MX];
  15. int pathcount[MX];
  16. int N,M,start,en;
  17.  
  18. void dijkstra(int s){
  19. amount[s] = teams[s];//s为开始时
  20. dist[s] = ;
  21. pathcount[s] = ;//目前有一条路
  22.  
  23. while (){
  24. int u, dmin=INF;
  25. for (int i=; i<N; i++){
  26. if (v[i]== && dist[i]<dmin){//v一开始全为0
  27. dmin = dist[i];//第一次循环,选出来的点是s
  28. u = i;//
  29. }
  30. }
  31. if (dmin==INF || u==en) break;
  32. v[u] = ;//设置为被访问过,每次都选最小的那个点出来。
  33. printf("\n");
  34. for (int i=; i<N; i++){
  35. if(v[i]==){
  36. if (dist[i] > dist[u] + mp[u][i]){
  37. dist[i] = dist[u] + mp[u][i];
  38. amount[i] = amount[u] + teams[i];
  39. pathcount[i] = pathcount[u];
  40. }else if (mp[u][i]!=INF&&dist[i] == dist[u] + mp[u][i]){
  41. pathcount[i] += pathcount[u];
  42. if (amount[i] < amount[u] + teams[i])
  43. amount[i] = amount[u] + teams[i];
  44. }
  45. printf("%d %d %d %d %d\n",u,i,dist[i],pathcount[i],amount[i]);
  46. }
  47.  
  48. }
  49. }
  50. }
  51.  
  52. int main()
  53. {
  54. scanf("%d%d%d%d", &N,&M,&start,&en);
  55. for (int i=; i<N; i++)
  56. {
  57. scanf("%d", &teams[i]);//每个点所有的救援队数
  58. }
  59. for (int i=; i<N; i++)
  60. {
  61. dist[i] = INF;//每一个距离初始化为正无穷。
  62. for (int j=; j<N; j++)
  63. mp[i][j] = INF;
  64. }
  65. for (int i=; i<M; i++)
  66. {
  67. int c1, c2, L;
  68. scanf("%d%d%d", &c1,&c2,&L);
  69. mp[c1][c2] = mp[c2][c1] = L;//将没有边的设置为正无穷。
  70. }
  71.  
  72. dijkstra(start);
  73. printf("%d %d", pathcount[en], amount[en]);
  74.  
  75. return ;
  76. }

//意思就是给出一个图,每个点有权值,边有权重,找出最短的权重的边,并且记录点的最大的权值和。就是要找出所有的最短边,并且进行比较。

//这里是给出了一个循环,每次找出最短的,并且遍历所有的点。看是否可以更新距离。如果到当前点的距离相等的话,那么就路径数相加,并且amount[i]取两者较大值。这是一种题型。应该会的。

代码来自:https://www.liuchuo.net/archives/2359

  1. #include <iostream>
  2. #include <algorithm>
  3. using namespace std;
  4. int n, m, c1, c2;
  5. int e[][], weight[], dis[], num[], w[];
  6. bool visit[];
  7. const int inf = ;
  8. int main() {
  9. scanf("%d%d%d%d", &n, &m, &c1, &c2);
  10. for(int i = ; i < n; i++)
  11. scanf("%d", &weight[i]);
  12. fill(e[], e[] + * , inf);//原来二维数组是这样初始化的。
  13. fill(dis, dis + , inf);
  14. int a, b, c;
  15. for(int i = ; i < m; i++) {
  16. scanf("%d%d%d", &a, &b, &c);
  17. e[a][b] = e[b][a] = c;
  18. }
  19. dis[c1] = ;
  20. w[c1] = weight[c1];
  21. num[c1] = ;//到当前点的共计路径条数。
  22. for(int i = ; i < n; i++) {
  23. int u = -, minn = inf;
  24. for(int j = ; j < n; j++) {
  25. if(visit[j] == false && dis[j] < minn) {
  26. u = j;
  27. minn = dis[j];
  28. }
  29. }
  30. if(u == -) break;//迪杰斯特拉都有这么一个判断,就是找不到其他的可以访问的点了。
  31. visit[u] = true;
  32. for(int v = ; v < n; v++) {
  33. if(visit[v] == false && e[u][v] != inf) {
  34. if(dis[u] + e[u][v] < dis[v]) {
  35. dis[v] = dis[u] + e[u][v];
  36. num[v] = num[u];
  37. w[v] = w[u] + weight[v];//点权相加
  38. } else if(dis[u] + e[u][v] == dis[v]) {
  39. num[v] = num[v] + num[u];//如果相等,就把两者相加。
  40. if(w[u] + weight[v] > w[v])
  41. w[v] = w[u] + weight[v];
  42. }
  43. }
  44. }
  45. }
  46. printf("%d %d", num[c2], w[c2]);
  47. return ;
  48. }

//感觉这个代码更好理解一点,就是使用迪杰斯特拉的变形,也就是在更新边的同时加了几个判断条件,通常来说迪杰斯特拉是遍历一遍就会结束了,但是这个加了一个for循环,那么就每次都会选出一个最短的边(到c1,即原点),选出后才会被标记为已经访问过。

PAT 1003 Emergency[图论]的更多相关文章

  1. PAT 1003. Emergency (25)

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

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

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

  3. PAT 1003 Emergency

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

  4. PAT 1003 Emergency 最短路

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

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

  6. PAT 1003. Emergency 单源最短路

    思路:定义表示到达i的最短路径数量,表示到达i的最短径,表示最短路径到达i的最多人数,表示从i到j的距离, 表示i点的人数.每次从u去更新某个节点v的时候,考虑两种情况: 1.,说明到达v新的最短路径 ...

  7. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  8. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

  9. PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)

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

随机推荐

  1. 【大数据系列】windows环境下搭建hadoop开发环境使用api进行基本操作

    前言 搭建完hadoop集群之后在windows环境下搭建java项目进行测试 操作hdfs中的文件 版本一 package com.slp.hadoop274.hdfs; import java.i ...

  2. 关于ASP.NET中Request.QueryString的乱码问题(转)

    转自 http://www.cnblogs.com/chinhr/archive/2008/09/23/1296582.html 今天在使用Request.QueryString的时候,发现所有接收到 ...

  3. mac下升级terminal/终端的subversion版本方法

    雖然現在程式碼管理已經以 Git 為主了,不過偶爾需要維護一些舊案子還是會用 SVN,懶得轉了. Mac OS 本身有內建 SVN,不過卻是 1.6 版,最近修改一個舊案子就有碰到 project 已 ...

  4. 题目1003:A+B(按逗号分隔的A+B)

    题目链接:http://ac.jobdu.com/problem.php?pid=1003 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  5. linux中删除文件名称乱码

    在最近的操作中发现一些上传的文件有乱码,更改几次都无法正常转换到中文.下面给出正确的解决方案: 使用 ls -i 或者 ls -inum 查找出文件id号(红色字体) [root@localhost ...

  6. window上安装pymysql

    date: 2018-11-26   18:54:04 安装: cmd: pip install pymysql 验证: cmd: python >>import pymysql 不报错即 ...

  7. 23种设计模式之责任链模式(Chain of Responsibility)

    责任链模式是一种对象的行为型模式,避免请求发送者与接收者耦合在一起,让多个对象都有可能接收请求,将这些对象连接成一条链,并且沿着这条链传递请求,直到有对象处理它为止.责任链模式不保证每个请求都被接受, ...

  8. Android按钮事件的4种写法

    经过前两篇blog的铺垫,我们今天热身一下,做个简单的例子. 目录结构还是引用上篇blog的截图. 具体实现代码: public class MainActivity extends Activity ...

  9. 【转】C内存管理

    在任何程序设计环境及语言中,内存管理都十分重要.在目前的计算机系统或嵌入式系统中,内存资源仍然是有限的.因此在程序设计中,有效地管理内存资源是程序员首先考虑的问题. 第1节主要介绍内存管理基本概念,重 ...

  10. 剖析Elasticsearch集群系列之一:Elasticsearch的存储模型和读写操作

    转载:http://www.infoq.com/cn/articles/analysis-of-elasticsearch-cluster-part01 1.辨析Elasticsearch的索引与Lu ...