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 ...
随机推荐
- npm 安装删除模块
npm安装模块 [npm install xxx]利用 npm 安装xxx模块到当前命令行所在目录: [npm install -g xxx]利用npm安装全局模块xxx: 本地安装时将模块写入pac ...
- TZ_06_SpringMVC_异常处理,自定义异常
1.SpringMVC异常处理的方式 . 2. 异常处理思路 1>. Controller调用service,service调用dao,异常都是向上抛出的,最终有DispatcherServle ...
- JS倒计时-毫秒
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- fiddler替换服务器上文件进行本地调试
在我们前端开发的日常工作中,发现服务器上某个css/javascript文件有问题,需要修改,那真是家常便饭.通常,我们需要将文件进行修改,然后重新发布再验证,这样就很容易影响到生产环境的稳定性.更普 ...
- solr问题missing content stream
在使用solrj建立索引的时候,报错:missing content stream; 原因在于 HttpSolrServer httpSolrServer = new HttpSolrServer(s ...
- GIT → 10:基于IntelliJ IDEA的Git 操作
GIT → 10:基于IntelliJ IDEA的Git 操作
- Redis数据库在ubuntu16.04下的安装
1.安装 sudo apt-get install redis-server 2.启动 sudo service redis-server start 3.查看 ps aux|grep redis 4 ...
- 2019-8-31-dotnet-Framework-源代码-类库的意思
title author date CreateTime categories dotnet Framework 源代码 类库的意思 lindexi 2019-08-31 16:55:58 +0800 ...
- XtraBackup构建MySQL主从环境的方法
环境:HE3主库,HE1从库HE1:192.168.1.248HE3:192.168.1.250从库my.cnf加入以下参数并重启数据库:read_only=1log_slave_updates=1( ...
- ListView设置的点点滴滴
去掉ListView的分界线 1. ListView的属性Divider设为#FFCC00 这种对任何背景都适用 2. 把ListView的属性Divider设为和背景一样的颜色 3.and ...