poj 3501 Escape from Enemy Territory 预处理+二分+bfs
给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标。 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离。
首先预处理出来地图上的所有点到敌人的最短距离, 然后二分距离, bfs就可以。
tle了好多次, 到网上搜题解, 看到别人是先把敌人的坐标都存到数组里最后在一起预处理, 而我是读一个点处理一个点, 改了以后才ac.......
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define mem(a) memset(a, 0, sizeof(a))
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const int inf = ;
const double eps = 1e-;
const int mod = 1e9+;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
int dis[][], x, y, n, x1, y1, x2, y2, vis[][];
pll point[];
struct node
{
int x, y, step;
node(){}
node(int x, int y, int step):x(x),y(y),step(step){}
}; void bfs() {
queue <node> q;
for(int i = ; i<n; i++) {
q.push(node(point[i].first, point[i].second, ));
dis[point[i].first][point[i].second] = ;
}
while(!q.empty()) {
node tmp = q.front(); q.pop();
for(int i = ; i<; i++) {
int tmpx = tmp.x+dir[i][];
int tmpy = tmp.y+dir[i][];
if(tmpx>=&&tmpx<x&&tmpy>=&&tmpy<y) {
if(dis[tmpx][tmpy]>tmp.step+) { //这里要注意
dis[tmpx][tmpy] = tmp.step+;
q.push(node(tmpx, tmpy, tmp.step+));
}
}
}
}
} int bin(int val) {
if(dis[x1][y1]<val)
return ;
queue <node> q;
mem(vis);
q.push(node(x1, y1, ));
vis[x1][y1] = ;
while(!q.empty()) {
node tmp = q.front(); q.pop();
if(tmp.x == x2 && tmp.y == y2)
return tmp.step;
for(int i = ; i<; i++) {
int tmpx = tmp.x+dir[i][];
int tmpy = tmp.y + dir[i][];
if(tmpx>=&&tmpx<x&&tmpy>=&&tmpy<y&&!vis[tmpx][tmpy]) {
vis[tmpx][tmpy] = ;
if(dis[tmpx][tmpy]>=val) {
q.push(node(tmpx, tmpy, tmp.step+));
}
}
}
}
return ;
} int main()
{
int t, a, b;
cin>>t;
while(t--) {
mem2(dis);
scanf("%d%d%d", &n, &x, &y);
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
for(int i = ; i<n; i++) {
scanf("%d%d", &a, &b);
point[i] = mk(a, b);
}
bfs();
int l = , r = dis[x1][y1], ans, ans1, ans2;
while(l<=r) {
int m = l+r>>;
ans = bin(m);
if(ans>) {
l = m+;
ans1 = m;
ans2 = ans;
} else {
r = m-;
}
}
printf("%d %d\n", r, ans2);
}
}
poj 3501 Escape from Enemy Territory 预处理+二分+bfs的更多相关文章
- poj 3501 Escape from Enemy Territory 二分+bfs
水题,不解释. #include<stdio.h> #include<math.h> #include<cstring> #include<algorithm ...
- hdu 2337 Escape from Enemy Territory
题目大意 给你一张nn*mm矩形地图.上面有些点上有敌营.给你起点和终点, 你找出一条最优路径.满足最优路径上的点离敌营的最近最短距离是所有路径最短的.若有多条找路径最短的一条. 分析 通过二分来确定 ...
- POJ 2723 Get Luffy Out(2-SAT+二分答案)
Get Luffy Out Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8851 Accepted: 3441 Des ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- hdu-5652 India and China Origins(二分+bfs判断连通)
题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- Poj 3233 Matrix Power Series(矩阵二分快速幂)
题目链接:http://poj.org/problem?id=3233 解题报告:输入一个边长为n的矩阵A,然后输入一个k,要你求A + A^2 + A^3 + A^4 + A^5.......A^k ...
- loj 1150(spfa预处理+二分+最大匹配)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26864 思路:首先是spfa预处理出每个'G'到'H'的最短距离, ...
- poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 16396 Accepted: 750 ...
- POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)
POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...
随机推荐
- CSS3 border属性的妙用
.ribbon { background: #45c9c8; position: absolute; width: 75px; height: 25px; line-height: 25px; top ...
- 如何用SQL操作数据------告别标题党
额,首先跟大家道一个歉,由于本人上次利用标题来骗访问,对各位大哥大姐,叔叔阿姨,弟弟妹妹,and舅子老表的时间及流量造成了严重的浪费,本人深表歉意(好吧,其实本人内心还是有那么一丢丢的自豪的,毕竟是一 ...
- 使用fastcgi_cache加速网站
为了提高网站的性能缓存是一把利器,nginx中可以配置fastcig_cache来缓存不需要实时获取的数据实现动静分离,nginx.conf配置如下: http { - fastcgi ...
- Eclipse 完全 设置UTF-8 环境
当开始一个新的Java项目时,最好将整个环境设置为UTF-8 一般通过如下几步来设置: 1: 设置工作空间的编码格式:Window -> Preferences -> General -& ...
- getElementByID、getElementsByName、getElementsByTagName实例详解
getElementByID.getElementsByName.getElementsByTagName实例详解 本文通过实例,详细介绍了getElementByID.getElementsByNa ...
- Nginx+PostgreSQL+Django+UWSGI搭建
最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx.常见的 django webapp 部署方式采用FCGI 或 WSGI ...
- Cocos2d-x 安装教程for mac(Xcode)
cocos2d v3.x 版本出来后,从配置安装到创建项目都是命令行,下面简单说一下. 1. 下载地址 http://cn.cocos2d-x.org/download/ (虽然没有标明 for ...
- MyBatis 最常见错误,启动时控制台无限输出日志
你是否遇到过下面的情况,控制台无限的输出下面的日志: Logging initialized using ‘class org.apache.ibatis.logging.log4j.Log4jImp ...
- Ubuntu 13.04 小米2S连接Eclipse真机调试
最近想继续将自己以前的一些Android程序代码进行改进和优化,遂将以前的代码在windows下导入eclipse工程,谁知导入后便eclipse假死,甚至windows资源管理器也动弹不得,诡异的是 ...
- CSS蒙版
蒙版:就是在图片上添加一个图层,用于美化页面,增加页面的可读性 <!DOCTYPE html><html><head lang="en"> &l ...