TZOJ 4471: Postman FJ (二分+bfs)
描述
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)的更多相关文章
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- hdu-5652 India and China Origins(二分+bfs判断连通)
题目链接: India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K ...
- TZOJ 1594 Optimal Milking(二分+最大流)
描述 FJ has moved his K (1 <= K <= 30) milking machines out into the cow pastures among the C (1 ...
- TopCoder SRM 642 Div.2 1000 --二分+BFS
题意: 给你一张图,N个点(0~N-1),m条边,国王要从0到N-1,国王携带一个值,当走到一条边权大于此值的边时,要么不走,要么提升该边的边权,提升k个单位花费k^2块钱,国王就带了B块钱,问能携带 ...
- 【BZOJ】1189: [HNOI2007]紧急疏散evacuate(二分+bfs+网络流)
http://www.lydsy.com/JudgeOnline/problem.php?id=1189 表示完全不会QAQ.... 于是膜拜题解orz 二分时间........... 于是转换成判定 ...
- hiho_1139_二分+bfs搜索
题目 给定N个点和M条边,从点1出发,到达点T.寻找路径上边的个数小于等于K的路径,求出所有满足条件的路径中最长边长度的最小值. 题目链接:二分 最小化最大值,考虑采用二分搜索.对所有的边长进 ...
- hdu 5652 India and China Origins 二分+bfs
题目链接 给一个图, 由01组成, 1不能走. 给q个操作, 每个操作将一个点变为1, 问至少多少个操作之后, 图的上方和下方不联通. 二分操作, 然后bfs判联通就好了. #include < ...
- poj 3501 Escape from Enemy Territory 预处理+二分+bfs
传送门 给一个起点一个终点, 给出整个地图的宽和高, 给出n个敌人的坐标. 让你找到一条路径, 这条路径上的点距离所有敌人的距离都最短, 输出最短距离. 首先预处理出来地图上的所有点到敌人的最短距离, ...
- POJ 2110 Mountain Walking 二分+bfs
传送门 昨天看到这个题还以为是个脑残的dp, 然而脑残的是我. 题目意思就是从左上角走到右下角, 设x为路径上的最大值-最小值, 求x的最小值. 二分x, 对于每一个x, 枚举下界lower, low ...
随机推荐
- Git同平台下多个账号配置
在公司要使用公司和自己的两个账号都往GitHub上面提交,所以整理成笔记 具体配置项 StrictHostKeyChecking no UserKnownHostsFile /dev/null # 为 ...
- python intern(字符串驻留机制)
python 中为了提高字符串的使用用效率和节约内存,对于由 字母.数字.下划线组成的标识符采用了 intern 机制,即对于短字符串,将其赋值给多个对象时,内存中只有一个副本,多个对象共享这个副本. ...
- 利用TensorFlow识别手写的数字---基于两层卷积网络
1 为什么使用卷积神经网络 Softmax回归是一个比较简单的模型,预测的准确率在91%左右,而使用卷积神经网络将预测的准确率提高到99%. 2 卷积网络的流程 3 代码展示 # -*- coding ...
- umask权限使用
很显然,系统中各种文件的权限设置对特定用户的数据安全有很大影响.但是要求用户逐一明确设置系统中每个文件的权限也是不现实的,为此,需要使用umask命令,该命令可以为用户账号中新文件的创建进行缺省设置. ...
- leetcode 57 Insert Interval & leetcode 1046 Last Stone Weight & leetcode 1047 Remove All Adjacent Duplicates in String & leetcode 56 Merge Interval
lc57 Insert Interval 仔细分析题目,发现我们只需要处理那些与插入interval重叠的interval即可,换句话说,那些end早于插入start以及start晚于插入end的in ...
- 【python之路40】Python 作用域
python的作用域与javaScript是一样的,参考:http://www.cnblogs.com/sunshuhai/p/9112578.html 一.python是以函数作为作用域的 if 1 ...
- TZ_06_SpringMVC_传统文件上传和SpringMVC文件上传方式
1.传统文件上传方式 <!-- 文件上传需要的jar --> <dependency> <groupId>commons-fileupload</groupI ...
- ubuntu中vi下删除键和上下左右键输入字符异常(ABCD)
刚安装的Ubuntu系统,使用vi编辑文本的时候, 出现以下现象: 点删除键输入了 D 回车无效 上下左右为字母 光标乱跳 原因: 自带的vi功能问题 解决: 卸载原有vi,重新安装完整版本vim 执 ...
- 字符串无法分割 split无效: java split()使用“.” “\” "|" "*" "+"要转义
.是特殊字符 特殊字符需要转义. 改成split(“\\.”)
- Python学习(二) 基础语法之初看python
Python 标识符 略 Python保留字符 一大堆,说了未必记得住,编码过程中慢慢去记住. 行和缩进 这个要说一下,学习Python与其他语言最大的区别就是,Python的代码块不使用大括号({} ...