PAT1003
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),
第一行包含四个正整数,N表示城市的数量
M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively.
M表示路的数量,C1和C2分别表示你当时在的城市和你需要救援的城市。
The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city.
下一行包含N个整数,第I个整数表示在第I个城市救援队的数量。
Then M lines follow, each describes a road with three integers c1, c2 and L,
下面接着M行,每一行用三个整数c1、c2、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.
C1和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.
不同的C1和C2的最短路的条数,和能集合的最大的救援队的数量。
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
一看到是最短路径的题目就眼前一亮,脑子里面应该马上浮现出那几种方法。
首先一上来就是最简单的floyd,但是一看数量级500,N^3就不得了了,然后求的也不是任意两点都要求距离,所以不适用。
那么简单一点就是用dijkstar。
然后就死了。。。。。为什么呢?因为做过太多的题目,都是让你求两点间的最短路径,但是这道题目偏偏不是,它是让你求出最短路径的条数,而不是最短路径的长度。
所以一上来就死了,我一直求出的第一个结果都是dis【】,然后给出的测试用例又是正确的,真的是,哎。
那么就有人会问了,那么让你求出最短路径的条数,怎么求呢?还有救援队的最大值呢?
这里我就要多说一下了,这道题目需要比较深刻的理解dijkstar的算法,然后合理的运用它的思想去解决这两个问题。
dijkstar在计算最短路径的时候,利用一个数组存放源点到任意点的距离。然后利用寻找到的当前的确定值,去松弛每一个估计值。
我们可以利用这一思想去解决我们的问题。下面的代码中,我使用teamDis2数组保存最短路径的数目当if(dis[i] > dis[index] + maps[index][i])出现的时候,证明可以松弛了,就代表需要更新路径,那么当前路径的最小值就是目标路径的最小值。当else if(dis[i] == dis[index] + maps[index][i])出现的时候,证明这两条路径均可以到达目标点,且距离相同,则最小路径为两者之和。
我使用teamDis1数组保存最大的救援队数目
当if(dis[i] > dis[index] + maps[index][i])出现的时候,证明可以松弛了,就代表需要更新路径,那么救援队的数目需要像dis距离一样变动。当else if(dis[i] == dis[index] + maps[index][i])出现的时候,证明这两条路径均可以到达目标点,且距离相同,那么救援队的数目需要取两条路径的最大值。
这两个子问题都是依靠dijkstar的思想解决的,需要仔细的思考一下。慢慢体会一下。
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<string.h> using namespace std; /*
dijkstra
*/
int maps[][];//用于记录路径
int team[];
int dis[];
int teamDis1[];
int teamDis2[];
int book[];
const int MAX = 0x7ffffff; int main()
{
int i,j,q,c1,c2;
int city,roads,starting,ending;
int index,indexNumber;//离当前点最近的点的坐标和坐标
cin>>city>>roads>>starting>>ending; //初始化dis数组
for (i = ; i < city; i++)
dis[i] = MAX; //初始化maps数组
for (i = ; i < city; i++)
for (j = ; j < city; j++)
if(i != j)
maps[i][j] = MAX;
else
maps[i][j] = ; for (i = ; i < city; i++)
{
cin>>team[i];
}
for (i = ; i < roads; i++)
{
cin>>c1>>c2;
cin>>maps[c1][c2];
maps[c2][c1] = maps[c1][c2];; if(c1 == starting)
{
dis[c2] = maps[c1][c2];
}
else if(c2 == starting)
{
dis[c1] = maps[c1][c2];
}
} dis[starting] = ;
teamDis2[starting] = ;
teamDis1[starting] = team[starting];
for (q = ; q < city; q++)
{
index=;
indexNumber = MAX;
for (i = ; i < city; i++)
{
if(book[i] == && dis[i] < indexNumber)
{
indexNumber = dis[i];
index = i;
}
} book[index] = ;
for (i = ; i < city; i++)
{
if(book[i] == && maps[index][i] != MAX)
{
if(dis[i] > dis[index] + maps[index][i])
{
dis[i] = dis[index] + maps[index][i];
teamDis1[i] = teamDis1[index] + team[i];
teamDis2[i] = teamDis2[index];
}
else if(dis[i] == dis[index] + maps[index][i])
{
teamDis2[i] += teamDis2[index];
if(teamDis1[i] < teamDis1[index] + team[i])
teamDis1[i] = teamDis1[index] + team[i];
}
}
}
if(index == ending)
break;
} cout<<teamDis2[ending]<<" "<<teamDis1[ending]<<endl;
return ;
}
PAT1003的更多相关文章
- PAT1003——我要通过!
“答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...
- PAT1003:Emergency
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- pat1003 迪杰斯特拉法和dfs求最短路
本题的背景是求定点和定点之间的最短路问题(所有的最短路 不是一个解 是全部解,方法手段来自数据结构课程中的迪杰斯特拉算法和dfs(深度优先遍历). 分别用两种方法编程如下代码 dfs #includ ...
- PAT-1003 Emergency(Dijkstra)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- pat1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT-1003 Emergency (25 分) 最短路最大点权+求相同cost最短路的数量
As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...
- PAT1030 Travel Plan (30)---DFS
(一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...
- PAT1018 (dijkstra+dfs)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
随机推荐
- c/c++ 浮点型处理
#include <stdio.h> #include <iostream> #include <string> #include <string.h> ...
- MediaWiki搭建教程
♦ MediaWiki是什么以及有什么作用,这里我就不再阐述了,网上可以查到很多.这里只是简单记录一下搭建wiki的基本过程,给一些热爱捯饬的小伙伴一些参考. ♦ 其实wiki的搭建本身很简单,最 ...
- Hibernate框架--配置,映射,主键
SSH框架: Struts框架, 基于mvc模式的应用层框架技术! Hibernate, 基于持久层的框架(数据访问层使用)! Spring, 创建对象处理对象的依赖关系以及框架整合! Da ...
- CSS 各类 块级元素 行级元素 水平 垂直 居中问题
元素的居中问题是每个初学者碰到的第一个大问题,在此我总结了下各种块级 行级 水平 垂直 的居中方法,并尽量给出代码实例. 首先请先明白块级元素和行级元素的区别 行级元素 一块级元素 1 水平居中: ( ...
- lua: Learning Official Doc notes
dynamically typed vars: basic types: nil, boolean, number, string, function, userdata, thread & ...
- sqlDeveloper连接oracle
1.解决oracle11g的ORA-12505问题 启动oraclehome92TNSlistener服务,启动oracleserviceXXXX,XXXX就是你的database SID. < ...
- Windows 8/7下还原系统默认扩展名打开方式类型
在百度知道上如果你搜“改回选错的打开方式”,看到的大多数都是XP系统的方法,不管是批处理还是别的方法,但适用于Windows 8/7的只有修改注册表的方法. 因为Windows 7你也就根本找不到[工 ...
- 更改web project 访问项目名称
1.新建web project 2.右键该项目名称------properties 3.访问该项目的URL http://localhost:8806/ssm/.......... 相比书写整个项目名 ...
- hdu_3067_小t的游戏(脑洞)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3067 题意:中文,不解释 题解:一看就知道是要找规律的题,都是有循环节的,看代码. #include ...
- CentOS中由一般用户切换为root用户
--->http://www.centoscn.com/CentOS/help/2014/0624/3173.html 1.打开终端,提示符为“$”,表明该用户为普通用户,此时,直接输su,回车 ...