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

题目描述:

抽象出来就是给你一张图,给你起点终点,着到起点到终点的所有最短路径,每个点上有一个数,代表这个点上的搜救队的数量,要求输出这些最短路径中你能召集到的最大的搜救队的数量。

算法分析:

思路1、dijsktra

简单题,最短路径算法(e.g. Dijastra),但是要稍微变化一下,要记录所有的的最短路径,所以从终点回溯上去可能有多于一个的parent。那么就在每个node上面挂一个 list好了,记录所有的parents. 得到后可以从终点DFS到起点,记录下每条最短路径上的搜救队的数量和。保存最大的那个输出即可。

思路2、DFS

注意点:

这个题目其实就是最短路径的小小变形,注意最原始的最短路径算法通过记录每个node最短路径上的parent来保存一条这样的最短路径,这里话就 是相当于,要多点判断,当新的路径和当前路径相同长度时,也要记录下来,但是后面如果找到更短的,那么之前的路径就要清空,记录路径的代码稍微注意一点就 没问题了。

Dijkstra

#include <iostream>
#include <cstring>
#include <cstdio> using namespace std; #define INF 0x3f3f3f3f
#define MX 501 int mp[MX][MX];
int v[MX];
int dist[MX];
int amount[MX];
int teams[MX];
int pathcount[MX];
int N,M,start,en; void dijkstra(int s){
amount[s] = teams[s];
dist[s] = ;
pathcount[s] = ; while (){
int u, dmin=INF;
for (int i=; i<N; i++){
if (v[i]== && dist[i]<dmin){
dmin = dist[i];
u = i;
}
}
if (dmin==INF || u==en) break;
v[u] = ;
for (int i=; i<N; i++){
if(v[i]==){
if (dist[i] > dist[u] + mp[u][i]){
dist[i] = dist[u] + mp[u][i];
amount[i] = amount[u] + teams[i];
pathcount[i] = pathcount[u];
}else if (dist[i] == dist[u] + mp[u][i]){
pathcount[i] += pathcount[u];
if (amount[i] < amount[u] + teams[i])
amount[i] = amount[u] + teams[i];
}
}
}
}
} int main()
{
scanf("%d%d%d%d", &N,&M,&start,&en);
for (int i=; i<N; i++)
{
scanf("%d", &teams[i]);
}
for (int i=; i<N; i++)
{
dist[i] = INF;
for (int j=; j<N; j++)
mp[i][j] = INF;
}
for (int i=; i<M; i++)
{
int c1, c2, L;
scanf("%d%d%d", &c1,&c2,&L);
mp[c1][c2] = mp[c2][c1] = L;
} dijkstra(start);
printf("%d %d", pathcount[en], amount[en]); return ;
}

DFS

#include<cstdio>
#include<cstring> #define INF 0x7FFFFF
int u[]={};
int teams[]={};
int dist[];
int mp[][];
int n,m,st,en;
int shortNum=,maxteam=,mindist=INF; void dfs(int s,int dis,int team){//到达S结点时的距离,teams
if(s==en){
if(dis<mindist){
mindist=dis;
shortNum=;
maxteam=team;
}else if(dis==mindist){
shortNum++;
if(team>maxteam) maxteam=team;
}
return;
}
u[s]=;
for(int i=;i<n;i++){
if(u[i]== && mp[s][i]>){
dfs(i,dis+mp[s][i],team+teams[i]);
}
}
u[s]=;
} int main(){
freopen("in.txt","r",stdin);
int i;
scanf("%d%d%d%d",&n,&m,&st,&en);
for(i=;i<n;i++) scanf("%d",&teams[i]);
memset(mp,-,sizeof(mp));
for(i=;i<m;i++){
int t1,t2,dis;
scanf("%d%d%d",&t1,&t2,&dis);
mp[t1][t2]=mp[t2][t1]=dis;
} //u[st]=1;
dfs(st,,teams[st]); printf("%d %d\n",shortNum,maxteam);
return ;
}

PAT 解题报告 1003. Emergency (25)的更多相关文章

  1. PAT 解题报告 1010. Radix (25)

    1010. Radix (25) Given a pair of positive integers, for example, 6 and 110, can this equation 6 = 11 ...

  2. PAT (Advanced level) 1003. Emergency (25) Dijkstra

    As an emergency rescue team leader of a city, you are given a special map of your country. The map s ...

  3. PAT (Advanced Level) 1003. Emergency (25)

    最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostrea ...

  4. 【PAT甲级】1003 Emergency (25 分)(SPFA,DFS)

    题意:n个点,m条双向边,每条边给出通过用时,每个点给出点上的人数,给出起点终点,求不同的最短路的数量以及最短路上最多能通过多少人.(N<=500) AAAAAccepted code: #in ...

  5. PAT 1003. Emergency (25)

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

  6. PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组

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

  7. PAT 甲级 1003. Emergency (25)

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

  8. 1003 Emergency (25)(25 point(s))

    problem 1003 Emergency (25)(25 point(s)) As an emergency rescue team leader of a city, you are given ...

  9. 1003 Emergency (25分) 求最短路径的数量

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

随机推荐

  1. 关于在TP的各类标签中的注意事项

    name的位置上的变量是不用加$的 value位置上的变量是要加$的 <eq name="volist.id" value="$Think.post.id" ...

  2. What is Heterogeneous Computing?

    http://developer.amd.com/resources/heterogeneous-computing/what-is-heterogeneous-computing/ Heteroge ...

  3. 汇总10.4版本ArcGIS Server与ArcMap 安装+SDE+注册数据源(非破解)

    文档参考了Server技术支持部各位前辈的总结文档. win10 + Server 10.4 +  ArcMap 10.4 + Oracle instant client (32位 和 64位) 安装 ...

  4. OpenGL完全教程 第一章 初始化OpenGL

    第一章 初始化OpenGL 无论是什么东西,要使用它,就必须对它进行初始化.如果你之前使用过GDI,你应该也多多少少了解到GDI在绘制图形之前要为之创建渲染环境.OpenGL也一样.本章给出的代码,大 ...

  5. php://input,$_POST,$HTTP_RAW_POST_DATA区别

    我们先来看两个demo 例子:php://input 代码如下   post.php 代码如下   例子:$_post 代码如下   welcome.php 代码如下   再来看$GLOBALS [& ...

  6. Java 判断图片资源的存在否

    question: 如题,举个例子吧 String image ="http://info-database.csdn.net/Upload/2010-10-30/735-60sap1030 ...

  7. Bluetooth GAP介绍

    目录 1 GAP协议栈 2 Profile Role 3 用户接口 4 模式 5 安全 5.1 认证(Authentication) 5.2 安全模式 6 Idle Mode Procedures 7 ...

  8. Google 开源项目风格指南

    Python风格规范 分号 Tip 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 Tip 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Py ...

  9. css中textarea去掉边框和选中后的蓝色边框问题的解决方法

    我们在设计网页的输入框时,有时会遇到需要把textarea的边框去掉的问题,经过测试,下面的代码是可以的. textarea{ border: solid 0px; outline:none; }

  10. MySQL- 锁(2)

    InnoDB行锁实现方式 InnoDB行锁是通过给索引上的索引项加锁来实现的,这一点MySQL与Oracle不同,后者是通过在数据块中对相应数据行加锁来实现的.InnoDB这种行锁实现特点意味着:只有 ...