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

  题目传送门

pow

题意翻译

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\times nm×nrectangle, 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 m and n , separated by a single space,1n,m1 000 . The following mm lines contain the description of the map. The (i+1) 'th line describes the i 'th row of unitary squares in the map. It contains nn integers xi,1,xi,2,...,xin , separated by single spaces, 1 000xi,j1 000 , xi,j1000 . The number xi,j describes the j 'th square of theii 'th line. The ground level in this square is xi,j∣ above sea level. If xi,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


  分析:

  比较考验思维的一道并查集题。

  首先,我们可以将每个点用一个值表示(用两个坐标表示不太方便),然后将其按照海拔从小到大排序,然后枚举每一个点,如果相邻的点的海拔$\leq$该点的海拔,则水可以又该点流向相邻的点,那么就将他们放在同一个并查集里。然后判断该店是否为城市,如果是则需要在联通块内放一个抽水机,如果不是则不用。但是注意,一定要将同一高度的点枚举完之后才能判断,否则会被特殊点卡掉。具体怎么特殊蒟蒻就不多讲,相信各位大佬们一定想得到。具体看代码吧。

  Code:

 1 //It is made by HolseLee on 15th July 2018
 2 //Luogu.org P3457
 3 #include<bits/stdc++.h>
 4 using namespace std;
 5 const int N=1e3+7;
 6 int n,m,a[N][N],cnt;
 7 bool vis[N*N];
 8 int fa[N*N],num[N][N],ans;
 9 int mvx[4]={0,0,1,-1};
10 int mvy[4]={1,-1,0,0};
11 struct Node{
12     int x,y,val;
13     bool operator < (const Node &x) const {
14         return val<x.val;}
15 }q[N*N];
16 inline int read()
17 {
18     char ch=getchar();int num=0;bool flag=false;
19     while(ch<'0'||ch>'9'){if(ch=='-')flag=true;ch=getchar();}
20     while(ch>='0'&&ch<='9'){num=num*10+ch-'0';ch=getchar();}
21     return flag?-num:num; 
22 }
23 inline int find(int x)
24 {
25     return fa[x]==x?x:fa[x]=find(fa[x]);
26 }
27 inline int Abs(int x)
28 {
29     return x>0?x:-x;
30 }
31 inline void merge(int x,int y)
32 {
33     int fax=find(x),fay=find(y);
34     vis[fay]|=vis[fax];
35     fa[fax]=fay;
36 }
37 int main()
38 {
39     m=read();n=read();
40     memset(vis,0,sizeof(vis));
41     memset(a,127,sizeof(a));
42     for(int i=1;i<=m;i++)
43     for(int j=1;j<=n;j++){
44         a[i][j]=read();}
45     for(int i=1;i<=m;i++)
46     for(int j=1;j<=n;j++){
47         num[i][j]=++cnt;
48         q[cnt]=(Node){i,j,Abs(a[i][j])};}
49     sort(q+1,q+cnt+1);
50     for(int i=1;i<=cnt;i++)fa[i]=i;
51     for(int i=1;i<=cnt;i++){
52         int X=q[i].x,Y=q[i].y;
53         for(int j=0;j<4;j++){
54             int x=X+mvx[j],y=Y+mvy[j];
55             if(Abs(a[x][y])<=q[i].val){
56                 merge(num[x][y],num[X][Y]);
57             } 
58         }
59         if(q[i].val!=q[i+1].val){
60             for(int j=i;q[j].val==q[i].val;j--)
61             if(a[q[j].x][q[j].y]>0){
62                 int fax=find(num[q[j].x][q[j].y]);
63                 if(!vis[fax])vis[fax]=1,ans++;
64             }
65         } 
66     }
67     printf("%d\n",ans);
68     return 0;
69 }

猜你喜欢

转载自www.cnblogs.com/cytus/p/9314516.html
今日推荐