题目描述

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

输入输出格式

输入格式:

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. Beta冲刺NO.7

    Beta冲刺 第七天 昨天的困难 昨天的困难在一些多表查询上,不熟悉hibernate的套路,走了很多弯路. 第一次使用图表插件,在图表的显示问题上花了一定的时间. 对于页面绑定和后台数据自动填充的理 ...

  2. fs 创建文件夹

    var http = require("http"); var fs = require("fs"); var server = http.createServ ...

  3. 创建带缩进的XML

    from xml.etree import ElementTree as ET from xml.dom import minidom root = ET.Element('}) son=ET.Sub ...

  4. 将数组写入Plist文件中

    -(void)writeToPlist:(NSArray *)uploadingfiles  Name:(NSString *)name {                  NSMutableArr ...

  5. 项目Beta冲刺Day4

    项目进展 李明皇 今天解决的进度 因服务器端未完成登录态维护,故无法进行前后端联动. 明天安排 前后端联动调试 林翔 今天解决的进度 因上课和实验室事务未完成登录态维护 明天安排 完成登录态维护 孙敏 ...

  6. 解决忽略VScode中Python插件pylint报错的问题

    pylint是VScode中python自带的插件,可以帮助代码规范,美观. 但是有些报错是你不想看到的,你可以选择性的忽略. 例如,在re.compile()中,可以添加参数re.S使. 匹配任意字 ...

  7. LeetCode & Q66-Plus One-Easy

    Array Description: Given a non-negative integer represented as a non-empty array of digits, plus one ...

  8. Mock API是如何在开发中发光发热的?

    在长期的服务过程中,我们经常会遇到前来咨询的用户与我们反馈以下这种情况:咨询者是一个前端人员,在项目开发的过程中需要与后端进行对接,遇到后端还没完成数据输出的情况下,他只好写静态模拟数据,在遇到大型项 ...

  9. Docker学习笔记 - Docker Compose 脚本命令

    Docker Compose 配置文件包含 version.services.networks 三大部分,最关键的是 services 和 networks 两个部分, version: '2' se ...

  10. python爬虫requests的使用

    1 发送get请求获取页面 import requests # 1 要爬取的页面地址 url = 'http://www.baidu.com' # 2 发送get请求 拿到响应 response = ...