noip 2010 water into the city (dfs + greedy)

  Very sorry, because the little busy yesterday, solution to a problem of solving the problem did not write, make up today, writing for a little more detail, so that we understand.

  Look at the meaning of the questions:

  In a distant country, a scenic lake side, the other side is endless desert. Administrative divisions of the country is very special, just constitute a NN N rows × M \ Times M × rectangle M columns, as shown above, where each grid represents a city, every city has a height above sea level.

  In order to make the residents have to drink as much as possible to clear lake, now to build irrigation facilities in some cities. There are two water conservancy facilities, namely water plants and water stations. Water plant pump function is to use the lake water drawn into the city's reservoir.

  Therefore, only the lake adjacent to the first 11 cities 1 line can build water plant. The function of the station is to use water drop height by a water pipeline, the water delivered from the heights to the flat. So the city can build a water station premise is the existence of higher and have a common edge adjacent to the city than its altitude, has built water conservancy facilities. Since the first NN city N lines close to the desert, arid region of the country, requires each city which are built irrigation facilities. Well, we can meet this requirement? If so, please calculate the minimum built several water plant; if not, impossible to find in arid regions has a number of urban water conservancy facilities.

Input and output formats

  Input formats:

  Each row two numbers, separated by a space between. Two first line of input is a positive integer N, the MN, M N , M, represents the size of the rectangle. Next NN N lines of MM M a positive integer, followed by representatives of altitude each city.

   Output formats:

Two lines. If they can meet the requirements, the output of the first line is an integer . 11 1, the second row is an integer representing the minimum number of storage plant construction; if not meet the requirements, the output of the first line is an integer of 00 to 0, the second line is an integer, representing several arid areas in the city can not build water conservancy facilities.

This time I took up the meaning of the questions are, Luo valley this question, so we can go to other oj Los Valley and other test data and see the meaning of the questions, do not say here, mainly to talk about ideas.

  From the point of view of solving the problem data range, n, m <= 500 is very large, so we need to consider efficient algorithms, or a time-out will not be easy, say This question can flow from the water to a height located and then find the least-built water plant,

If there are no outputs '0' ...... (meaning of the questions is to see it, to say the very clear meaning of the questions).

  Not difficult to find, we need to know the city on the lake as a water plant is able to flow there, it needs to be solved dfs, dfs also requires skill, you need to record the current position can be conveyed to those cities near the desert.

  In this way we get the relationship between the city and the desert city of lakes, simplifies the problem, we'll take a look at the city lake corresponding desert city, find the corresponding desert city must be continuous (think about it, why?) If Discontinuous

Then there is no solution, and you need not irrigate the output of a number of cities, for continuous city, we can easily think of greed in the range of coverage problems, we need to sort the range, the first sort (small forward left) on the left ,

Left the same look on the right (the right side of the big front), which is the order of the choice of site range of different (this is very important, otherwise it can not be the optimal solution), then follow the zone covered by the program to do it on the line, you can go look at other blog section above

Covering explanation is not difficult, then this question with the corresponding lower, on the issue of range coverage, I will say, Here's the code:

 

// P1514 
 // DFS greedy + (interval coverage problems) 
#include <cstdio>  
#include <CString>  
#include <algorithm>
 the using  namespace STD; 

const  int MAXN = 500 + . 5 ;
 int N, M, G [MAXN] [ MAXN]; 

int L [MAXN] [MAXN], R & lt [MAXN] [MAXN]; // where L [1] tABLE 1 Block near lake city corresponding to the leftmost desert city   
BOOL Vis [MAXN] [MAXN] , C [MAXN]; 

const  int DX [] = {- . 1 , . 1 , 0 , 0 }; 
 const  int Dy [] = { 0, 0, -1, 1}; 

void dfs(int x, int y) {
  if (x == N-1) C[y] = 1; 
  for (int i = 0; i < 4; ++i) {
    int x1 = x + dx[i], y1 = y + dy[i]; 
    if (x1 >= 0 && x1 < N && y1 >= 0 && y1 < M && G[x][y] > G[x1][y1]) {
      if (Vis[x1][y1] == 0) {
        Vis[x1][y1] = 1; 
        dfs(x1, y1);
      }
      if (L[x][y] == -1 || (L[x][y] > L[x1][y1] && L[x1][y1] != -1)) L[x][y] = L[x1][y1]; 
      if (R[x][y] == -1 || (R[x][y] < R[x1][y1] && R[x1][y1] != -1)) R[x][y] = R[x1][y1]; 
    }
  }
}

int City[maxn];

bool cmp(int a, int b) {
  if (L[0][a] != L[0][b]) return L[0][a] < L[0][b]; 
  return R[0][a] > R[0][b];     
}

void find_pos(int& lastpos, int& rpos) {
  int larpos = 0, rem = rpos; 
  for (int i = rem; i < M; ++i) 
    if (L[0][City[i]] <= lastpos+1) {
      if (R[0][City[i]] > larpos) {
        larpos = R[0][City[i]]; 
        rpos = i;
      }
    }     
    else break; 
  lastpos = larpos; 
}

int main() { 
  freopen("in.txt", "r", stdin);
  freopen("out.txt", "w", stdout); 
  scanf("%d%d", &N, &M); 
  for (int i = 0; i < N; ++i) 
    for (int j = 0; j < M; ++j) 
      scanf("%d", &G[i][j]); 
  memset(L, -1, sizeof(L)); 
  memset(R, -1, sizeof(R));
  // dfs
  memset(Vis, 0, sizeof(Vis));
  for (int j = 0; j < M; ++j) L[N-1][j] = R[N-1][j] = j;
  for (int j = 0; j < M; ++j) if (!Vis[0][j]) {
    Vis[0][j] = 1; 
    DFS ( 0 , J);   
  } 
  // greedy interval coverage problems   
  int CNT = 0 ; 
   for ( int Y = 0 ; Y <M; Y ++) IF (C [Y] == 0 ) ++ CNT; 
   IF ( CNT) the printf ( " 0 \% n-D \ n- " , CNT); 
   the else {
     for ( int Y = 0 ; Y <M; Y ++) City [Y] = Y; 
    Sort (City, City + M, CMP ); 
    CNT = 0 ; 
     int POS = 0, rpos = 0; 
    while (pos != M-1) {
      cnt++; 
      find_pos(pos, rpos); 
    }
    printf("1\n%d\n", cnt);
  }
  return 0;
}

 

Guess you like

Origin www.cnblogs.com/yifeiWa/p/11220241.html