Mountaineers
Mountaineers
时间限制: 3 Sec 内存限制: 128 MB
题目描述
You are given a topographic map of a part of the Andes, represented as a two-dimensional grid of height values, as well as the list of origins and destinations. Mountaineers can move from each grid cell to any of the four adjacent cells. For each mountaineer find the minimal height that they must be able to reach in order to complete their journey.
输入
•one line with three integers m, n and q (1 ≤ m, n ≤ 500, 1 ≤ q ≤ 105), where m is the number of rows, n is the number of columns, and q is the number of mountaineers;
•m lines, each with n integers h1, ... , hn (1 ≤ hi ≤ 106), the height values in the map;
•q lines, each with four integers x1, y1, x2, y2 (1 ≤ x1, x2 ≤ m, 1 ≤ y1, y2 ≤ n), describing a mountaineer who wants to trek from (x1, y1) to (x2, y2).
The top left cell of the grid has coordinates (1, 1) and the bottom right cell has coordinates (m, n).
输出
样例输入
3 5 3
1 3 2 1 3
2 4 5 4 4
2 1 3 2 2
1 1 3 2
2 4 2 2
1 4 3 4
样例输出
2
4
3
来源/分类
题意:有一个m*n的图,图中每个格子有一个数,每次询问两个格子,问从其中一个格子走到另一个格子所经过的数中,最大的那个数最小是多少。
#include<bits/stdc++.h>
#define N 250005
using namespace std; struct edge{int v,w;};
vector<edge>edges[N];
int grand[N][]={};
int depth[N],DEPTH,sum=; void addedge(int a,int b)
{
edges[b].push_back((edge){a});
} void dfs(int x)
{
for(int i=;i<=DEPTH;i++)
{
grand[x][i]=grand[grand[x][i-]][i-];
} for(int i=;i<edges[x].size();i++)
{
int to=edges[x][i].v;
if(grand[x][]==to)continue; depth[to]=depth[x]+;
grand[to][]=x;
dfs(to);
}
} void init(int s)
{
DEPTH=floor(log(sum + 0.0) / log(2.0));
depth[s]=; //注意根节点的深度不要设为0,否则下面判深度会出错
memset(grand,,sizeof(grand));
dfs(s);
} int lca(int a,int b)
{
if(depth[a]>depth[b])swap(a,b); for(int i=DEPTH;i>=;i--)
if(depth[a]<depth[b]&&depth[grand[b][i]]>=depth[a])
b=grand[b][i]; for(int i=DEPTH;i>=;i--)
if(grand[a][i]!=grand[b][i])
{
a=grand[a][i];
b=grand[b][i];
} if(a!=b)
{
return grand[a][];
}
return a;
} int pre[]; int Find(int x)
{
int boss=x;
while(boss!=pre[boss])boss=pre[boss]; int temp;
while(x!=pre[x])
{
temp=pre[x];
pre[x]=boss;
x=temp;
}
return boss;
} struct ss
{
int x,y,value,number; bool operator < (const ss &s) const
{
return value<s.value;
} };
ss arr[];
int vis[][]={};
int ans[]; int main()
{ int m,n,q,s;
scanf("%d %d %d",&m,&n,&q);
for(int i=;i<=m*n;i++)pre[i]=i; for(int i=;i<=m;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&arr[sum].value);
arr[sum].x=i;
arr[sum].y=j;
arr[sum].number=sum;
ans[sum]=arr[sum].value;
sum++;
} sort(arr+,arr+sum);
for(int i=;i<sum;i++)
{
int x=arr[i].x,y=arr[i].y,num=arr[i].number;
vis[x][y]=num;
s=num; if(x->=&&vis[x-][y]&&Find(vis[x-][y])!=num)
{
addedge(Find(vis[x-][y]),num);
pre[Find(vis[x-][y])]=num;
} if(x+<=m&&vis[x+][y]&&Find(vis[x+][y])!=num)
{
addedge(Find(vis[x+][y]),num);
pre[Find(vis[x+][y])]=num;
} if(y->=&&vis[x][y-]&&Find(vis[x][y-])!=num)
{
addedge(Find(vis[x][y-]),num);
pre[Find(vis[x][y-])]=num;
} if(y+<=n&&vis[x][y+]&&Find(vis[x][y+])!=num)
{
addedge(Find(vis[x][y+]),num);
pre[Find(vis[x][y+])]=num;
} } init(s); while(q--)
{
int a,b,c,d;
scanf("%d %d %d %d",&a,&b,&c,&d);
a--;
c--;
printf("%d\n",ans[lca(a*n+b,c*n+d)]);
}
return ;
}
Mountaineers的更多相关文章
- Mountaineers Gym - 102021M (LCA+MST)
题目链接: Mountaineers Gym - 102021M 题目大意:给你一个n*m的矩阵,a[i][j]代表当前方块的高度,然后每次询问给你一个起点和终点,然后问你在这个图上你选择一条路径, ...
- Cheap Hollister Clothing
(link to hollisterco site), Spectacles don't simply take care of the eye area inside sun; Putting th ...
- 2018 German Collegiate Programming Contest (GCPC 18)
2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...
- Python3自然语言(NLTK)——语言大数据
NLTK 这是一个处理文本的python库,我们知道文字性的知识可是拥有非常庞大的数据量,故而这属于大数据系列. 本文只是浅尝辄止,目前本人并未涉及这块知识,只是偶尔好奇,才写本文. 从NLTK中的b ...
- Gym .102021 .German Collegiate Programming Contest (GCPC 18) (寒假gym自训第三场)
B .Battle Royale 题意:给你两个点A,B,以及一个圆S,保证两个点在圆外,且其连线与圆相交,求两点间最短距离. 思路:显然是要分别与圆相切,然后在圆弧想走,直到相交. 那么ans=与圆 ...
- L164
“TAKE ONLY memories, leave only footprints” is more than a hiking motto at the Sagarmatha National P ...
随机推荐
- EL1008E: Property or field 'timestamp' cannot be found on object of type 'java.util.HashMap
2018-06-22 09:50:19.488 INFO 20096 --- [nio-8081-exec-2] o.a.c.c.C.[Tomcat].[localhost].[/] : ...
- ios invalid put policy encoding 七牛上传报错
获取七牛token的时候deadline不能为NSString类型 NSDictionary *infoDic = @{@"scope":@"yangtao", ...
- make 与makefile(会不会写 makefile,从一个侧面说明了一个人是否具备完成大型工程的能力。)
跟我一起写 Makefile /**/ 陈皓 (CSDN) 概述 —— 什么是makefile?或许很多Winodws的程序员都不知道这个东西,因为那些Windows的IDE都为你做了这个工作,但我觉 ...
- UVA 1412 Fund Management (预处理+状压dp)
状压dp,每个状态可以表示为一个n元组,且上限为8,可以用一个九进制来表示状态.但是这样做用数组开不下,用map离散会T. 而实际上很多九进制数很多都是用不上的.因此类似uva 1601 Mornin ...
- Spring中的事务传播行为与隔离级别
事务传播行为 事务传播行为(为了解决业务层方法之间互相调用的事务问题): 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己 ...
- 由DAG到背包问题——记忆化搜索和递推两种解法
一.问题描述 物品无限的背包问题:有n种物品,每种均有无穷多个.第 i 种物品的体积为Vi,重量为Wi.选一些物品装到一个容量为 C 的背包中,求使得背包内物品总体积不超过C的前提下重量的最大值.1≤ ...
- [POJ]1111 Image Perimeters
Description Technicians in a pathology lab analyze digitized images of slides. Objects on a slide ar ...
- mysql基本优化
文件打开数 show status like "%Open_files%" Open_files 133 show VARIABLES like "%open_files ...
- DateFormat的format()方法线程不安全的问题分析
最近看到<侦探剧场:堆内存神秘溢出事件>https://my.oschina.net/u/2368090/blog/1628720,于是自己也想测试了解一下DateFormat的多线程安全 ...
- Python3 安装pip 提示ModuleNotFoundError: No module named 'distutils.util'
环境ubutun14,python版本是python3.6. 今天在安装Pip 时出现ModuleNotFoundError: No module named 'distutils.util'.操作步 ...