【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)
http://www.lydsy.com/JudgeOnline/problem.php?id=3432
题目说要相互可达,但是只需要从某个点做bfs然后判断其它点是否可达即可。
原因太简单了。。。。。因为它是abs
所以我们二分D,然后判断即可
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr2(a, b, c) for1(i, 1, b) { for1(j, 1, c) cout << a[i][j]; cout << endl; }
#define printarr1(a, b) for1(i, 1, b) cout << a[i]; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=505, M=300005, dx[]={-1, 1, 0, 0}, dy[]={0, 0, 1, -1};
int mp[N][N], must[N][N], n, m, front, tail, vis[N][N], X, Y;
struct dat{ int x, y; }q[M]; void bfs(int D) {
for1(i, 1, n) for1(j, 1, m) vis[i][j]=0;
front=tail=0;
q[tail].x=X, q[tail++].y=Y; vis[X][Y]=1;
int x, y;
while(tail!=front) {
dat &t=q[front++]; if(front==M) front=0;
x=t.x, y=t.y;
rep(i, 4) {
int fx=dx[i]+x, fy=dy[i]+y;
if(fx<1 || fy<1 || fx>n || fy>m || vis[fx][fy] || abs(mp[fx][fy]-mp[x][y])>D) continue;
vis[fx][fy]=1;
q[tail].x=fx; q[tail++].y=fy; if(tail==M) tail=0;
}
}
}
bool check(int D) {
bfs(D);
for1(i, 1, n) for1(j, 1, m) if(must[i][j]==1 && !vis[i][j]) return false;
return true;
} int main() {
int mx=0;
read(n); read(m);
for1(i, 1, n) for1(j, 1, m) read(mp[i][j]), mx=max(mx, mp[i][j]);
for1(i, 1, n) for1(j, 1, m) {
read(must[i][j]); if(must[i][j]==1) X=i, Y=j;
}
int l=0, r=mx;
while(l<=r) {
int mid=(l+r)>>1;
if(check(mid)) r=mid-1;
else l=mid+1;
}
print(r+1);
return 0;
}
Description
The cross-country skiing course at the winter Moolympics is described by an M x N grid of elevations (1 <= M,N <= 500), each elevation being in the range 0 .. 1,000,000,000. Some of the cells in this grid are designated as waypoints for the course. The organizers of the Moolympics want to assign a difficulty rating D to the entire course so that a cow can reach any waypoint from any other waypoint by repeatedly skiing from a cell to an adjacent cell with absolute elevation difference at most D. Two cells are adjacent if one is directly north, south, east, or west of the other. The difficulty rating of the course is the minimum value of D such that all waypoints are mutually reachable in this fashion.
N*M的格子,每个格子都有一个分值v,有的格子一定要经过.两个格子i,j可以互相到达,当且仅当它们有一条边重复(即上下左右方向),且abs(vi-vj)<=D.
Input
* Line 1: The integers M and N.
* Lines 2..1+M: Each of these M lines contains N integer elevations.
* Lines 2+M..1+2M: Each of these M lines contains N values that are
either 0 or 1, with 1 indicating a cell that is a waypoint.
Output
* Line 1: The difficulty rating for the course (the minimum value of D
such that all waypoints are still reachable from each-other).
Sample Input
20 21 18 99 5
19 22 20 16 26
18 17 40 60 80
1 0 0 0 1
0 0 0 0 0
0 0 0 0 1
INPUT DETAILS: The ski course is described by a 3 x 5 grid of
elevations. The upper-left, upper-right, and lower-right cells are
designated as waypoints.
Sample Output
OUTPUT DETAILS: If D = 21, the three waypoints are reachable from
each-other. If D < 21, then the upper-right waypoint cannot be
reached from the other two.
HINT
Source
【BZOJ】3432: [Usaco2014 Jan]Cross Country Skiing (bfs+二分)的更多相关文章
- BZOJ 3432: [Usaco2014 Jan]Cross Country Skiing (二分+染色法)
还是搜索~~可以看出随着D值的增大能到达的点越多,就2分d值+染色法遍历就行啦~~~ CODE: #include<cstdio>#include<iostream>#incl ...
- 洛谷 题解 P4955 【[USACO14JAN]Cross Country Skiing 越野滑雪】
二分+DFS 看到这么多大佬写了并查集,BFS的,还没有人写DFS版的,那么肯定是要来水水积分的啦毕竟这可是道伪紫题呢! 做法楼上楼下也讲得很清楚了吧,详见代码的注释 #include<bits ...
- BZOJ 3430: [Usaco2014 Jan]Ski Course Rating(并查集+贪心)
题面 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 136 Solved: 90 [Submit][Status][Discuss] Descript ...
- BZOJ 3433 [Usaco2014 Jan]Recording the Moolympics:贪心
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3433 题意: 给出n个区间[a,b). 有两个记录器,每个记录器中存放的区间不能重叠. 求 ...
- bzoj 1594: [Usaco2008 Jan]猜数游戏【二分+线段树】
写错一个符号多调一小时系列-- 二分答案,然后判断这个二分区间是否合法: 先按值从大到小排序,然后对于值相同的一些区间,如果没有交集则不合法:否则把并集在线段树上打上标记,然后值小于这个值的区间们,如 ...
- BZOJ 1614 [Usaco2007 Jan]Telephone Lines架设电话线 (二分+最短路)
题意: 给一个2e4带正边权的图,可以免费k个边,一条路径的花费为路径上边权最大值,问你1到n的最小花费 思路: 对于一个x,我们如果将大于等于x的边权全部免费,那么至少需要免费的边的数量就是 “设大 ...
- BZOJ3433: [Usaco2014 Jan]Recording the Moolympics
3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 55 So ...
- 3433: [Usaco2014 Jan]Recording the Moolympics
3433: [Usaco2014 Jan]Recording the Moolympics Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 137 S ...
- [bzoj 3048] [Usaco2013 Jan]Cow Lineup
[bzoj 3048] [Usaco2013 Jan]Cow Lineup Description 给你一个长度为n(1<=n<=100,000)的自然数数列,其中每一个数都小于等于10亿 ...
随机推荐
- Shell 基本运算符(转)
Shell 和其他编程语言一样,支持多种运算符,包括: 算数运算符 关系运算符 布尔运算符 字符串运算符 文件测试运算符 原生bash不支持简单的数学运算,但是可以通过其他命令来实现,例如 awk 和 ...
- CXF调用方式——使用wsdl2java(Windwos下)
1.自动生成客户端代码: 先把CXF下到本地,本例中我下的是apache-cxf-3.1.2,然后在命令行里到相应路径执行命令: D:\soft\DevelopSoft\apache-cxf-\bin ...
- 无需超级用户mpi多机运行
在之前的一篇博文中(Linux下mpi环境配置与运行步骤(Ubuntu为例) ),有讨论过怎样使用MPI在两个不同的机器上运行程序,在那篇博文中使用了超级用户权限.不幸的是.有些情况下,我们不能拥有操 ...
- 网络请求框架---Volley
去年的Google I/O大会为android开发者带来了一个网络请求框架,它的名字叫做Volley.Volley诞生的使命就是让Android的网络请求更快,更健壮,而且它的网络通信的实现是基于Ht ...
- C# DateTime的11种构造函数 [Abp 源码分析]十五、自动审计记录 .Net 登陆的时候添加验证码 使用Topshelf开发Windows服务、记录日志 日常杂记——C#验证码 c#_生成图片式验证码 C# 利用SharpZipLib生成压缩包 Sql2012如何将远程服务器数据库及表、表结构、表数据导入本地数据库
C# DateTime的11种构造函数 别的也不多说没直接贴代码 using System; using System.Collections.Generic; using System.Glob ...
- java 解析webservice 中的soapheader
//从MessageContet中获取头域中的值 public HeaderBean getBeanFromRequest(org.apache.axis2.context.MessageContex ...
- C# 中字符串string和字节数组byte[]的转换
string转byte[]: byte[] byteArray = System.Text.Encoding.Default.GetBytes ( str ); byte[]转string: stri ...
- 点滴积累【JS】---JS小功能(列表页面隔行变色)
效果: 代码: <head runat="server"> <title></title> <script type="text ...
- HR问“预期薪资是多少”,这么说能加薪zz
每年过完节,收好上一年的年终奖,身边人就开始蠢蠢欲动,招聘市场也异常火爆,节前各种裁员的新闻,过了个节都变成了“我们还要继续招人”. 年景不好,人才更是成了紧俏货.可现实中,我却发现,优质的人才未必能 ...
- NIO - Selector源码分析
1. 背景 SelectableChannel对象的多路复用器. 可以通过调用Selector.open()方法创建Selector对象.Selector.open()方法会利用系统默认的Select ...