"Search +DP" codevs1066-water diversion into the city

Source: Internet
Author: User

"The main topic"

A rectangle of n rows m columns, as shown, each of which represents a city with an altitude of each city. Water facilities are now being built in some cities. There are two kinds of water conservancy facilities, namely water storage plant and conveyance station. The function of a storage plant is to pump water from a lake into a reservoir in the city in which it is used. As a result, only cities with a 1th row adjacent to the lake can build water storage plants. The function of the water-conveyance station is to transfer the lake from the high to the lower level by using the height drop of the pipe line. Therefore, a city can build a water station, the premise is that there is higher than its elevation and the public side of the adjacent cities, has built water conservancy facilities. As the nth row of cities is close to the desert, it is the arid zone of the country, requiring water conservancy facilities in each of the cities. So, can this requirement be met? If so, please calculate a minimum number of water storage plants and, if not, the numbers of cities in the arid zone that are unlikely to be able to build irrigation facilities.

Ideas

Run DFS from every city along the lake, and get to the cities that can reach the desert side. In the case where all the cities can be accessed by the desert, it can be proved that a city by the lake next to the desert city is contiguous.

Proof: If it is not continuous then there must be another point B can reach the point where a cannot reach, then the path of two points must have a point of intersection, a must be able to reach through the intersection of the so-called unreachable place, so the assumption is not established.

Problem transformation in order to give some line segments, the minimum number of segments is used to cover an interval. DP just a bit better.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intmaxn= -+ -;7 structnode8 {9     intL,r;Ten     BOOL operator< (ConstNODE&AMP;X)Const One     { A         returnl<X.L; -     } - }ARRIV[MAXN]; the intM,N,H[MAXN][MAXN],VIS[MAXN][MAXN]; - intF[MAXN],COVER[MAXN]; - intdx[4]={1,-1,0,0}; - intdy[4]={0,0,1,-1}; +  - voidInit () + { Ascanf"%d%d",&m,&n); at      for(intI=1; i<=m;i++) -          for(intj=1; j<=n;j++) scanf ("%d",&H[i][j]);  - } -  - voidDfsintXintYintfr) - { invis[x][y]=fr; -     if(x==m) to     { +Arriv[fr].l=min (arriv[fr].l,y); -Arriv[fr].r=Max (arriv[fr].r,y); thecover[y]=1; *     } $      for(intI=0;i<4; i++)Panax Notoginseng     { -         intxx=x+dx[i],yy=y+Dy[i]; the         if(xx<=0|| Xx>m | | yy<=0|| Yy>n)Continue; +         if(H[xx][yy]fr) Dfs (XX,YY,FR); A     } the } +  - voidSolve () $ { $memset (Cover,0,sizeof(cover)); -memset (Vis,0,sizeof(Vis)); -      for(intI=1; i<=n;i++)  the     { -arriv[i].l=maxn,arriv[i].r=-1;WuyiDfs1, i,i); the     } -      Wu     intflag=1, rem=0; -      for(intI=1; i<=n;i++)if(!cover[i]) {flag=0; rem++;} About      $     if(flag) -     { -Puts"1"); -Sort (arriv+1, arriv+n+1);  A          for(intI=1; i<=n;i++) f[i]=MAXN; +f[0]=0; the          for(intI=1; i<=n;i++) -         { $             intL=arriv[i].l,r=ARRIV[I].R; the              for(intj=l-1; j<=r;j++) F[r]=min (f[r],f[j]+1); the         } theprintf"%d", F[n]); the     }  -     Elseprintf"0\n%d", REM); in } the  the intMain () About { the init (); the solve (); the     return 0; +}

"Search +DP" codevs1066-water diversion into the city

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.