Title: Given a map, all the points are flooded, there are some key points, the need to put the least water pump so that all the key points of the drain is drained
Front Row Thank VFK
First, it proves that there must be an optimal solution to make all the pumps at the critical point.
So we sort all the key points in the order of height, from small to large enumerate each key point
For each key x, we add all points with a height less than or equal to x points and check the set and merge adjacent
Since X is the highest point in the focus, it is possible to locate the pump at any point in the concentration, causing the point x to be drained.
Therefore, if X is located and the pump is already placed, there is no need to place the pump at x point
Or you're going to put a pump at x point.
Time Complexity O (Mnlog (MN))
At first wrote an O (1000mn) SPFA result T to death--
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define M 1010using namespace Std;struct abcd{int height,x,y;abcd () {}abcd (int _,int __,int ___): Height (_), X (__), Y (___) {}bool operator < (const ABCD &a) Const{return height < a.height;}} Cities[m*m],map[m*m];int n,m,ans,tot;int a[m][m];namespace union_find_set{pair<int,int> fa[M][M];bool pumped[ M][m];p air<int,int> Find (pair<int,int> x) {if (Fa[x.first][x.second]==make_pair (0,0) | | FA[X.FIRST][X.SECOND]==X) return Fa[x.first][x.second]=x;return fa[x.first][x.second]=find (Fa[x.first][x.second]) ;} void Union (pair<int,int> x,pair<int,int> y) {x=find (x); Y=find (y); if (x==y) return; fa[x.first][x.second]= Y;pumped[y.first][y.second]|=pumped[x.first][x.second];}} void Insert (int x,int y) {using namespace union_find_set;static const int dx[]={1,-1,0,0};static const INT DY[]={0,0,1,-1} ; int I;for (i=0;i<4;i++) {int xx=x+dx[i],yy=y+dy[i];if (xx<=0| | yy<=0| | xx>m| | Yy>n) continue;if (A[xx][yy]>a[x][y]) continue; Union (Make_pair (x, y), Make_pair (Xx,yy));}} int main () {using namespace Union_find_set;int i,j;cin>>m>>n;for (i=1;i<=m;i++) for (j=1;j<=n;j++) { scanf ("%d", &a[i][j]); if (a[i][j]>0) new (&cities[++tot]) ABCD (A[I][J],I,J); Elsea[i][j]=-a[i][j];new ( &MAP[I*N-N+J]) ABCD (A[I][J],I,J);} Sort (cities+1,cities+tot+1), sort (map+1,map+m*n+1), for (j=1,i=1;i<=tot;i++) {for (; J<=m*n&&map[j]. height<=cities[i].height;j++) Insert (map[j].x,map[j].y);p air<int,int> temp=find (Make_pair (cities[i].x, CITIES[I].Y)); if (Pumped[temp.first][temp.second]) continue;++ans,pumped[temp.first][temp.second]=true;} Cout<<ans<<endl;return 0;}
Bzoj 1104 POI2007 Flood pow-ji