码迷,mamicode.com
首页 > 其他好文 > 详细

Mountaineers

时间:2018-09-09 12:12:27      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:限制   gif   ems   esc   height   popular   init   and   where   

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][25]={0};
int depth[N],DEPTH,sum=1;

void addedge(int a,int b)
{
    edges[b].push_back((edge){a});
}

void dfs(int x)
{
    for(int i=1;i<=DEPTH;i++)
    {
        grand[x][i]=grand[grand[x][i-1]][i-1];
    }

    for(int i=0;i<edges[x].size();i++)
    {
        int to=edges[x][i].v;
        if(grand[x][0]==to)continue;

        depth[to]=depth[x]+1;
        grand[to][0]=x;
        dfs(to);
    }
}

void init(int s)
{
    DEPTH=floor(log(sum + 0.0) / log(2.0));
    depth[s]=1; //注意根节点的深度不要设为0,否则下面判深度会出错
    memset(grand,0,sizeof(grand));
    dfs(s);
}

int lca(int a,int b)
{
    if(depth[a]>depth[b])swap(a,b);

    for(int i=DEPTH;i>=0;i--)
    if(depth[a]<depth[b]&&depth[grand[b][i]]>=depth[a])
    b=grand[b][i];

    for(int i=DEPTH;i>=0;i--)
    if(grand[a][i]!=grand[b][i])
    {
        a=grand[a][i];
        b=grand[b][i];
    }

    if(a!=b)
    {
        return grand[a][0];
    }
    return a;
}

int pre[300000];

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[300000];
int vis[520][520]={0};
int ans[300000];

int main()
{

    int m,n,q,s;
    scanf("%d %d %d",&m,&n,&q);
    for(int i=0;i<=m*n;i++)pre[i]=i;
    
    for(int i=1;i<=m;i++)
    for(int j=1;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+1,arr+sum);
    for(int i=1;i<sum;i++)
    {
        int x=arr[i].x,y=arr[i].y,num=arr[i].number;
        vis[x][y]=num;
        s=num;
        
        if(x-1>=1&&vis[x-1][y]&&Find(vis[x-1][y])!=num)
        {
            addedge(Find(vis[x-1][y]),num);
            pre[Find(vis[x-1][y])]=num;
        }
        
        if(x+1<=m&&vis[x+1][y]&&Find(vis[x+1][y])!=num)
        {
            addedge(Find(vis[x+1][y]),num);
            pre[Find(vis[x+1][y])]=num;
        }
        
        if(y-1>=1&&vis[x][y-1]&&Find(vis[x][y-1])!=num)
        {
            addedge(Find(vis[x][y-1]),num);
            pre[Find(vis[x][y-1])]=num;
        }
        
        if(y+1<=n&&vis[x][y+1]&&Find(vis[x][y+1])!=num)
        {
            addedge(Find(vis[x][y+1]),num);
            pre[Find(vis[x][y+1])]=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 0;
}
View Code

 

Mountaineers

标签:限制   gif   ems   esc   height   popular   init   and   where   

原文地址:https://www.cnblogs.com/tian-luo/p/9611640.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!