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

1003.突发事件(25)

作为一个城市紧急援救队的指挥者,你得到了一个国家的特殊地图。地图上分散着几座城市,城市间用道路连接着。每个城市援救队的数量以及两座城市之间每条道路的长度已经在地图上标出。当某些城市发生了突发事件,需要你的帮助时,你的工作是带领你的队伍尽快的赶到事发现场,与此同时,召集尽可能多的在路上的队伍。

输入

每个输入文件包含一个测试实例。每个实例的第一行有四个正整数:N(<= 500)是城市的个数(城市的编号从0到N-1),M是道路的个数,C1和C2分别是你现在所在的城市以及你必须去救援的城市。下一行有N个整数,第i个整数是第i个城市中救援队的数量。然后下面有M行,每行表示一条道路。每一行有三个整数c1,c2和L,分别表示道路连接的两个城市以及道路的长度。保证C1到C2之间存在至少一条路径。

输出

对于每个测试实例,在一行中输出两个数字:C1和C2之间不同的最短路径的个数,你能聚集起来的最多的救援队数量。

一行中的所有数字必须被一个空格分隔开,在每行的结尾不允许出现空格。

题解:

本题我用了Dijstra,你可以什么都别管先打个Dijstra模版。

然后考虑最短路条数:

  一开始,和C1相连的所有点的最短路数初始化为1,包括C1(可能自己到自己),其它初始化为0,。

  当d[k]+e[k][j]<d[j](通过k可以对C1到 j 可以更新)时,ti[j]=ti[k],(ti[p]表示C1到p的最短路条数),因为既然更新,肯定直接可以把原来的ti【j】覆盖了

  当 d[k]+e[k][j]==d[j],ti[j]+=ti[k];条数就可以增加了,但不是+1,是加上ti[k]。

然后考虑人数最多:

  一开始,我只初试化了C1:rm[c1]=r[c1];C1的人数最多的就是为r[c1]。

  之后Dijstra每挑选出一个k都检查一下:rm[k]=max(rm[k],r[k]+r[s]);

  当d[k]+e[k][j]<d[j],rm[j]=rm[k]+r[j];

  当d[k]+e[k][j]==d[j],rm[j]=max(rm[j],rm[k]+r[j]);

与第二组测试数据类似,我被卡的样例:

3 2 1 1
0 1 2
0 1 1
1 2 1

应该为1 1

#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<cmath>
#include<queue>
#define inf 0x3f3f3f3f
using namespace std;
int e[][];
int ti[];
int r[];
int rm[];
int v[];
int d[];
int n,m;
int c1,c2;
int ss=;
void dijstra(int s)
{
for(int i=;i<=n;i++) d[i]=e[s][i];
d[s]=;
v[s]=;
while()
{
int mm=inf;
int k=-;
for(int i=;i<=n;i++)
{
if(!v[i]&&mm>d[i])
{
k=i;
mm=d[i];
}
}
if(k==-) break;
v[k]=;
rm[k]=max(rm[k],r[k]+r[s]);
for(int j=;j<=n;j++)
{
if(d[k]+e[k][j]<d[j])
{
d[j]=d[k]+e[k][j];
ti[j]=ti[k];
rm[j]=rm[k]+r[j];
}
else if(d[k]+e[k][j]==d[j])
{
ti[j]+=ti[k];
rm[j]=max(rm[j],rm[k]+r[j]);
}
}
}
}
int main()
{
while(cin>>n>>m>>c1>>c2)
{
c1++;
c2++;
memset(e,inf,sizeof(e));
memset(ti,,sizeof(ti));
memset(v,,sizeof(v));
memset(rm,,sizeof(rm));
ss=;
for(int i=;i<=n;i++)
{
cin>>r[i];
}
for(int i=;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
x++;
y++;
if(e[x][y]&&x==c1||y==c1)
{
if(x==c1)
ti[y]++;
else ti[x]++;
} if(e[x][y]>z)
e[x][y]=e[y][x]=z;
}
rm[c1]=r[c1];
ti[c1]=;
dijstra(c1);
cout<<ti[c2]<<" "<<rm[c2]<<endl;
}
return ;
}

PAT 甲级1003 Emergency (25)(25 分)(Dikjstra,也可以自己到自己!)的更多相关文章

  1. PAT甲级1003. Emergency

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

  2. 图论 - PAT甲级 1003 Emergency C++

    PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...

  3. PAT 甲级 1003. Emergency (25)

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

  4. PAT 甲级 1003 Emergency

    https://pintia.cn/problem-sets/994805342720868352/problems/994805523835109376 As an emergency rescue ...

  5. 1003 Emergency (25 分)

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

  6. 【PAT甲级】1070 Mooncake (25 分)(贪心水中水)

    题意: 输入两个正整数N和M(存疑M是否为整数,N<=1000,M<=500)表示月饼的种数和市场对于月饼的最大需求,接着输入N个正整数表示某种月饼的库存,再输入N个正数表示某种月饼库存全 ...

  7. PAT Advanced 1003 Emergency 详解

    题目与翻译 1003 Emergency 紧急情况 (25分) As an emergency rescue team leader of a city, you are given a specia ...

  8. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  9. PAT 甲级 1146 Topological Order (25 分)(拓扑较简单,保存入度数和出度的节点即可)

    1146 Topological Order (25 分)   This is a problem given in the Graduate Entrance Exam in 2018: Which ...

随机推荐

  1. Runtime获取类的属性列表和方法列表

    Runtime获取类的属性列表和方法列表 Runtime很强大,他使得OC中没有真正意义上的私有属性和私有方法,我们可以利用OC的运行时拿到一个类的任何方法和任何属性,然后动态的去调用方法,objc_ ...

  2. Python中列表生成式和字典生成式练习

    (一)列表生成式 练习一:编写名为collatz(number)的函数:实现的功能:参数为偶数时,打印number// 2;参数为奇数时,打印3*number + 1 解析: number = int ...

  3. Linux之FTP服务

     一.ftp服务 ftp是一个文件传输协议(File Transfer Protocal).lftp相当于一个浏览器,用来向服务器发送请求的. 进行FTP服务的相关操作的时候,要先修改 vim /et ...

  4. java入门学习(5)—面向对象注意点总结

    1.一个类里面最多有5种成份(属性,方法,构造器,还有两种还没有涉及). 2.定义方法时又返回值的保证最起码有一个有效的return语句,最起码让其在编译的时候就识别到,而不是经过判断识别,如通过if ...

  5. runtime查找 UIAlertAction 的key 及 UIActionSheet 设置字体颜色

    修改不了颜色了 结果发现kvo 的key 不对 哎 直接上代码 设置正确的属性找到对应的key  还以为iOS 11改变了方法 unsigned int count; Ivar *ivars =  c ...

  6. Python源码分析(一)

    最近想学习下Python的源码,希望写个系列博客,记录的同时督促自己学习. Python源码目录 从Python.org中下载源代码压缩包并解压,我下载的是Python2.7.12,解压后: 对于主要 ...

  7. 【剑指offer】栈的压入弹出序列,C++实现(举例)

    原创文章,转载请注明出处! 本题牛客网地址 博客文章索引地址 博客文章中代码的github地址 1.题目 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为第一个序列的出栈序列.注意 ...

  8. 开源中文检索引擎Coreseek简单使用

    Coreseek结合MySQL使用简单示例,如下所示: echo 北京 | iconv -f gbk -t utf-8 | search -c D:\web\coreseek\etc\csft_mys ...

  9. 【测试技术】websocket-client

    Websocket Client 继续上周的议题.因为我不会写go,不会写websocket客户端,导致整个测试过程我是个完美的酱油党.上周我终于把客户端服务写好了. 选择Websokcet框架 现在 ...

  10. java 邮件发送工具类【来源网络自己已经实际应用】

    最近在做一个Java发送邮件的工具类,现在分享一下完整的代码 首先需要java邮件的包javax.mail-1.5.4.jar 之前因为链接给错了,很不好意思,现在重新发一次. 包在这里可以下载htt ...