描述

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. 第三方博客同步xmlrpc、rest、API等相关的文章网址记录

    http://answers.microsoft.com/en-us/windowslive/forum/writer-wlsettings/i-am-encountering-a-problem-s ...

  2. Springboot 之 启动报错-Cannot determine embedded database driver class for database type NONE

    Springboot 之 启动报错-数据库 springboot项目在启动时,报如下错误: Error starting ApplicationContext. To display the auto ...

  3. SecondaryNameNode 理解

    NameNode将对文件系统的改动追加保存到本地文件系统上的一个日志文件(edits).当一个NameNode启动时,它首先从一个映像文件(fsimage)中读取HDFS的状态,接着应用日志文件中的e ...

  4. qq邮箱问卷,测试不支持form表单

    想做个类似苹果调查问卷的: 找到qq邮箱的代码编辑器: 写好我们的网页(h5) <!DOCTYPE html> <html lang="en"> <h ...

  5. mybatis框架学习:

    一.什么是框架 它是我们软件开发中的一套解决方案,不同的框架解决的是不同的问题 使用框架的好处: 框架封装了很多的细节,使开发者可以使用极简的方式实现功能 大大提高开发效率 二.三层框架 表现层: 用 ...

  6. ubuntu下搜狗输入法待选框中文乱码问题解决(重启搜狗输入法)

    简单的三个命令就可以解决 killall fcitx //关闭fcitx killall sogou-qimpanel //关闭搜狗输入法 fcitx //打开fcitx

  7. JAVA面试常见问题之Redis篇

    Redis为单线程 1.Redis 有哪些数据类型 String 哈希 list set 有序set 2.Redis 内部结构 参考:https://www.cnblogs.com/chenpingz ...

  8. 使用openssl 生成网站证书

    *.key是私钥文件 证书通常以.crt为后缀,表示证书文件 CSR(Certificate Signing Request)包含了公钥和名字信息.通常以.csr为后缀,是网站向CA发起认证请求的文件 ...

  9. 关于python的字典操作

    字典和列表的区别: 列表是有序的 字典是无序的 字典使用{}定义 字典使用键值对存储数据,键值对之间使用 “   ,”分隔 键 key 是索引 值 value 是数据 键和值之间使用  “  :”分隔 ...

  10. Python服务端工程师就业面试指导

    Python服务端工程师就业面试指导 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候 ...