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 题意: 作为一个城市的紧急救援队长,你将得到一个你所在国家的特别地图.该地图显示了几条分散的城市,连接着一些道路.每个城市的救援队数量和任何一对城市之间的每条 ...
随机推荐
- 02-msyql-存储引擎
1.优化器针对索引算法 1.1MySQL索引自优化-AHI(自适应HASH索引) MySQL的InnoDB引擎,能够创建只有Btree. AHI作用: 自动评估"热"的内存索引pa ...
- k8s系列--- dashboard认证及分级授权
http://blog.itpub.net/28916011/viewspace-2215214/ 因版本不一样,略有改动 Dashboard官方地址: https://github.com/kube ...
- hive使用beeline无法登录时的解决办法
如果参考官方文档执行下列命令,报错: $ $HIVE_HOME/bin/hiveserver2 $ $HIVE_HOME/bin/beeline -u jdbc:hive2://$HS2_HOST:$ ...
- oracle11g R2数据库的迁移(同windows系统迁移)使用RMAN
实验环境:windows 2008 R2 & windows 2008 R2 Oracle版本:11.2.0.1.0 源数据库端: 为保证在恢复之后的数据库中得到一致的数据,应禁止用户对数据的 ...
- 嗅探、DNS劫持配合CS钓鱼
本章节讲述的是嗅探和DNS劫持的利用 嗅探:同一个局域网下,原本应该丢弃的包,被保留下来,即使不开双向欺骗 Driftnet工具:Driftnet监视网络流量,抓取网络流量中的JPEG和GIF图像.这 ...
- symfonos2
0x01 进入网页 啥也没有 0x02 目录爆破 啥也没有 0x03 端口扫描 知识盲区: ProFTPD 1.3.5 用ProFTPD服务权限执行复制命令,默认在'nobody'用户的特权下运行.通 ...
- hyper-v安装ubuntu18的全过程+踩过的坑(win10家庭版)
一.背景介绍(作者的闲言闲语,可跳过) 最近新购入小米笔记本pro15增强版,想在笔记本上装ubuntu18的系统. 最开始尝试了双系统的方法,但是安装完之后不能wifi上网,显卡MX250不能适配, ...
- C++中static关键字的用法
运行一个完整的程序.我们可将整个存储区分为四块: (1)栈区:就比如局部变量,对应的函数参数等这些,调用完之后相应的内存会自己释放掉,很让人省心. (2)堆区:堆来堆去的.得要人动手.所以得我们自己手 ...
- SpringBoot支持SpringData es
ElasticSearch CRUD 1.springboot springData es spring data 是spring对数据访问抽象.这些数据可以放入db,index,nosql等包含以下 ...
- 不用循环游标,一句update代码实现滚动计算
发现一段经典SQL,不用循环游标,一句update代码实现滚动计算结存.为方便理解,结合实例测试之 --1,源数据#t1,jcshl初值为每个sid的当前库存数量,要实现的效果:每个sid的后一结存数 ...