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 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
随机推荐
- a标签中使用img后的高度多了几个像素
a元素下有一个匿名文本,这个文本外有一个匿名行级盒子,它有的默认vertical-align是baseline的,而且往往因为上文line-height的影响,使它有个line-height,从而使其 ...
- 自定义alert和confirm
var common = {}; common.showAlert = function (msg) { var html = "<div id='dialog_alert' clas ...
- C语言的常用字符串操作函数(一)
一直做的是单片机相关的程序设计,所以程序设计上更偏向底层,对于字符串的操作也仅限于液晶屏幕上的显示等工作,想提高下字符串操作的水平,而不是笨拙的数组替换等方式,翻看帖子发现C语言的字符串操作函数竟然这 ...
- linux下写脚本时-gt是什么意思
-eq 等于-ne 不等于-gt 大于-ge 大于等于-lt 小于-le 小于等于
- 十二、oracle 数据库(表)的逻辑备份与恢复
一.介绍逻辑备份是指使用工具export将数据对象的结构和数据导出到文件的过程.逻辑恢复是指当数据库对象被误操作而损坏后使用工具import利用备份的文件把数据对象导入到数据库的过程.物理备份即可在数 ...
- Python 查找binlog文件
经常需要在 binlog 中查找一些日志信息,于是写了一个简单的脚本.对于非常巨大的 binlog 文件,该脚本可能会速度慢,毕竟还是用的 list,暂时没想到好办法. 详细看代码: #/usr/bi ...
- android 实践项目
电子词典 http://files.cnblogs.com/blogLYF/lyf_danci.apk
- Ubuntu编译安装PHP7
参数配置 ./configure --prefix=/usr/local/php7 \ --with-config-file-path=/usr/local/php7/etc \ --with-apx ...
- Linux - CentOS6.5服务器搭建与初始化配置详解(下)
传送带:Linux - CentOS6.5服务器搭建与初始化配置详解(上) 继续接着上面的安装,安装完后会出现下面界面 点击reboot重启 重启后可以看到下面的tty终端界面 因为这就是最小化安装 ...
- 开启真机的View Server引入HierarchyViewer/By写monkeyrunner自动化测试脚本
其实相关文章网上也有不少了,不过在真机上开启View Server的中文文章好像只有一篇,前段时间按照这篇文章的内容,并结合英文源文去hack我的Nexus S(4.1.2)也走了一点弯路.现在总结一 ...