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的更多相关文章

  1. PAT1003——我要通过!

    “答案正确”是自动判题系统给出的最令人欢喜的回复.本题属于PAT的“答案正确”大派送 —— 只要读入的字符串满足下列条件,系统就输出“答案正确”,否则输出“答案错误”. 得到“答案正确”的条件是: 1 ...

  2. PAT1003:Emergency

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

  3. pat1003 迪杰斯特拉法和dfs求最短路

    本题的背景是求定点和定点之间的最短路问题(所有的最短路 不是一个解  是全部解,方法手段来自数据结构课程中的迪杰斯特拉算法和dfs(深度优先遍历). 分别用两种方法编程如下代码 dfs #includ ...

  4. PAT-1003 Emergency(Dijkstra)

    1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...

  5. pat1003. Emergency (25)

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

  6. 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 ...

  7. PAT1030 Travel Plan (30)---DFS

    (一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...

  8. PAT1018 (dijkstra+dfs)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  9. PAT甲级1003. Emergency

    PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...

随机推荐

  1. ural 1353. Milliard Vasya's Function(背包/递归深搜)

    1353. Milliard Vasya's Function Time limit: 1.0 second Memory limit: 64 MB Vasya is the beginning ma ...

  2. php回传ajax数据

    <?php $json_width = $_POST["img_width"]; $json_height = $_POST["img_height"]; ...

  3. 正则表达式 Pattern & Matcher

    1 compile and pattern Pattern类用于创建一个正则表达式,也可以说创建一个匹配模式,它的构造方法是私有的,不可以直接创建,但可以通过Pattern.complie(Strin ...

  4. 单击Echart饼图实现数据钻取

    使用场景和需求: 第一层饼图显示党员党龄分布的情况. 点击某个党龄段,查看拥有该党龄段的党支部. 默认显示3个党支部,余下的党支部合并为其它,点击其它,查看余下的党支部. 点击某个党支部,返回第一层饼 ...

  5. 8. Python自定义模块humansize

    我们在提取一个文件元信息的时候,经常会使用到获取元信息的size, 但是默认提取出来的是字节为单位计算的大小,我们需要转换成MB或者GB 或者TB的大小. 因此就需要使用到humansize这个模块, ...

  6. OpenCV:二值图像连通区域分析与标记算法实现

    http://blog.csdn.net/cooelf/article/details/26581539?utm_source=tuicool&utm_medium=referral Open ...

  7. app调用支付宝支付 笔记

    1.提交各种申请 2.通过后进入支付宝开放平台  --> 管理中心 -->创建应用  --> 填写相关信息 提交等待审核通过(1,2天)   3.下载集成包(https://doc. ...

  8. PHP:var_dump展示不全

    问题:var_dump展示不全,出现省略号 我们使用php的var_dump函数,可以更直观的查看输出结果,包括类型,数量等详细信息.此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值.数 ...

  9. PHP图形处理函数试题

    一.问答题 1.取得当前安装的 GD 库的信息的函数是? 2.取得图像大小的函数是? 3.为一幅图像分配颜色 + alpha的函数是? 4.新建一个基于调色板的图像的函数是? 5.新建一个黑色图像的函 ...

  10. MySQL外键的作用和创建

    MySQL外键的作用: 保持数据一致性,完整性,主要目的是控制存储在外键表中的数据.使两张表形成关联,外键只能引用外表中列的值! 我们来建两个表 CREATE TABLE `example1` ( ` ...