PAT 1003
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的更多相关文章
- PAT 1003我要通过!
PAT 1003 我要通过! 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于 PAT 的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出&qu ...
- PAT 1003 我要通过!(20)(代码+思路)
1003 我要通过!(20)(20 分)提问 "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下 ...
- 迪杰斯特拉算法——PAT 1003
本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...
- PAT 1003. 我要通过!(20) JAVA
参考http://blog.csdn.net/bin8632/article/details/50216297 答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答 ...
- PAT 1003. 我要通过!(20)
"答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确&quo ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- 浙大 pat 1003 题解
1003. Emergency (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003 我要通过!
https://pintia.cn/problem-sets/994805260223102976/problems/994805323154440192 “答案正确”是自动判题系统给出的最令人欢喜的 ...
随机推荐
- XCode修改工程名注意
以下文字转载过来,在使用的过程中遇到几个问题 1.需要在 Build phases 里面,检查下 Link Binary With Libraries 以及Compline Sources 2.Bul ...
- 让Apache支持ASP.NET
Apache是目前广泛使用的一种网络服务器程序,不仅在UNIX/LINUX平台上被大量使用,而且在Windows平台上也有许多站点放弃了IIS 而转向Apache..NET是微软推出的功能强大的开发技 ...
- Codeforces Round #363
http://codeforces.com/contest/699 ALaunch of Collider 题意:n个球,每个球向左或右,速度都为1米每秒,问第一次碰撞的时间,否则输出-1 贪心最短时 ...
- vim对erlang语法支持
发现vim写erlang代码语法缩进都不对,后来发现vim是7.0的,vim7.3开始才对erlang这块进行了支持,所以升级vim git上下载源码包,然后一系列配置安装 http://www.2c ...
- 【转】Maven实战(六)--- dependencies与dependencyManagement的区别
原博文出自于:http://blog.csdn.net/liutengteng130/article/details/46991829 感谢! 在上一个项目中遇到一些jar包冲突的问题,之后还有很 ...
- [cocos2d-x]File文件的IO读写处理
转载:http://blog.csdn.net/chiuan/article/details/8618411 为了保存自定义数据文件,需要保存文件和读取文件,也就是File的IO处理: 针对cocos ...
- Java IO (3) - Reader
Java IO (3) - Reader 前言 JavaIO一共包括两种,一种是stream,一种是reader/writer,每种又包括in/out,所以一共是四种包.Java 流在处理上分为字符流 ...
- MongoDB Windows 下安装部署
下面主要是我在Windows上(Win7)安装.运行.安装Windows服务的笔记,以作备忘. 1.下载 下载地址:http://www.mongodb.org/downloads 从其下载页面就可以 ...
- mysql删除重复记录语句,删除除了 id 号不同,其他都相同的学生冗余信息
/** 在Mysql下执行: delete from my.stu where id not in( select min(id) id from my.stu group by code) ; 用途 ...
- C++多态实现(虚函数,成员函数覆盖、隐藏)
// 1.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> using namespace ...