题目描述

给定一张地势图,所有的点都被水淹没,现在有一些关键点,要求放最少的水泵使所有关键点的水都被抽干

输入输出格式

输入格式:

In the first line of the standard input there are two integers  and , separated by a single space,. The following  lines contain the description of the map. The 'th linedescribes the 'th row ofunitary squares in the map. It contains  integers , separated by single spaces,.

The number  describes the 'th square of the ![](http://main.edu.pl/images/OI14/pow-en-tex.14.pn

输出格式:

Your programme should write out one integer to the standard output - the minimum number of pumpsneeded to drain Byteburg.

输入输出样例

输入样例#1:

6 9
-2 -2 -1 -1 -2 -2 -2 -12 -3
-2 1 -1 2 -8 -12 2 -12 -12
-5 3 1 1 -12 4 -6 2 -2
-5 -2 -2 2 -12 -3 4 -3 -1
-5 -6 -2 2 -12 5 6 2 -1
-4 -8 -8 -10 -12 -8 -6 -6 -4
输出样例#1:

2
我们首先考虑如果在格子 a 修建一个抽水机,在什么情况下格子 b 的水也可以被抽干。
我们可以发现当且仅当存在一条从 a 到 b 的路径,中间经过的抽水机(包括 a)的高度都不大于 b 的高度。
即h[b]>=max(h[i])
因此我们可以考虑把所有格子的高度从小到大排序,我们把每一个格子建成一个集合。
然后按照海拔高度从小到大扫描格子,对于当前的格子 i,我们找到所有与 i 相邻并且海拔高度不大于格子 i 的格子,
我们发现如果这些格子中的任意一个洪水要是被解决了,那么格子 i 的洪水也可以被解决,所以我们合并这些格子。
对于当前的格子 i,如果它必须被清理且与它相邻的格子集合中没有任何一个被清理,我们则把这个集合的清理状态标记为真,然后答案加 1。
集合和每个格子是否被清理用并查集来维护就可以了。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int s,x,y;
} city[],maze[];
pair<int,int> set[][];
int dx[]={,,-,,};
int dy[]={,,,,-};
int n,m,a[][],tot,cnt,vis[][],ans;
bool cmp(node a,node b)
{
return (a.s<b.s);
}
pair<int,int> find(pair<int,int> x)
{
if (set[x.first][x.second]!=x) set[x.first][x.second]=find(set[x.first][x.second]);
return set[x.first][x.second];
}
void union_set(pair<int,int> x,pair<int,int> y)
{
x=find(x),y=find(y);
set[x.first][x.second]=y;
vis[y.first][y.second]|=vis[x.first][x.second];
}
void exam(int x,int y)
{
for(int i=;i<=;i++)
{
int tx=x+dx[i], ty=y+dy[i];
if(tx<=||ty<=||tx>m||ty>n) continue;
if(a[tx][ty]>a[x][y]) continue;
union_set(make_pair(x, y), make_pair(tx, ty));
}
}
int main()
{
int i,j;
cin>>n>>m;
for (i=; i<=n; i++)
{
for (j=; j<=m; j++)
{
scanf("%d",&a[i][j]);
if (a[i][j]<=)
{
a[i][j]=-a[i][j];
}
else
{
city[++tot]=(node){a[i][j],i,j};
}
maze[++cnt]=(node){a[i][j],i,j};
set[i][j]=make_pair(i,j);
}
}
sort(maze+,maze+cnt+,cmp);
sort(city+,city+tot+,cmp);
for (i=; i<=tot; i++)
{
for (j=; j<=cnt,city[i].s>=maze[j].s; j++)
{
exam(maze[j].x,maze[j].y);
pair<int,int> x=find(make_pair(city[i].x,city[i].y));
if (vis[x.first][x.second]==)
{
ans++;
vis[x.first][x.second]=;
}
}
}
cout<<ans;
}

[POI2007]POW-The Flood的更多相关文章

  1. [洛谷3457][POI2007]POW-The Flood

    洛谷题目链接:[POI2007]POW-The Flood 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方 ...

  2. 洛谷P3457 [POI2007]POW-The Flood [并查集,模拟]

    题目传送门 pow 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是 ...

  3. [POI2007]洪水pow 题解

    [POI2007]洪水pow 时间限制: 5 Sec  内存限制: 128 MB 题目描述 AKD市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD市全被水淹没了.Blue Mary,AKD ...

  4. [POI2007]POW-The Flood(并查集)

    [POI2007]POW-The Flood Description AKD 市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD 市全被水淹没了.Blue Mary,AKD 市的市长,召集了 ...

  5. P3457 [POI2007]POW-The Flood

    题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是否是该市的一个组成部 ...

  6. bzoj1104: [POI2007]洪水pow

    #include <iostream> #include <cstdio> #include <cmath> #include <cstring> #i ...

  7. 【BZOJ】1104: [POI2007]洪水pow

    题意 给一个\(n * m(1 \le n, m \le 1000)\)的矩阵,如果\(a_{i, j}\)为正表示城市.\(|a_{i, j}|(|a_{i, j}| \le 1000)\)是格子\ ...

  8. [POI2007]洪水pow 并查集

    我们先得出一个结论:水泵要建在城市上.因为如果在非城市上建能把其他一些城市抽干,那么在城市上建也是一个效果(自己画图感性理解一下) 然后我们明白抽水的条件:周围的高度要>=自身的高度,这样会抽完 ...

  9. Luogu345: [POI2007]POW-The Flood

    题意 见luogu Sol 贪心 从小到大枚举高度,把小于等于这一高度的相邻格子用并查集合并 那么这个集合内的所有格子都一定可以由这个集合内的一个最低点抽完水 那么合并之后(一定要在合并之后) 判断这 ...

随机推荐

  1. Git简单图文教程

    环境: Windows [版本 10.0.15063]64位 Git-2.14.1 64位[下载] TortoiseGit-2.5.0.0 64位[下载],这是一个Git 客户端,外号"乌龟 ...

  2. decltype操作符

    关于decltype操作符的说明: 1.在C++中,decltype作为操作符,用于查询表达式的数据类型.decltype在C++11标准制定时引入,主要是为泛型编程而设计,以解决泛型编程中,由于有些 ...

  3. 自主学习之RxSwift(二) -----flatMap

    最近项目中有这么一个需求,下面是三个网络请求 A.从服务器获取到时间戳(GET 方法,获取 timeLine) B.进行用户头像上传,获得回传的URL(POST方法,参数为 userId, timeL ...

  4. idea搭建springdata+mongodb+maven+springmvc

    idea搭建springdata+mongodb+maven+springmvc 今天我们来学习一下SpringData操作MongoDB. 项目环境:IntelliJ IDEA2017+maven3 ...

  5. HAOI 2012 高速公路

    https://www.luogu.org/problem/show?pid=2221 题目描述 Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这 ...

  6. 常用的 html 标签及注意事项

    <a> 标签 用法:用于定义超链接 清除浏览器默认样式: a { text-decoration: none;/* 去除下划线 */ color: #333;/* 改变链接颜色 */ } ...

  7. postcss的安装与使用

    我是经过公司另外一个同事推荐的这个 他是一个资深的大哥哥  我觉得我确实需要跟多的学习和成长 而且我觉得我应该听他的话 多学学新知识 最近一直在做适配的网站 会出现很多媒体查询 我发现用这个写媒体查询 ...

  8. Ubuntu 17.10.1安装, 定制

    p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 2018.4.7 Ubuntu 17.10.1安装, 定制, 后续搭建LAMP环境 ...

  9. Nokia大事录

    1994年,接通中国第一个GSM电话. 1995年,接通中国第一个无线数据电话. 1996年,接通中国第一个GSM1800网络电话.首家推出同时支持简繁中文短讯的移动电话--诺基亚8110.  199 ...

  10. Leetcode:Two Sum

    原题:https://leetcode.com/problems/two-sum/ 尝试了两种方法: 方法一: var twoSum = function(nums, target) { for(va ...