描述

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. 爬虫(三)通过Selenium + Headless Chrome爬取动态网页

    一.Selenium Selenium是一个用于Web应用程序测试的工具,它可以在各种浏览器中运行,包括Chrome,Safari,Firefox 等主流界面式浏览器. 我们可以直接用pip inst ...

  2. non-identifying and identifying

    An identifying relationship means that the child table cannot be uniquely identified without the par ...

  3. sqoop的数据抽取过程记录

    今天公司抽取了4千万的表大概十几G 用sqoop抽取是30--40分钟 开了两个map.模型是oracle----hdfs(hive).以前只抽过几十万级别,所以千万级别感觉还是spilt做好切分和定 ...

  4. 2019-7-9-Roslyn-如何在-Target-引用-xaml-防止文件没有编译

    title author date CreateTime categories Roslyn 如何在 Target 引用 xaml 防止文件没有编译 lindexi 2019-07-09 17:16: ...

  5. AGC035D

    AGC035D Add and Remove 题意 给出\(n\)个数,每次删除一个不在两端的数,然后把它的权值加到相邻的两个数上. 问操作\(n-2\)次后,所剩的两数之和的最小值 \(n\le18 ...

  6. 循环读取文件夹中的图片matlab代码

    参考:http://blog.csdn.net/guoxiaojie_415/article/details/21317323:http://blog.csdn.net/qikaihuting/art ...

  7. nginx、php-fpm启动脚本

    Nginx官方启动脚本 //service nginx stop|start|restart|reloadtouch /etc/init.d/nginx chmod nginxvi /etc/init ...

  8. beanstalkd 消息队列发邮件

    放入消息 /** * 获取beanstalk实例 * * @staticvar resource|bool $beanstalk * @return resource */ function get_ ...

  9. spring cloud深入学习(六)-----熔断监控Hystrix Dashboard和Turbine

    Hystrix-dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard我们可以在直观地看到各Hystrix Command的请求响应时间, 请求成功率等数 ...

  10. Java 类与类之间的调用

    方法1. 新建一个类. 然后在调用类中先进行被调用类实例化,然后通过实例化的对象访问. 例如: //先定义一个类 import static java.lang.System.out; public ...