日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

[POI2007]POW-The Flood

發(fā)布時(shí)間:2023/12/13 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 [POI2007]POW-The Flood 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目描述

給定一張地勢(shì)圖,所有的點(diǎn)都被水淹沒,現(xiàn)在有一些關(guān)鍵點(diǎn),要求放最少的水泵使所有關(guān)鍵點(diǎn)的水都被抽干

輸入輸出格式

輸入格式:

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 修建一個(gè)抽水機(jī),在什么情況下格子 b 的水也可以被抽干。
我們可以發(fā)現(xiàn)當(dāng)且僅當(dāng)存在一條從 a 到 b 的路徑,中間經(jīng)過的抽水機(jī)(包括 a)的高度都不大于 b 的高度。
即h[b]>=max(h[i])
因此我們可以考慮把所有格子的高度從小到大排序,我們把每一個(gè)格子建成一個(gè)集合。
然后按照海拔高度從小到大掃描格子,對(duì)于當(dāng)前的格子 i,我們找到所有與 i 相鄰并且海拔高度不大于格子 i 的格子,
我們發(fā)現(xiàn)如果這些格子中的任意一個(gè)洪水要是被解決了,那么格子 i 的洪水也可以被解決,所以我們合并這些格子。
對(duì)于當(dāng)前的格子 i,如果它必須被清理且與它相鄰的格子集合中沒有任何一個(gè)被清理,我們則把這個(gè)集合的清理狀態(tài)標(biāo)記為真,然后答案加 1。
集合和每個(gè)格子是否被清理用并查集來維護(hù)就可以了。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 struct node 7 { 8 int s,x,y; 9 } city[1000001],maze[100001]; 10 pair<int,int> set[1001][1001]; 11 int dx[5]={0,1,-1,0,0}; 12 int dy[5]={0,0,0,1,-1}; 13 int n,m,a[1001][1001],tot,cnt,vis[1001][1001],ans; 14 bool cmp(node a,node b) 15 { 16 return (a.s<b.s); 17 } 18 pair<int,int> find(pair<int,int> x) 19 { 20 if (set[x.first][x.second]!=x) set[x.first][x.second]=find(set[x.first][x.second]); 21 return set[x.first][x.second]; 22 } 23 void union_set(pair<int,int> x,pair<int,int> y) 24 { 25 x=find(x),y=find(y); 26 set[x.first][x.second]=y; 27 vis[y.first][y.second]|=vis[x.first][x.second]; 28 } 29 void exam(int x,int y) 30 { 31 for(int i=1;i<=4;i++) 32 { 33 int tx=x+dx[i], ty=y+dy[i]; 34 if(tx<=0||ty<=0||tx>m||ty>n) continue; 35 if(a[tx][ty]>a[x][y]) continue; 36 union_set(make_pair(x, y), make_pair(tx, ty)); 37 } 38 } 39 int main() 40 { 41 int i,j; 42 cin>>n>>m; 43 for (i=1; i<=n; i++) 44 { 45 for (j=1; j<=m; j++) 46 { 47 scanf("%d",&a[i][j]); 48 if (a[i][j]<=0) 49 { 50 a[i][j]=-a[i][j]; 51 } 52 else 53 { 54 city[++tot]=(node){a[i][j],i,j}; 55 } 56 maze[++cnt]=(node){a[i][j],i,j}; 57 set[i][j]=make_pair(i,j); 58 } 59 } 60 sort(maze+1,maze+cnt+1,cmp); 61 sort(city+1,city+tot+1,cmp); 62 for (i=1; i<=tot; i++) 63 { 64 for (j=1; j<=cnt,city[i].s>=maze[j].s; j++) 65 { 66 exam(maze[j].x,maze[j].y); 67 pair<int,int> x=find(make_pair(city[i].x,city[i].y)); 68 if (vis[x.first][x.second]==0) 69 { 70 ans++; 71 vis[x.first][x.second]=1; 72 } 73 } 74 } 75 cout<<ans; 76 }

?

轉(zhuǎn)載于:https://www.cnblogs.com/Y-E-T-I/p/7221242.html

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的[POI2007]POW-The Flood的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。