题目描述

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

输入输出格式

输入格式:

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. oracle导出dmp文件的2种方法

    使用exp和expdp导出数据 1.exp导出数据命令 exp gd_base/@192.168.13.211/oanet file=D:\export\gd_base.dmp log=D:\expo ...

  2. Beta冲刺NO.6

    Beta冲刺 第六天 1. 昨天的困难 1.对于设计模式的应用不熟悉,所以在应用上出现了很大的困难. 2.SSH中数据库的管理是用HQL语句实现的,所以在多表查询时出现了很大的问题. 3.页面结构太凌 ...

  3. Beta版本敏捷冲刺每日报告——Day3

    1.情况简述 Beta阶段第三次Scrum Meeting 敏捷开发起止时间 2017.11.4 08:00 -- 2017.11.4 22:00 讨论时间地点 2017.11.4晚9:00,软工所实 ...

  4. 敏捷冲刺每日报告五(Java-Team)

    第五天报告(10.29  周日) 团队:Java-Team 成员: 章辉宇(284) 吴政楠(286) 陈阳(PM:288) 韩华颂(142) 胡志权(143) github地址:https://gi ...

  5. 第14、15週PTA題目的處理

    題目1 選擇法排序 1.實驗代碼 #include <stdio.h> #include <stdlib.h> int main() { int n,index,exchang ...

  6. jsonp处理

    def loads_jsonp(self,_jsonp): try: return json.loads(re.match(".*?({.*}).*",_jsonp,re.S).g ...

  7. python 异步协程

    """A very simple co-routine scheduler. Note: this is written to favour simple code ov ...

  8. [Android FrameWork 6.0源码学习] View的重绘过程之WindowManager的addView方法

    博客首页:http://www.cnblogs.com/kezhuang/p/关于Activity的contentView的构建过程,我在我的博客中已经分析过了,不了解的可以去看一下<[Andr ...

  9. Python内置函数(20)——hex

    英文文档: hex(x) Convert an integer number to a lowercase hexadecimal string prefixed with "0x" ...

  10. Nginx负载均衡(架构之路)

    [前言] 在大型网站中,负载均衡是有想当必要的.尤其是在同一时间访问量比较大的大型网站,例如网上商城,新闻等CMS系统,为了减轻单个服务器的处理压力,我们引进了负载均衡这一个概念,将一个服务器的压力分 ...