[洛谷3457][POI2007]POW-The Flood
洛谷题目链接:[POI2007]POW-The Flood
题意翻译
Description 你手头有一张该市的地图。这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形。对于每个小正方形,地图上已经标注了它的海拔高度以及它是否是该市的一个组成部分。地图上的所有部分都被水淹没了。并且,由于这张地图描绘的地面周围都被高山所环绕,洪水不可能自动向外排出。显然,我们没有必要抽干那些非该市的区域。
每个巨型抽水机可以被放在任何一个1∗1正方形上。这些巨型抽水机将持续地抽水直到这个正方形区域里的水被彻底抽干为止。当然,由连通器原理,所有能向这个格子溢水的格子要么被抽干,要么水位被降低。每个格子能够向相邻的格子溢水,“相邻的”是指(在同一高度水平面上的射影)有公共边。
Input
第一行是两个数m,n(1<=m,n<=1000).
以下 m 行,每行 n 个数,其绝对值表示相应格子的海拔高度;若该数为正,表示它是该市的一个区域;否则就不是。
请大家注意:所有格子的海拔高度其绝对值不超过 1000 ,且可以为零.
Output
只有一行,包含一个整数,表示至少需要放置的巨型抽水机数目。
感谢@FlashHu 提供的翻译
题目描述
Byteburg, the capital of Byteotia, is a picturesque city situated in a valley in the midst of mountains. Unfortunately, recent heavy rainfall has caused a flood - all the Byteburg is now completely under water. Byteasar, the king of Byteotia, has summoned his most enlightened advisors, including you, to a council. After long deliberations the council agreed to bring a few pumps, set them up in the flooded area and drain Byteburg.
The king has asked you to determine the minimum number of pumps sufficing to drain the city.
You are provided with a map of the city and the valley it is situated in. The map is in the shape of a m×nm\times nm×n rectangle, divided into unitary squares. For each such square the map tells its height above sea level and alsowhether it is a part of Byteburg or not. The whole area depicted in the map is under water. Furthermore, it issurrounded by much higher mountains, making the outflow of water impossible. Obviously, there is no needto drain the area that does not belong to Byteburg.
Each pump can be placed in any unitary square depicted in the map. The pump will be drawing thewater until its square is completely drained. Of course, the communicating tubes principle makes its work, so draining one square results in lowering the water level or complete draining of those squares from which the water can flow down to the one with the pump. Water can flow only between squares with a common side (or, more exact, squares whose projections onto horizontal plane have a common side, since the squares may be at different level). Apart from that, the water obviously only flows down.
Task
Write a programme that:
reads description of the map from the standard input,
determines the minimum number of pumps needed to drain whole Byteburg,
writes out the outcome to the standard output.
给定一张地势图,所有的点都被水淹没,现在有一些关键点,要求放最少的水泵使所有关键点的水都被抽干
输入输出格式
输入格式:
In the first line of the standard input there are two integers mmm and nnn , separated by a single space, 1≤n,m≤1 0001 \le n, m \le 1\ 0001≤n,m≤1 000 . The following mmm lines contain the description of the map. The (i+1)(i+1)(i+1) 'th line describes the iii 'th row of unitary squares in the map. It contains nnn integers xi,1,xi,2,...,xinx_{i,1}, x_{i,2}, ..., x_{i_n}xi,1,xi,2,...,xin , separated by single spaces, −1 000≤xi,j≤1 000-1\ 000 \le x_{i,j} \le 1\ 000−1 000≤xi,j≤1 000 , xi,j≠1000x_{i,j} \ne 1000xi,j≠1000 . The number xi,jx_{i,j}xi,j describes the jjj 'th square of the iii 'th line. The ground level in this square is ∣xi,j∣|x_{i,j}|∣xi,j∣ above sea level. If xi,j>0x_{i,j} > 0xi,j>0 , then the square is part of Byteburg, otherwise it is outside the city. Notice, that the area of Byteburg need not be connected. In fact the city may have several separate parts.
输出格式:
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
一句话题意: 一个\(n*m\)的矩阵,矩阵上的值代表着该位置上的海拔高度(如果是负数则说明不再城市中),可以认为一开始水在无限高的地方,你需要在一些地方放水泵使得所有城市的位置的水位等于海拔高度.其中水可以向上下左右四个方向海拔比它低的地方流.
题解: 一开始看题目看了好久没看懂样例,后来看了题解才知道是像物理中一个连通器一样的东西.对于一个城市要使它的水位等于海拔高度,那么就至少要有个水泵在这个高度,或者更低,并且要和这个城市是连通的,也就是来他们两点的路径中没有一个位置可以阻挡这个水流.
那么该如何求出这个最小个数呢?我们考虑海拔最低的城市,显然这个高度是需要一个水泵的.
然后对于每一个位置都可以向上下左右四个方向扩展一格,那么最终统一高度的格子会形成一个连通块,那么对于这些连通块只要有一个水泵,那么这个连通块内的所有格子的水都可以被抽干.
所以我们可以想到用一个并查集来维护每个格子间的连通关系,并且通过水流的流向来不断将海拔较低的位置向海拔较高的位置更新,也就是说,海拔较低的位置有一个水泵,并且另一个海拔较高的位置与它相连通的话,海拔高的位置上的水也会被抽干.所以在维护并查集的同时再维护一下每个集合内是否含有水泵的情况就可以了.
然后因为水是从高向低流的,所以肯定要先考虑海拔较低的位置,然后才能用海拔较低的位置更新海拔高的位置.所以我们先将所有的点按照高度排序,然后按顺序按海拔从低到高维护连通关系,就可以了.
#include<bits/stdc++.h>
using namespace std;
const int N=1000+5;
const int inf=2147483647;
int n, m, cnt = 0, ans = 0, vis[N][N], fa[N*N], have[N*N], hi[N][N], mv[] = {1, 0, -1, 0, 1};
// have 维护的是一些点连通的并查集内是否有水泵
// vis 维护的是某个位置所对应的节点编号(排序前)
// fa 维护并查集的连通关系
// hi 维护某个位置的高度
struct pos{
int x, y, h, c;
}a[N*N];
inline int gi(){
int ans = 0, f = 1; char i = getchar();
while(i>'9' || i<'0'){ if(i == '-') f = -1; i = getchar(); }
while(i>='0' && i<='9') ans = ans*10+i-'0', i = getchar();
return ans * f;
}
inline bool cmp(pos a, pos b){
return a.h < b.h;
}
inline int find(int x){
return fa[x] == x ? x : fa[x] = find(fa[x]);
}
inline void comb(int x, int y){
int r1 = find(x), r2 = find(y);
if(r1 == r2) return;
fa[r2] = r1, have[r1] |= have[r2];// r1是海拔高的位置
// 所以只要海拔低的地方有抽水机, 海拔高的地方就有
}
int main(){
// freopen("pow.in", "r", stdin);
// freopen("pow.out", "w", stdout);
int x, y; n = gi(), m = gi();
memset(hi, 127, sizeof(hi));
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
a[++cnt].x = i, a[cnt].y = j, a[cnt].h = gi(), vis[i][j] = cnt;
if(a[cnt].h <= 0) a[cnt].h = -a[cnt].h, a[cnt].c = 0;
else a[cnt].c = 1;
hi[i][j] = a[cnt].h;
}
}
for(int i=1;i<=cnt;i++) fa[i] = i;
sort(a+1, a+cnt+1, cmp);
for(int i=1;i<=cnt;i++){
x = a[i].x, y = a[i].y;
for(int j=0;j<4;j++){
int nx = x+mv[j], ny = y+mv[j+1];
if(a[i].h >= hi[nx][ny]) comb(vis[x][y], vis[nx][ny]);
} // 排序后用vis[x][y]是避免排序后编号发生变化,就与之前所对应的编号相连通
if(a[i].h != a[i+1].h){ // 将同一高度的一起处理
int j = i;
while(a[i].h == a[j].h){
if(a[j].c && have[find(vis[a[j].x][a[j].y])] == 0){
have[find(vis[a[j].x][a[j].y])] = 1, ans++;
}
j--;
}
}
}
cout << ans << endl;
return 0;
}
这道题因为我没有考虑排序后编号的改变导致我调了一个多小时
[洛谷3457][POI2007]POW-The Flood的更多相关文章
- [洛谷P3460] [POI2007]TET-Tetris Attack
洛谷题目链接:[POI2007]TET-Tetris Attack 题目描述 A puzzle called "Tetris Attack" has lately become a ...
- 洛谷P3459 [POI2007]MEG-Megalopolis(树链剖分,Splay)
洛谷题目传送门 正解是树状数组维护dfn序上的前缀和,这样的思路真是又玄学又令我惊叹( 我太弱啦,根本想不到)Orz各路Dalao 今天考了这道题,数据范围还比洛谷的小,只有\(10^5\)(害我复制 ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [树链剖分]
题目传送门 MEG 题目描述 Byteotia has been eventually touched by globalisation, and so has Byteasar the Postma ...
- 洛谷 P3462 [POI2007]ODW-Weights
题面: https://www.luogu.org/problemnew/show/P3462 https://www.lydsy.com/JudgeOnline/problem.php?id=111 ...
- 洛谷P3457 [POI2007]POW-The Flood [并查集,模拟]
题目传送门 pow 题意翻译 Description 你手头有一张该市的地图.这张地图是边长为 m∗n 的矩形,被划分为m∗n个1∗1的小正方形.对于每个小正方形,地图上已经标注了它的海拔高度以及它是 ...
- 洛谷 P3456 [POI2007]GRZ-Ridges and Valleys
P3456 [POI2007]GRZ-Ridges and Valleys 题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两 ...
- 洛谷 P3455 [POI2007]ZAP-Queries (莫比乌斯函数)
题目链接:P3455 [POI2007]ZAP-Queries 题意 给定 \(a,b,d\),求 \(\sum_{x=1}^{a} \sum_{y=1}^{b}[gcd(x, y) = d]\). ...
- 洛谷P3459 [POI2007]MEG-Megalopolis [2017年6月计划 树上问题02]
[POI2007]MEG-Megalopolis 题目描述 Byteotia has been eventually touched by globalisation, and so has Byte ...
- 【刷题】洛谷 P3455 [POI2007]ZAP-Queries
题目描述 Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He ha ...
随机推荐
- selenium中的三种等待方式(显示等待WebDriverWait()、隐式等待implicitly()、强制等待sleep())---基于python
我们在实际使用selenium或者appium时,等待下个等待定位的元素出现,特别是web端加载的过程,都需要用到等待,而等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,在selenium中 ...
- Thinkphp实现excel导出数据
前端: 点击导出触发click事件,传值export指令和args关键字(args是指我们是否有查询取哪些数据)到控制器 $(document).on("click", " ...
- ptrdiff_t类型
一.特性 1. 这是一种标准库类型 2. 是两个指针相减的结果的类型(因为差值可能为负值,所以是一种带符号类型) 3. 和size_t一样,ptrdiff_t也是一种定义在<cstddef> ...
- 搭建独立域名博客 -- 独立域名博客上线了 www.hanshuliang.com
博客是安装在阿里云的服务器上. 小结 : -- 进入数据库命令 :mysql -uroot -p123456 ; -- 检查nginx配置语法 :.../nginx/sbin/nginx -t; -- ...
- alpha阶段个人总结(201521123031林庭亦)
一.个人总结 第一部分:硬的问题 第二部分:软的问题,在成长路上学到了什么? 1 当你看到不靠谱的设计.糟糕的代码.过时的文档和测试用例的时候,不要想 "既然别人的代码已经这样了,我的代码也 ...
- AutoHotKey 快速入门
AutoHotKey 是一个免费的键盘宏程序,可以用于配置键盘快捷键.鼠标事件 以及摇杆事件,还可以在输入文本的时候对文本进行扩展(自动补全) 第一个脚本 新建文件test.ahk并输入以下内容: ^ ...
- java基础简介
一.软件开发 软件:是由数据和指令组成的(例:计算器) 如何实现软件开发呢? 就是使用开发工具和计算机语言做出东西来 二.常用dos命令 d: 回车 盘符切换 dir(directory):列出 ...
- 【bzoj1131】[POI2008]Sta 树形dp
题目描述 给出一个N个点的树,找出一个点来,以这个点为根的树时,所有点的深度之和最大 输入 给出一个数字N,代表有N个点.N<=1000000 下面N-1条边. 输出 输出你所找到的点,如果具有 ...
- 分离IE9以下浏览器
/*判断浏览器版本是否过低*/ function IETester(userAgent) { var UA = userAgent || navigator.userAgent; if (/msie/ ...
- 【POJ2728】Desert King(分数规划)
[POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...