传送门

给一个起点一个终点, 给出整个地图的宽和高, 给出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的更多相关文章

  1. poj 3501 Escape from Enemy Territory 二分+bfs

    水题,不解释. #include<stdio.h> #include<math.h> #include<cstring> #include<algorithm ...

  2. hdu 2337 Escape from Enemy Territory

    题目大意 给你一张nn*mm矩形地图.上面有些点上有敌营.给你起点和终点, 你找出一条最优路径.满足最优路径上的点离敌营的最近最短距离是所有路径最短的.若有多条找路径最短的一条. 分析 通过二分来确定 ...

  3. POJ 2723 Get Luffy Out(2-SAT+二分答案)

    Get Luffy Out Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8851   Accepted: 3441 Des ...

  4. hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)

    题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...

  5. hdu-5652 India and China Origins(二分+bfs判断连通)

    题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others)     Memory Limit: 65536/65536 K ...

  6. 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 ...

  7. loj 1150(spfa预处理+二分+最大匹配)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26864 思路:首先是spfa预处理出每个'G'到'H'的最短距离, ...

  8. poj 1247 The Perfect Stall 裸的二分匹配,但可以用最大流来水一下

    The Perfect Stall Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 16396   Accepted: 750 ...

  9. POJ 2010 Moo University - Financial Aid( 优先队列+二分查找)

    POJ 2010 Moo University - Financial Aid 题目大意,从C头申请读书的牛中选出N头,这N头牛的需要的额外学费之和不能超过F,并且要使得这N头牛的中位数最大.若不存在 ...

随机推荐

  1. JavaScript之JSON

    一.简介:Json是JavaScript中读取结构化数据更好的方式.因为Json数据可以直接传给eval(),而且不必创建DOM对象.Json是一种数据格式,不是一种编程语言,虽然具有相同的语法形式, ...

  2. C语言实现有序二叉树(1)

    在cpp中使用的C语言 头文件 /* 有序二叉树 BsTree */ #ifndef _BT_H #define _BT_H /*节点*/ typedef struct BsTreeNode { in ...

  3. Python核心编程读笔 5: python的序列

    第六章 序列:字符串.列表.元组 一.序列 (1)序列类型操作符 seq[ind] 获得下标为 ind 的元素 seq[ind1:ind2] 切片操作 seq * expr 序列重复 expr 次 s ...

  4. 1.js编程风格。 --- 编写可维护的javascript

    1. 使用4个空格字符作为一个缩进层级. 2. 不省略分号. ---> 自动插入分号机制非常复杂,且难于记忆. 3. 行的长度限定于80个字符. 4. 通常在运算符换行之后,下一行会增加两个层级 ...

  5. Linux学习之进程管理

    |-进程管理     进程常用命令        |- w查看当前系统信息        |- ps进程查看命令        |- kill终止进程        |- 一个存放内存中的特殊目录/p ...

  6. Android RelativeLayout常用属性介绍

    下面介绍一下RelativeLayout用到的一些重要的属性: 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 android:layou ...

  7. attribute和property兼容性分析

    上一篇文章中,详细的分析了他们的区别,请看Javascript中的attribute和property分析 这次,来详细的看下他们的兼容性,这些内容主要来自于对于jQuery(1.9.x)源代码的分析 ...

  8. Nginx+PostgreSQL+Django+UWSGI搭建

    最近因为项目上的需要开始大量使用nginx,因此也想趁机将以前常用的django+apache的架构换成django+nginx.常见的 django webapp 部署方式采用FCGI 或 WSGI ...

  9. hdu 4635 Strongly connected 强连通

    题目链接 给一个有向图, 问你最多可以加多少条边, 使得加完边后的图不是一个强连通图. 只做过加多少条边变成强连通的, 一下子就懵逼了 我们可以反过来想. 最后的图不是强连通, 那么我们一定可以将它分 ...

  10. virtualBox使用nat模式下ssh连接

    virtualBox本地虚拟机通过ssh连接一般可通过桥接模式和Nat模式 桥接模式下,共享本地主机网卡,在同一个局域网之下,直接获取Ip地址就可以进行连接了. Nat模式下,获取的Ip与本地主机不是 ...