Mountaineers

时间限制: 3 Sec  内存限制: 128 MB

题目描述

The Chilean Andes have become increasingly popular as a destination for backpacking and hiking. Many parts of the Andes are quite remote and thus dangerous. Because of this, the Ministry of Tourism wants to help travelers plan their trips. In particular, the travelers need to know how high they will have to climb during their journey, as this information will help them decide which equipment they need to bring. The Ministry has tasked you to provide the aspiring mountaineers with this data.
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.

输入

The input consists of:
•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).

输出

Output q integers, the minimal height for each mountaineer, in the same order as in the input.

样例输入

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

来源/分类

GCPC2018


题意:有一个m*n的图,图中每个格子有一个数,每次询问两个格子,问从其中一个格子走到另一个格子所经过的数中,最大的那个数最小是多少。

解法:神奇的建树思想。先把全部格子对权值进行升序排序,遍历所有格子,每次遍历时把这个格子标记为已遍历,并且看看这个格子周围的格子有没有被遍历,若被遍历,则加一条有向边从当前格子指向周围格子的根结点。最后处理询问时,两个格子的lca对应的值就是此次询问的答案。(本人能力有限,给不出证明)。
#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的更多相关文章

  1. Mountaineers Gym - 102021M (LCA+MST)

    题目链接: Mountaineers  Gym - 102021M 题目大意:给你一个n*m的矩阵,a[i][j]代表当前方块的高度,然后每次询问给你一个起点和终点,然后问你在这个图上你选择一条路径, ...

  2. Cheap Hollister Clothing

    (link to hollisterco site), Spectacles don't simply take care of the eye area inside sun; Putting th ...

  3. 2018 German Collegiate Programming Contest (GCPC 18)

    2018 German Collegiate Programming Contest (GCPC 18) Attack on Alpha-Zet 建树,求lca 代码: #include <al ...

  4. Python3自然语言(NLTK)——语言大数据

    NLTK 这是一个处理文本的python库,我们知道文字性的知识可是拥有非常庞大的数据量,故而这属于大数据系列. 本文只是浅尝辄止,目前本人并未涉及这块知识,只是偶尔好奇,才写本文. 从NLTK中的b ...

  5. Gym .102021 .German Collegiate Programming Contest (GCPC 18) (寒假gym自训第三场)

    B .Battle Royale 题意:给你两个点A,B,以及一个圆S,保证两个点在圆外,且其连线与圆相交,求两点间最短距离. 思路:显然是要分别与圆相切,然后在圆弧想走,直到相交. 那么ans=与圆 ...

  6. L164

    “TAKE ONLY memories, leave only footprints” is more than a hiking motto at the Sagarmatha National P ...

随机推荐

  1. 按Home键切换到后台后会触发libGPUSupportMercury.dylib: gpus_ReturnNotPermittedKillClient导致crash

    转自:http://www.eoeandroid.com/thread-251598-1-1.html 好像有很多朋友都碰到过这个问题,即在真机调试时,按hone键返回桌面,再回到app时,app会c ...

  2. Django 表增加外键

    1.创建临时表,并把原表的数据复制到临时表 先根据python manage syl article查看创建临时表 CREATE TABLE `article_article_temp` ( `id` ...

  3. labview密码忘记怎么办,如何破解labview密码,vi密码md5码破解重置

    labview密码忘记了或者需要破解labview密码,可以找到vi文件的md5码,把里面的md5码拿到网站http://cmd5.la解密就可以了. 把vi文件的32位md5码放到网站cmd5.la ...

  4. 图片,二进制,oracle数据库

    图片在oracle数据库中一般以二进制存在,存储类型是blob,然而clob类型一般存储的是大于4000的字符,不能用来存储图像这样的二进制内容,下面展示一下实现图像,二进制,oracle 数据库的应 ...

  5. 剑指offer——把字符串转换成整数(c++)

    题目描述请你写一个函数StrToInt,实现把字符串转换成整数这个功能.当然,不能使用atoi或者其他类似的库函数. 示例 1:输入: " -42"输出: -42解释: 第一个非空 ...

  6. Spring中的事务传播行为与隔离级别

    事务传播行为 事务传播行为(为了解决业务层方法之间互相调用的事务问题): 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己 ...

  7. DaemonSet 案例分析

    本节详细分析两个 k8s 自己的 DaemonSet:kube-flannel-ds 和 kube-proxy . kube-flannel-ds 下面我们通过分析 kube-flannel-ds 来 ...

  8. Asp.Net Core 进阶(三)—— IServiceCollection依赖注入容器和使用Autofac替换它

    Asp.Net Core 提供了默认的依赖注入容器 IServiceCollection,它是一个轻量级的依赖注入容器,所以功能不多,只是提供了基础的一些功能,要实现AOP就有点麻烦,因此在实际工作当 ...

  9. Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameter 标签: 构造器注入Spring

    问题:要么是因为构造方法改变了,要么就是构造方法入参实例化失败(比如没有实现) 问题 在练习spring构造器注入方式的小程序的时候报错: Exception in thread “main” org ...

  10. 第2节 azkaban调度:16、azkaban的介绍以及azkaban的soloserver的安装使用

    2. 工作流调度器azkaban 2.1 概述 azkaban官网: https://azkaban.github.io/ 2.1.1为什么需要工作流调度系统 l  一个完整的数据分析系统通常都是由大 ...