描述

FJ now is a postman of a small town in the hills. The town can be represented by a N×N matrix. Each field is represented with:
-'K' character: a house;
-'P' character: the post office;
-'.' character: a pasture.

Moreover, each field is assigned with an altitude.
Each day, FJ starts at the single post office of the town, and has to deliver mail to all houses in the town. He can move horizontally, vertically and diagonally to adjacent squares. When he delivers all mails, he must return to the post office.
Now, we define the "tiredness" as the difference between the heights of the highest and the lowest field during the delivering.  Tell FJ the minimal tiredness to deliver all the mail.

输入

The first line has an integer N (2 ≤ N ≤ 50), then follows 2*N lines. For the first N lines, each lines has N characters (can be 'P', 'K', '.' characters and 'P' appears exactly once, and 'K' appears at least once) . The next N lines each contain N positive integers indicating  the corresponding altitudes of the N*N fields.
Each integer is less than 1000000.

输出

Output one integer indicating the minimum possible tiredness.

样例输入

2
P.
.K
2 1
3 2

样例输出

0

题意:

给一个n*n的矩阵,每个点都有一个高度,图中有一个邮局和若干个房子,求邮递员以邮局为起点途径所有房子并返回起点所经过的点的高度差(最大值-最小值)最小。

邮递员每次可以向周围八个方向移动一格。

题目分析:

首先可以知道只有最多2500个点,我们可以先枚举高度最小值,然后二分它的最大值,二分过程中用bfs判能不能从起点到所有房子。复杂度为:n^4*log(n^2)。

 

#include <bits/stdc++.h>
using namespace std;
const int N=;
char s[N][N];
int dx[]={-,-,-,,,,,},dy[]={-,,,-,,-,,};
vector<int> G[N*N],A,B;
int h[N*N],n,S;
bool vis[N*N];
bool check(int l,int r) {//判断从起点开始只经过高度[l,r]能否到达所有房子
if(h[S]<l||h[S]>r) return false;
for(int i=;i<n*n;i++) vis[i]=false;
queue<int> qu;
qu.push(S),vis[S]=true;
while(!qu.empty()) {
int u=qu.front();qu.pop();
for(int i=;i<G[u].size();i++) {
int v=G[u][i];
if(h[v]<l||h[v]>r||vis[v]) continue;
qu.push(v),vis[v]=true;
}
}
for(int i=;i<A.size();i++) {
if(!vis[A[i]]) return false;
}
return true;
}
int main() {
scanf("%d",&n);
for(int i=;i<n;i++) scanf("%s",s[i]);
for(int i=;i<n;i++) {
for(int j=;j<n;j++) {
scanf("%d",&h[i*n+j]);
B.push_back(h[i*n+j]);//存所有的高度
if(s[i][j]=='P') S=i*n+j;//起点
else if(s[i][j]=='K') A.push_back(i*n+j);//存所有的房子
}
}
for(int i=;i<n;i++) {//建图
for(int j=;j<n;j++) {
for(int k=;k<;k++) {
int xx=i+dx[k],yy=j+dy[k];
if(xx<||xx>=n||yy<||yy>=n) continue;
G[i*n+j].push_back(xx*n+yy);
}
}
}
sort(B.begin(),B.end());
int up=B.size(),mn=0x3f3f3f3f;
for(int i=;i<up;i++) {//枚举最小值
int L=i,R=up-,ans=-;//二分最大值
while(L<=R) {
int mid=(L+R)>>;
if(check(B[i],B[mid])) ans=mid,R=mid-;
else L=mid+;
}
if(ans!=-) mn=min(mn,B[ans]-B[i]);
}
printf("%d\n",mn);
return ;
}

TZOJ 4471: Postman FJ (二分+bfs)的更多相关文章

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

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

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

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

  3. TZOJ 1594 Optimal Milking(二分+最大流)

    描述 FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 ...

  4. TopCoder SRM 642 Div.2 1000 --二分+BFS

    题意: 给你一张图,N个点(0~N-1),m条边,国王要从0到N-1,国王携带一个值,当走到一条边权大于此值的边时,要么不走,要么提升该边的边权,提升k个单位花费k^2块钱,国王就带了B块钱,问能携带 ...

  5. 【BZOJ】1189: [HNOI2007]紧急疏散evacuate(二分+bfs+网络流)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1189 表示完全不会QAQ.... 于是膜拜题解orz 二分时间........... 于是转换成判定 ...

  6. hiho_1139_二分+bfs搜索

    题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分     最小化最大值,考虑采用二分搜索.对所有的边长进 ...

  7. hdu 5652 India and China Origins 二分+bfs

    题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...

  8. poj 3501 Escape from Enemy Territory 预处理+二分+bfs

    传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...

  9. POJ 2110 Mountain Walking 二分+bfs

    传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...

随机推荐

  1. [Day4] Nginx Http模块一

    之前介绍了Nginx作为静态资源服务器的用法,​除此之外,Nginx更多的场景是作为反向代理服务器,提高网站的并发和可用性.下面几节着重说一下作为反向代理的http模块,并且了解一些Nginx的架构. ...

  2. workbench使用

    1.你是指默认的mysql目录下data里面的'mysql'这个schema没有在workbench里面看到吧?点击菜单-Edit->Preferences里面的SQL Editor,然后把&q ...

  3. TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式

    1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...

  4. 安装node/npm,通过express搭建node项目

    nodejs软件的下载地址:https://nodejs.org/en/ (推荐下载稳定版) 1.只要安装好了nodejs,就自动安装好了npm包. 2.在cmd中通过命令node -version查 ...

  5. Hackerrank--Ashton and String(后缀数组)

    题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...

  6. http请求生命周期流程

    https://mp.weixin.qq.com/s/fpA2CThk2L-YBw6z0k4rtw HTTP 请求/相应 1.客户端连接到Web服务器 一个HTTP客户端,通常是浏览器,与Web服务器 ...

  7. 淘宝镜像(CNPM)安装

    淘宝镜像安装:开始-运行-填写cmd,回车键确定- 输入"npm install -g cnpm --registry=https://registry.npm.taobao.org&quo ...

  8. JavaScript模式:字面量和构造函数

    本篇主要讨论了通过字面量以构造对象的方法,比如对象.数组以及正则表达式等字面量的构造方法,同时还讨论了与类似Object()和Array()等内置构造函数相比,为什么基于字面量表示法是更为可取. 对象 ...

  9. 让Drewtech的J2534 ToolBox 软件支持任何J2534的设备

    更改windows注册表中的FunctionLibrary和ConfigApplication,将DLL和exe路径替换原来的,其他不要动. 或者 create second key in regis ...

  10. 【python之路13】python的深浅拷贝

    深浅拷贝 一.数字和字符串 对于 数字 和 字符串 而言,赋值.浅拷贝和深拷贝无意义,因为其永远指向同一个内存地址. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 impor ...