Poj 2226 muddy fields Hungarian algorithm/minimum point coverage

Source: Internet
Author: User

Farmer John's cattle farm is a rectangle in Column C of column R. After heavy rain, water is accumulated in the low-lying areas of the cattle farm. John's cows are very delicate. When they eat grass, they don't want to dirty their hooves. To prevent the cows from getting their shoot dirty, John decided to put the place with water on the plank. His plank is 1 in width and has no length limit.


He wanted to cover all the low-lying areas with water with the fewest purpose boards, provided that the boards could not cover the grass, but could overlap.


The composition of this question is indeed more clever. If there is a continuous low-lying area, assuming it is horizontal, then these consecutive low-lying areas can be covered by a board. Similarly, if the vertical bars have continuous low-lying areas, you can also use a board to cover them. In this way, when a low-lying area can take both the horizontal board and the vertical board cover, it is the intersection. Then this line represents a low-lying place, which can be either horizontally or vertically covered. Now we take all the low-lying areas of the horizontal row as left (continuous low-lying areas as one board), and the same is true for vertical positions. The following table is used as an example:

Sample:

4

*.*.

.***

***.

..*.


Concatenate the pits in the rows as a point, that is, a horizontal board with serial numbers. The sample is converted:


1 0 2 0

0 3 3 3

4 4 4 0

0 0 5 0


Add these sequence numbers to the X set and then perform the following operations by column:


1 0 4 0

0 3 4 5

2 3 4 0

0 0 4 0


Similarly, if you add the y set, one pit can only be covered by a horizontal or vertical wooden board, and different serial numbers are marked for the original image pit. There are nine pits in total.


1. 2.

. 3 4 5

6 7 8.

... 9.

In the figure, the No. 2 low-lying area can be either a horizontal plate 2 or a vertical plate 4, so there is an edge between plate 2 and Board 4, the side actually represents a low-lying area. For example, the side of No. 2 low-lying area is {2, 4}. It can be understood that No. 2 low-lying area can be covered by plate 2 or plate 4. Connect all edges with the least vertices, which means the least vertices are covered.

Above from http://hi.baidu.com/onlys_c/blog/item/9781e0dd858f2fd28d102919.html

#include <cstring>#include <iostream>using namespace std;char temp[100][100];bool map[1000][1000],vis[1000];int U[100][100], V[100][100],match[1000];int R,C,u,v;bool find_path ( int r ){for ( int i = 1; i <= v; i++ ){if ( map[r][i] && !vis[i] ){vis[i] = true;if ( 0 == match[i] || find_path(match[i]) ){match[i] = r;return true;}}}return false;}int Hungary(){int ans = 0;for ( int i = 1; i <= u; i++ ){memset(vis,0,sizeof(vis));if ( find_path ( i ) )ans++;}return ans;}void construct (){int i,j;memset(map,0,sizeof(map));memset(match,0,sizeof(match));    u = v = 0;for ( i = 1; i <= R; i++ )for ( j = 1; j <= C; j++ )cin >> temp[i][j];for ( i = 1; i <= R; i++ ){for ( j = 1; j <= C; j++ ){if ( temp[i][j] == '*' ){u++;while ( j <= C && temp[i][j] == '*' ){U[i][j] = u;j++;}}}}for ( j = 1; j <= C; j++ ){for ( i = 1; i <= R; i++ ){if ( temp[i][j] == '*' ){v++;while ( i <= R && temp[i][j] == '*' ){V[i][j] = v;i++;}}}}for ( i = 1; i <= R; i++ ){for ( j = 1; j <= C; j++ ){if ( temp[i][j] == '*' )map[U[i][j]][V[i][j]] = true;}}}int main(){cin >> R >> C;construct();cout << Hungary() << endl;return 0;}

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.