1003. Emergency (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

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 

最短路径搜索,比较麻烦的是,必须统计所有最短路径的数量,所以在找到相同cost路径时必须把路径的数量相加,体现在代码的第56~60之间。

代码

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <limits.h>
 4 
 5 int dijkstra(int,int,int&,int&);
 6 int N,M,c1,c2;
 7 int num_rescue[];
 8 int map[][];
 9 int cost[];
 int num_path[];
 int num_res_gather[];
 int flag[];
 int main()
 {
     int i;
     int s,e,c;    
     while (scanf("%d%d%d%d",&N,&M,&c1,&c2) != EOF){
         memset(num_rescue,,sizeof(num_rescue));
         memset(map,,sizeof(map));
         for(i=;i<N;++i)
             cost[i] = INT_MAX;
         memset(num_path,,sizeof(num_path));
         memset(num_res_gather,,sizeof(num_res_gather));
         memset(flag,,sizeof(flag));
         for (i=;i<N;++i){
             scanf("%d",&num_rescue[i]);
         }
         for (i=;i<M;++i){
             scanf("%d%d%d",&s,&e,&c);
             map[s][e] = c;
             map[e][s] = c;
         }
         cost[c1] = ;
         num_path[c1] = ;
         num_res_gather[c1] = num_rescue[c1];
         flag[c1] = ;
         int path,rescue;
         dijkstra(c1,c2,path,rescue);
         printf("%d %d\n",path,rescue);
     }
     return ;
 }
 
 int dijkstra(int start,int end,int& path,int& rescue)
 {
     int s = start;
     while(!flag[end]){
         int i;
         for (i=;i<N;++i){
             if(!flag[i] && map[s][i]){
                 if (cost[s] + map[s][i] < cost[i]){
                     cost[i] = cost[s] + map[s][i];
                     num_path[i] = num_path[s];
                     num_res_gather[i] = num_res_gather[s] + num_rescue[i];
                 }
                 else if (!flag[i] && cost[s] + map[s][i] == cost[i]){
                     num_path[i] = num_path[s] + num_path[i];
                     num_res_gather[i] = ( num_res_gather[i] > num_res_gather[s] + num_rescue[i]? 
                         num_res_gather[i] : num_res_gather[s]+num_rescue[i]);
                 }
             }
         }
         i = ;
         while(flag[i++]);
         int min_cost = cost[--i];
         s = i;
         for (++i;i<N;++i){
             if (!flag[i] && cost[i] < min_cost){
                 min_cost = cost[i];
                 s = i;
             }
         }
         flag[s] = ;
     }
     path = num_path[end];
     rescue = num_res_gather[end];
     return cost[end];
 }

PAT 1003的更多相关文章

  1. PAT 1003我要通过!

    PAT 1003 我要通过! 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出&qu ...

  2. PAT 1003 我要通过!(20)(代码+思路)

    1003 我要通过!(20)(20 分)提问 "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下 ...

  3. 迪杰斯特拉算法——PAT 1003

    本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...

  4. PAT 1003. 我要通过!(20) JAVA

    参考http://blog.csdn.net/bin8632/article/details/50216297 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答 ...

  5. PAT 1003. 我要通过!(20)

    "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确&quo ...

  6. PAT 1003. Emergency (25)

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

  7. 浙大 pat 1003 题解

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

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

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

  9. PAT 1003 我要通过!

    https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192 “答案正确”是自动判题系统给出的最令人欢喜的 ...

随机推荐

  1. Selenium2Library系列 keywords 之 _SelectElementKeywords 之 _get_values_for_options(self, options)

    def _get_values_for_options(self, options): values = [] for option in options: values.append(option. ...

  2. 写给Python初学者的设计模式入门

    有没有想过设计模式到底是什么?通过本文可以看到设计模式为什么这么重要,通过几个Python的示例展示为什么需要设计模式,以及如何使用. 设计模式是什么? 设计模式是经过总结.优化的,对我们经常会碰到的 ...

  3. json字符串转换为JSONObject和JSONArray

    一.下载json 具体到http://www.json.org/上找java-json下载,并把其放到项目源代码中,这样就可以引用其类对象了 二.具体转化过程 //JSONObject String ...

  4. RabbitMQ C# 例子 -摘自网络

    //刚刚接触,如有不对还望不吝指正 public static void StartUp() { #region 前期准备工作 ConnectionFactory factory = new Conn ...

  5. Spark SQL概念学习系列之如何使用 Spark SQL(六)

    val sqlContext = new org.apache.spark.sql.SQLContext(sc) // 在这里引入 sqlContext 下所有的方法就可以直接用 sql 方法进行查询 ...

  6. HDU 3577 Fast Arrangement (线段树区间更新)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3577 题意不好理解,给你数字k表示这里车最多同时坐k个人,然后有q个询问,每个询问是每个人的上车和下车 ...

  7. POJ 1679 The Unique MST (次小生成树)

    题目链接:http://poj.org/problem?id=1679 有t组数据,给你n个点,m条边,求是否存在相同权值的最小生成树(次小生成树的权值大小等于最小生成树). 先求出最小生成树的大小, ...

  8. 【BJG吐槽汇】第一期 - 警惕亚马逊莫名自动扣款!千万不要进了它的坑!

    BJG吐槽汇:一直以来我都觉得其实生活中工作中会有各种各样奇葩的事或者奇葩的人可以去吐槽,那么BeJavaGod本身聊得就是关于JavaWeb技术,互联网技术,互联网产品等,那么今天起咱们开了这么一档 ...

  9. 关于Linux vi命令 vi命令一览表

    vi是所有UNIX系统都会提供的屏幕编辑器,它提供了一个视窗设备,通过它可以编辑文件.当然,对UNIX系统略有所知的人,或多或少都觉得vi超级难用,但vi是最基本的编辑器,学好了vi,以后在UNIX世 ...

  10. python中List操作

    传送门 官方文件地址 list.append(x): 将x加入列表尾部,等价于a[len(a):] = [x] 例: >>> list1=[1,2,3,4] >>> ...