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 ...
随机推荐
- static初始化问题探究
两个小示例 demo1 package containers; public class TempTest { static{ a= 1; // System.out.println(a); } st ...
- 常州培训 day5 解题报告
第一题:(贪心) 题目大意:给出N*M的矩形,要用正方形将它铺满(正方形之间不能重叠),相邻的正方形颜色不能相同,颜色用ABCD表示.要求从上到下从左到右字典序最小. N,M<=100 解题过程 ...
- DatagridView的CellLeave光标离开响应事件,实现某列数字自动求和
//光标离开DatagridView,循环获取DatagridView的每一行的第3列的值,相加传给重量 private void dgpz_dataGridView_CellLeave(object ...
- #数据结构-fib
/////////////////////////////////////////////////////////////////////////////// // // FileName : fic ...
- HTML5中canvas的save和restore方法
canvas的save和restore方法: save() 方法把当前绘画状态的一份拷贝压入到一个保存图像状态的栈中.这里的绘画状态指坐标原点.变形时的变化矩阵(该矩阵是调用 rotate().sca ...
- vim配置及插件安装管理(超级详细)
1 写在前面 Linux下编程一直被诟病的一点是: 没有一个好用的IDE, 但是听说Linux牛人, 黑客之类的也都不用IDE. 但是对我等从Windows平台转移过来的Coder来说, 一个好用 ...
- [__NSCFString absoluteURL]错误的解决方案
Xcode提醒错误: -[__NSCFString absoluteURL]: unrecognized selector sent to instance 0x8c4d3a0 *** Termina ...
- C# HttpBrowser 跨进程访问,解决内存泄露问题
#undef DEBUG using Microsoft.Win32; using Newtonsoft.Json; using System; using System.Collections.Ge ...
- Servlet三种实现方法(四)
开发Servlet有三种方式:1.实现Servlet接口2.通过继承GenericServlet3.通过继承HttpServlet 一.实现Servlet接口 需求如下:请使用实现 接口的方式,来开发 ...
- jQuery 关于 end() 方法的详细解释
<ul class="first"> <li class="foo">list item 1</li> <li> ...