PTA 1003 Emergency
问题描述
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 Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - 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 Specification:
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
思路:Floyd+DFS 遇到的问题:
1.一开始题看错了,问路径数量想当然地以为是问最短路径长度,又撞上了如此巧的Sample,惨
2.Floyd翻了个车,里面的if语句条件写的有点问题,碰巧又错得很艺术,简单的样例都能过,大囧 总结:太久不摸键盘了,手生了,以后还是得多多练习
代码:
#include <iostream>
using namespace std; int n,m,c1,c2,dist[][],res[],map[][],len=,sup,ressum,resmax=,count=;
// n城市数,m路径数,c1、c2起止城市,dist存距离,res存救援队数量,map存路径长度
// len存DFS路径长度,sup存c1到c2距离,ressum存DFS过程中救援队数量,resmax存最大救援队数量,count存路径数
bool proc[]; //存储DFS过程中城市经过情况,防止重复经过某一城市 void dfs(int x){
if (len>sup) return; // 走过距离超过c1、c2距离直接返回
if (x==c2){ //到达c2
if (len==sup){ //恰好是最短的路径
count++; //路径数+1
if (ressum>resmax) resmax=ressum; //判断此时救援队数量
}
return;
}
for (int i=;i<n;i++){
if (map[x][i]>= && proc[i]==false){ // i是x可以通往的城市
len+=map[x][i];
ressum+=res[i];
if (len<=sup){
proc[i]=true; //上述处理DFS准备
dfs(i);
proc[i]=false; //下述处理DFS结束后还原
}
len-=map[x][i];
ressum-=res[i];
}
}
return;
} int main(){
int x,y,dis;
//初始化
for (int i=;i<=;i++){
proc[i]=false;
for (int j=;j<=;j++){
dist[i][j]=map[i][j]=-;
}
}
//读入
cin >> n >> m >> c1 >> c2;
for (int i=;i<n;i++)
cin >> res[i];
for (int i=;i<m;i++){
cin >> x >> y >> dis;
dist[y][x]=dist[x][y]=map[y][x]=map[x][y]=dis;
}
//特殊情况判断
if (c1==c2){
cout << "1 " << res[c1];
return ;
}
//Floyd处理
for (int k=;k<n;k++)
for (int i=;i<n;i++)
for (int j=;j<n;j++)
if ((dist[i][j]< && dist[i][k]>= && dist[k][j]>=)||
(dist[i][j]>dist[i][k]+dist[k][j]&&dist[i][k]>=&&dist[k][j]>=))
dist[i][j]=dist[i][k]+dist[k][j];
//DFS
sup=dist[c1][c2];
proc[c1]=true;
ressum=res[c1];
dfs(c1);
//输出
cout << count << " " << resmax;
return ;
}
PTA 1003 Emergency的更多相关文章
- PAT (Advanced Level) Practise 1003 Emergency(SPFA+DFS)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 解题报告 1003. Emergency (25)
1003. Emergency (25) As an emergency rescue team leader of a city, you are given a special map of yo ...
- PAT 1003. Emergency (25)
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003. Emergency (25) dij+增加点权数组和最短路径个数数组
1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...
- PAT 1003 Emergency
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of ...
- PAT 1003 Emergency[图论]
1003 Emergency (25)(25 分) As an emergency rescue team leader of a city, you are given a special map ...
- 1003 Emergency (25 分)
1003 Emergency (25 分) As an emergency rescue team leader of a city, you are given a special map of y ...
- 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 ...
- PAT甲级1003. Emergency
PAT甲级1003. Emergency 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
随机推荐
- Linux下安装 php imagick扩展
今天小编 由于工作需求用到了 imagick 这个扩展 服务器环境是 lnmp 架构下面稍微来介绍下 这个 东东 imagick是一个PHP的扩展,用ImageMagick提供的API来进行图片的创 ...
- vue 过渡 & 动画
过渡 & 动画 过渡动画 用css先定义好动画效果 .a-enter-active, .a-leave-active { transition: all 1.5s; } .a-enter, . ...
- Android 7.0新特性“Nougat”(牛轧糖)。
1.Unicode 9支持和全新的emoji表情符号 Android Nougat将会支持Unicode 9,并且会新增大约70种emoji表情符号.这些表情符号大多数都是人形的,并且提供不同的肤色, ...
- Spring——自动装配(@Autowired/@Profile/底层组件)
本文介绍Spring中关于自动装配的方法和规则,以及@Profile动态激活的用法和一个例子. 一.@Autowired自动装配 @Autowired注解可以加在构造器.属性.方法.方法参数上. 自动 ...
- 动态规划------背包问题(c语言)
/*背包问题: 背包所能容纳重量为10:共五件商品,商品重量用数组m存储m[5]={2,2,6,5,4}, 每件商品的价值用数组n存储,n[5]={6,3,5,4,6};求背包所能装物品的最大价值. ...
- iOS9下的Map Kit View下的使用
最近有个任务是关于地理位置上的标注开发,经过一些资料的查找和对比,现总结一些经验,给读者也是给自己. iOS9下的Map Kit View实际是以前MapKit,只不过换了一个名字,实际是指同一个UI ...
- 【Java并发工具类】原子类
前言 为保证计数器中count=+1的原子性,我们在前面使用的都是synchronized互斥锁方案,加锁独占访问的方式未免太过霸道,于是我们来介绍另一种解决原子性问题的无锁方案:原子变量.在正式介绍 ...
- 通过LD_PRELOAD绕过disable_functions
今天做靶场时遇到了一个情形:拿到了webshell,却不能执行任何命令,如图 后来百度知道了disable_functions功能,这类服务器针对命令执行函数做了防范措施 一般绕过思路是利用漏掉的函数 ...
- MySQL面试笔试题集-BAT
01.MySQL技术特点? Mysql数据库软件是一个客户端或服务器系统,支持各种客户端程序和库的多线程SQL服务器.不同的后端.广泛的应用程序编程接口和管理工具 02.MySQL默认端口号? 330 ...
- 万科A顺利出局,布局一心堂
万科的这两日的走势还不错,今日冲高回落,顺利出局. 那么有选中了一只 股票 一心堂 资金量W 12 建仓价格 22.2 加仓系数 1.5 加仓间隔 1.50% 总盈利比 ...