HDU 2612 (BFS搜索+多终点)
题目链接: http://poj.org/problem?id=1947
题目大意:两人选择图中一个kfc约会。问两人到达时间之和的最小值。
解题思路:
对于一个KFC,两人的BFS目标必须一致。
于是就有以下的SB行为:记录所有KFC,对于每个KFC,对两人BFS。然后你就会看见红红的TLE。
实际上,只需要两次BFS就可以了,用time_k[0][i]记录a同学到达第i个kfc的时间,time_k[1][i]记录b同学到达第i个kfc的时间。
在一次bfs过程中,记录到达所有kfc的时间。
本题有个不严谨的地方,就是没有说明不被选择的kfc是相当于road还是障碍,我是当成road算的。
由于图中有些kfc是不能到达的,所以time_k数组初始化为inf,防止不能到达的kfc扰乱结果。
最后ans=min(ans,time_k[0][i]+time_k[1][i])。
#include "cstdio"
#include "cstring"
#include "string"
#include "iostream"
#include "queue"
#include "vector"
using namespace std;
#define inf 0x3f3f3f3f
int n,m,sx,sy,ssx,ssy,ex,ey,dir[][]={-,,,,,-,,},vis[][],ans,cnt,kfc[][],time_k[][*];
char map[][];
struct status
{
int x,y,dep;
status(int x,int y,int dep):x(x),y(y),dep(dep) {}
};
void bfs(int x,int y,int p)
{
memset(vis,,sizeof(vis));
queue<status> Q;
Q.push(status(x,y,));
vis[x][y]=true;
while(!Q.empty())
{
status t=Q.front();Q.pop();
for(int s=;s<;s++)
{
int X=t.x+dir[s][],Y=t.y+dir[s][];
if(X<||X>n||Y<||Y>m||map[X][Y]=='#'||vis[X][Y]) continue;
vis[X][Y]=true;
if(map[X][Y]=='@') time_k[p][kfc[X][Y]]=t.dep+;
Q.push(status(X,Y,t.dep+));
}
}
}
int main()
{
//freopen("in.txt","r",stdin);
ios::sync_with_stdio(false);
string tt;
while(cin>>n>>m)
{
memset(time_k,0x3f,sizeof(time_k));
ans=inf,cnt=;
for(int i=;i<=n;i++)
{
cin>>tt;
for(int j=;j<tt.size();j++)
{
map[i][j+]=tt[j];
if(tt[j]=='Y') {sx=i;sy=j+;}
if(tt[j]=='M') {ssx=i;ssy=j+;}
if(tt[j]=='@') {kfc[i][j+]=++cnt;}
}
}
bfs(sx,sy,);bfs(ssx,ssy,);
for(int i=;i<=cnt;i++) ans=min(ans,(time_k[][i]+time_k[][i])*);
cout<<ans<<endl;
}
}
| 11915289 | 2014-10-19 20:15:30 | Accepted | 2612 | 15MS | 996K | 1596 B | C++ | Physcal |
HDU 2612 (BFS搜索+多终点)的更多相关文章
- HDU 1180 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...
- HDU 1026 (BFS搜索+优先队列+记录方案)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...
- HDU 1242 (BFS搜索+优先队列)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...
- HDU 2531 (BFS搜索)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...
- HDU 1312 (BFS搜索模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...
- HDU - 2612 bfs [kuangbin带你飞]专题一
分别以两个人的家作为起点,bfs求得到每个KFC最短距离.然后枚举每个KFC,求得时间之和的最小值即可. 此题不符合实际情况之处: 通过了一个KFC再去另一个KFC可以吗? 出题人都没好好想过吗? ...
- hdu 1240:Asteroids!(三维BFS搜索)
Asteroids! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- HDU 2612 Find a way bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...
- HDU.2612 Find a way (BFS)
HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...
随机推荐
- 用php实现百度网盘图片直链的代码分享
第一种代码:代码量较少通过正则表达式获取百度网盘的文件真实地址,来实现直链的效果 将下面的代码保存为downbd.php 复制代码代码如下: <?php $canshu=$_SERVER[&qu ...
- 【Web】关于URL中文乱码问题
关于URL编码 一.问题的由来 URL就是网址,只要上网,就一定会用到. ...
- 【leetcode】Palindrome Partitioning II
Palindrome Partitioning II Given a string s, partition s such that every substring of the partition ...
- coco2dx加载网络图片并保存
直接上代码 bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::i ...
- 两个文件去重的N种姿势
最近利用shell帮公司优化挖掘关键词的流程,用shell替代了多个环节的操作,极大提高了工作效率. shell在文本处理上确有极大优势,比如多文本合并.去重等,但是最近遇到了一个难搞的问题,即两个大 ...
- CodeForces - 427A (警察和罪犯 思维题)
Police Recruits Time Limit: 1000MS Memory Limit: 262144KB 64bit IO Format: %I64d & %I64u Sub ...
- Servlet过滤器和监听器
1,Servlet过滤器 <filter> <filter-name>charset</filter-name> <filter-class>org.g ...
- [Android Pro] UI设计师不可不知的安卓屏幕知识
reference to : http://www.android100.org/html/201505/24/149342.html 不少设计师和工程师都被安卓设备纷繁的屏幕搞得晕头转向,我既做UI ...
- dbVisualizer破解
下载dbvis.puk,替换C:\Program Files\DbVisualizer\lib\dbvis.jar中的文件. 替换后打开选手动的key:下载地址dbvis.license
- fork
#include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <fcntl.h> ...