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. MVC和WebForm的优缺点对比

    1 WebForm优点 1)支持事件模型开发,得益于丰富的服务端组件,WebForm开发可以迅速的搭建Web应用 2)使用方便,入门容易 3)控件丰富的WebForm 2 WebForm缺点  1)封 ...

  2. 《Genesis-3D开源游戏引擎--横版格斗游戏制作教程:简介及目录》(附上完整工程文件)

    介绍:讲述如何使用Genesis-3D来制作一个横版格斗游戏,涉及如何制作连招系统,如何使用包围盒实现碰撞检测,软键盘的制作,场景切换,技能读表,简单怪物AI等等,并为您提供这个框架的全套资源,源码以 ...

  3. 内核源码分析之linux内核栈(基于3.16-rc4)

    在3.16-rc4内核源码中,内核给每个进程分配的内核栈大小为8KB.这个内核栈被称为异常栈,在进程的内核空间运行时或者执行异常处理程序时,使用的都是异常栈,看下异常栈的代码(include/linu ...

  4. JAVA使用jdbc连接MYSQL简单示例

    以下展示的为JAVA使用jdbc连接MYSQL简单示例: import java.sql.DriverManager; import java.sql.ResultSet; import java.s ...

  5. Type datetime2 is not a defined system type - Entity Framework 摘自网络

    "Type datetime2 is not a defined system type" Solution: 把edmx 改为 ProviderManifestToken=&qu ...

  6. wine的中文字体显示

    从1.1.4开始wine的界面就已经支持中文了,但是对于软件中的中文支持并不太好,主要原因.还是字体...Let's go 首先,copy一下字体:把simsun.ttc (即宋体)复制到 ~/.wi ...

  7. ffmpeg内置aac编码器正式发布

    https://www.ffmpeg.org/#aac_encoder_stable February 15th, 2016, FFmpeg 3.0 "Einstein" FFmp ...

  8. linux内核地址mapping

    linux内核采用页式存储管理,虚拟地址空间划分成固定大小的页面,由MMU(memory manager unit)在运行时将virtual address mapping to (或者说是变化成)某 ...

  9. 【转】Nginx系列(二)--模块化

    原博文出于: http://blog.csdn.net/liutengteng130/article/details/46700977  感谢! 高度模块化的设计设Nginx架构的基础.在Nginx中 ...

  10. delphi回调函数

    文章来源: http://anony3721.blog.163.com/blog/static/5119742010866050589/ 一.主单元 unit UnMain; interface us ...