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头牛的中位数最大.若不存在 ...
随机推荐
- Error 56: …… VPN Service has not been started
vpn service在系统服务中名为Cisco System, Inc. VPN Service(cvpnd),异常情况下,该服务时停止的,并且手动启动该服务后没几秒,就被kill了.根据Zhiga ...
- xsqlbuilder使用说明
动态构造sql的利器:rapid-xsqlbuider详细说明 特性列表: 动态构造sql条件语句,提供sql拼接与使用占位符两种方式 数据类型的修饰 对SQL注入攻击的防范 问题: 手工构造SQL语 ...
- AJAX获取JSON形式的数据
test.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 编程工具篇——Vim
配置 配置文件位于:/etc/vim/vimrc(添加配置在文件末尾输入代码即可) 常用配置 配色方案 :colorscheme ron(其中ron为我的配色方案,也可以选择其他,软件中自带配色文件全 ...
- 数组初始化(c, c++, gcc, g++)
这是很基础的东西,但基础的重要性不言而喻,我敢肯定这个知识点我肯定曾经了解过,但现在,我不敢确定,由此可见纪录的重要性,这世界没有什么捷径,找对方向,然后不停重复.所以从今天开始,我会比较详细的纪录这 ...
- sql查询数据库表中重复记录方法
1.查找表中多余的重复记录,重复记录是根据单个字段(peopleId)来判断 代码如下: select * from people where peopleId in (select peopleId ...
- 有关文件夹与文件的查找,删除等功能 在 os 模块中实现
最近在写的程序频繁地与文件操作打交道,这块比较弱,还好在百度上找到一篇不错的文章,这是原文传送门,我对原文稍做了些改动. 有关文件夹与文件的查找,删除等功能 在 os 模块中实现.使用时需先导入这个模 ...
- 接收时物料必须为Active状态
应用 Oracle Inventory 层 Level Function 函数名 Funcgtion Name RCV_RCVRCERC 表单名 Form Name RCVRCERC 说明 Descr ...
- ajax与算法,sql的group处理
function correctAction(){ $semester_id = $this->_getParam("semester_id"); $day = $this- ...
- uestc Palindromic String
字符串hash因为如果一个字符串是回文串,那么正着做哈希和反着做哈希结果应该一样.于是我们先正反各做一边哈希.如果判断出来一个字符串是回文穿那么这个字符串的前半部分和后半部分的重数一定相同,于是当前位 ...