题目链接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搜索+多终点)的更多相关文章

  1. HDU 1180 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1180 题目大意:迷宫中有一堆楼梯,楼梯横竖变化.这些楼梯在奇数时间会变成相反状态,通过楼梯会顺便到达 ...

  2. HDU 1026 (BFS搜索+优先队列+记录方案)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1026 题目大意:最短时间内出迷宫.迷宫里要杀怪,每个怪有一定HP,也就是说要耗一定时.输出方案. 解 ...

  3. HDU 1242 (BFS搜索+优先队列)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目大意:多个起点到一个终点,普通点耗时1,特殊点耗时2,求到达终点的最少耗时. 解题思路: ...

  4. HDU 2531 (BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2531 题目大意: 你的身体占据多个点.每次移动全部的点,不能撞到障碍点,问撞到目标点块(多个点)的最 ...

  5. HDU 1312 (BFS搜索模板题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1312 题目大意:问迷宫中有多少个点被访问. 解题思路: DFS肯定能水过去的.这里就拍了一下BFS. ...

  6. HDU - 2612 bfs [kuangbin带你飞]专题一

    分别以两个人的家作为起点,bfs求得到每个KFC最短距离.然后枚举每个KFC,求得时间之和的最小值即可. 此题不符合实际情况之处:  通过了一个KFC再去另一个KFC可以吗? 出题人都没好好想过吗? ...

  7. hdu 1240:Asteroids!(三维BFS搜索)

    Asteroids! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  8. HDU 2612 Find a way bfs 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=2612 bfs两次就可将两个人到达所有kfc的时间求出,取两人时间之和最短的即可,这个有点不符合实情,题目应该出两 ...

  9. HDU.2612 Find a way (BFS)

    HDU.2612 Find a way (BFS) 题意分析 圣诞节要到了,坤神和瑞瑞这对基佬想一起去召唤师大峡谷开开车.百度地图一下,发现周围的召唤师大峡谷还不少,这对基佬纠结着,该去哪一个...坤 ...

随机推荐

  1. index and polymorphic

    http://guides.rubyonrails.org/association_basics.html#polymorphic-associations class CreateStars < ...

  2. Instance Variables in ruby

    Dogs have many shared characteristics, like the abilities to wag their tails and drink water from a ...

  3. 调用python 报R6034 错误

    R6034 指的是:"An application has made an attempt to load the C runtime library incorrectly. Please ...

  4. POJ 1797 Heavy Transportation (Dijkstra变形)

    F - Heavy Transportation Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & ...

  5. users

    NAME users - print the user names of users currently logged in to the current host SYNOPSIS users [O ...

  6. poj3904

    题意:给出n(n<10000)个数,这些数<=10000,要求选出四个数字且他们的最大公约数为1的(注意:不需要两两互质),有多少种选法. 分析: 容斥原理 假设平面上有一些圆,互相之间有 ...

  7. SQL Server集群服务器的优缺点

    由二台或更多物理上独立的服务器共同组成的“虚拟”服务器称之为集群服务器.一项称做MicroSoft集群服务(MSCS)的微软服务可对集群服务器进行管理.一个SQL Server集群是由二台或更多运行S ...

  8. block引发的陷阱

    block在项目的开发中使用时非常频繁的,苹果官方也极力推荐使用block.其实,究其本质,block就是指向结构体的指针(可利用运行时机制查看底层生成的c代码).然而在使用block时会存在很多陷阱 ...

  9. Linux用户名显示-bash-4.1$快速排查

    最近项目使用的的服务器有点多(100多台),很多开发同事经常问这个问题,现在整理如下: 几个可能导致的原因: 1 用户的家目录所属组被改为root,解决方法使用root执行cd /home/;chow ...

  10. CentOS下源码安装mplayer播放器

    http://www.mplayerhq.hu/MPlayer/releases/ [root@ok MPlayer-1.2.1]# pwd /root/MPlayer-1.2.1 http://ww ...