传送门

给一个起点一个终点, 给出整个地图的宽和高, 给出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. QRMaker生成二维码,支持中文

    QRMaker如果想支持中文,可以将中文转为UTF8,然后用InputDateB直接传入Byte() Option Explicit Private Declare Function WideChar ...

  2. C# 与 VB.NET 对比

    C# 与 VB.NET 对比 2008-06-20 15:30 by Anders Cui, 1462 阅读, 3 评论, 收藏, 编辑 Table of Contents 1.0       Int ...

  3. JS中String的反转函数

    利用prototype封住reverse方法: (求更简便的方法: String.prototype.reverse=function () { var str=""; for(v ...

  4. Android 使用Jsoup解析Html

    想要做一个看新闻的应用,类似Cnbeta客户端的东西.大致思路如下:根据链接获取新闻列表页的html代码,然后解析,找到所有的新闻标题和新闻链接用listView显示,当点击ListView的Item ...

  5. c语言libcurl 使用实例get/post方法+c语言字符串处理

    #include <stdio.h> #include <curl/curl.h> #include <string.h> #include <ctype.h ...

  6. VC中利用多线程技术实现线程之间的通信

    当前流行的Windows操作系统能同时运行几个程序(独立运行的程序又称之为进程),对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程,线程提供了多任务处理的能力.用进程和线程的观点来研究软 ...

  7. C++ 栈的实现

    #ifndef _STACK_H #define _STACK_H #pragma once template< class T >class Stack{public: Stack( v ...

  8. docker基于 aufs 文件系统

    docker的核心功能就是容器版本管理,在容器层实现了CVS版本管理,比如git那种可以commit/roll back 而AUFS就是其实现基础 AUFS可以实现文件系统的snapshot,这样对f ...

  9. 移动开发之fastclick 点击穿透

    穿透(点穿)是在mobile各种浏览器上发生的常见的bug.可能是由click事件的延迟(300ms)或者事件冒泡导致 现象:在A页面中有个 btn1<或a标签>,在B页面中有个 btn2 ...

  10. 04737_C++程序设计_第5章_特殊函数和成员

    例5.1 分析下面程序中析构函数与构造函数的调用顺序. #include<iostream> using namespace std; class object { private: in ...