hdu 2337 Escape from Enemy Territory
题目大意
给你一张nn*mm矩形地图。上面有些点上有敌营。给你起点和终点, 你找出一条最优路径。满足最优路径上的点离敌营的最近最短距离是所有路径最短的。若有多条找路径最短的一条。
分析
通过二分来确定路径离敌营最短距离。然后bfs来验证。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#define maxn 1010
#define MAXN 2005
#define MAXM 20000005
#define INF 100000000000000
#define oo 1000000007
using namespace std; typedef long long LL;
struct Point
{
int x,y,dist;
}p[maxn*maxn];
int mid,n,nn,mm,stx,sty,edx,edy;
int vist[maxn][maxn],vistdist[maxn][maxn];
int dirx[]={,-,,};
int diry[]={-,,,};
int max_dist,dist1,dist2;
void bfs1()
{
queue<Point> q;
while(!q.empty())
q.pop();
for(int i=;i<n;i++)
q.push(p[i]);
while(!q.empty())
{
Point tem=q.front();
q.pop(); for(int i=;i<;i++)
{
int newx=tem.x+dirx[i];
int newy=tem.y+diry[i];
if(newx>=&&newx<mm&&newy>=&&newy<nn&&!vist[newx][newy])
{
vist[newx][newy]=;
int newdist=tem.dist+;
vistdist[newx][newy]=newdist;
max_dist=max(max_dist,newdist);
Point pp;
pp.x=newx;
pp.y=newy;
pp.dist=newdist;
q.push(pp);
}
}
}
}
int bfs2()
{
queue<Point> q;
while(!q.empty())
q.pop();
Point pp;
pp.x=stx;
pp.y=sty;
pp.dist=;
q.push(pp);
memset(vist,,sizeof(vist));
if(vistdist[stx][sty]<mid)
return ; while(!q.empty())
{
Point tem=q.front();
q.pop();
if(tem.x==edx&&tem.y==edy)
{
dist1=mid;
dist2=tem.dist;
return ;
}
for(int i=;i<;i++)
{
int xx=tem.x+dirx[i];
int yy=tem.y+diry[i];
if(xx>=&&xx<mm&&yy>=&&yy<nn&&!vist[xx][yy]&&vistdist[xx][yy]>=mid)
{
vist[xx][yy]=;
Point pp;
pp.x=xx;
pp.y=yy;
pp.dist=tem.dist+;
q.push(pp);
} }
}
return ; }
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d %d %d",&n,&mm,&nn);
scanf("%d %d %d %d",&stx,&sty,&edx,&edy);
memset(vist,,sizeof(vist));
for(int i=;i<n;i++)
{
scanf("%d %d",&p[i].x,&p[i].y);
p[i].dist=;
vistdist[p[i].x][p[i].y]=;
vist[p[i].x][p[i].y]=;
}
max_dist=-;
bfs1();
int l=,r=max_dist;
while(l<=r)
{
mid=(l+r)/;
if(bfs2())
l=mid+;
else
r=mid-; }
printf("%d %d\n",dist1,dist2);
}
return ;
}
注意的是队列数组别开小了,还有就是插入队列的时候应该这样写
Point pp;
pp.x=newx;
pp.y=newy;
pp.dist=newdist;
q.push(pp);
如果这样写就会CE
q.push((Point){newx,newy,newdist});
hdu 2337 Escape from Enemy Territory的更多相关文章
- poj 3501 Escape from Enemy Territory 二分+bfs
水题,不解释. #include<stdio.h> #include<math.h> #include<cstring> #include<algorithm ...
- poj 3501 Escape from Enemy Territory 预处理+二分+bfs
传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...
- HDU 3533 Escape(大逃亡)
HDU 3533 Escape(大逃亡) /K (Java/Others) Problem Description - 题目描述 The students of the HEU are maneu ...
- HDU 3605 Escape (网络流,最大流,位运算压缩)
HDU 3605 Escape (网络流,最大流,位运算压缩) Description 2012 If this is the end of the world how to do? I do not ...
- Hdu 3605 Escape (最大流 + 缩点)
题目链接: Hdu 3605 Escape 题目描述: 有n个人要迁移到m个星球,每个星球有最大容量,每个人有喜欢的星球,问是否所有的人都能迁移成功? 解题思路: 正常情况下建图,不会爆内存,但是T ...
- HDU 3605 Escape(状压+最大流)
Escape Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- HDU 3605 Escape 最大流+状压
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 3605 Escape 二分图的多重匹配(匈牙利算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3605 Escape Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 3533 Escape bfs 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=3533 一道普通的bfs,但是由于代码实现出了bug还是拖了很久甚至对拍了 需要注意的是: 1.人不能经过炮台 2 ...
随机推荐
- Jquery操作复选框(CheckBox)的取值赋值实现代码
赋值 复选框 CheckBox 遍历 取值 1. 获取单个checkbox选中项(三种写法): $("input:checkbox:checked").val() 或者 $(&q ...
- iOS各种动画效果
ios各种动画效果 最普通动画: //开始动画 [UIView beginAnimations:nil context:nil]; //设定动画持续时间 [UIView setAnimationDu ...
- bzoj 2820 YY的GCD 莫比乌斯反演
题目大意: 给定N, M,求1<=x<=N, 1<=y<=M且gcd(x, y)为质数的(x, y)有多少对 这里就抄一下别人的推断过程了 后面这个g(x) 算的方法就是在线性 ...
- POJ 1741 树上的点分治
题目大意: 找到树上点对间距离不大于K的点对数 这是一道简单的练习点分治的题,注意的是为了防止点分治时出现最后分治出来一颗子树为一条直线,所以用递归的方法求出最合适的root点 #include &l ...
- HDU 3336 扩展kmp
题目大意: 找到字符串中所有和前缀字符串相同的子串的个数 对于这种前缀的问题,通常通过扩展kmp来解决 其实吧这是我第一次做扩展kmp的题目,原来确实看过这个概念,今天突然做到,所以这个扩展kmp的模 ...
- 在Tomcat下配置Solr 4.x 版本
solr是一款非常优秀的全文检索服务器,最新版本在配置和前台页面上都做了较大的改动, 所以对用惯了老版本的朋友们来说,再重新配置新版本的solr,无疑又是一件痛苦的事情. 配置环境:windows ...
- Apache Ant运行时Unable to locate tools.jar解决方法
下载Apache Ant 一.解压ant安装包在D:\ant下 二.环境变量配置 ANT_HOME D:\ant\apache-ant-1.9.0 CLASSPATH ;%ANT_HOME%lib; ...
- matlab图像剪裁命令imcrop()
调用格式: I2=imcrop(I,RECT): X2=imcrop(X,MAP,RECT): RGB2=imcrop(RGB,RECT): 其中,I.X.RGB分别对应灰度图像.索引图像.RGB图像 ...
- navtab方法参数以及事件
参数(options) DOM方式初始化navtab的,推荐使用集合属性data-options定义参数,如果使用data属性定义参数,注意转换成对应的名称. 名称 类型 默认值 描述 id stri ...
- 元素ID命名规范
因为本框架默认所有内容都位于一个Document中,所以为元素命名为ID的时候需要做到唯一性,如果确实不可避免的会出现有重读ID的现象,需要操作当前页片(页面片段,就是子页面)的时候,尽量用: $.C ...