Number of HDU 1569 squares (2)

Source: Internet
Author: User

Hdu_1569

There was no idea at the beginning, and later I came to the others' solution report saying that we should construct a bipartite graph and convert it into a minimal cut.

Later, I thought about it again. The general principle is as follows: I + J The parity is divided into two parts (that is, the result of being dyed as the chess board). If an edge is connected between adjacent grids, the greatest weight independence set is obtained, and the largest weight independence set is equal Sum Subtract the least vertex weight overwrite set, so that we can convert it to the least vertex weight overwrite set. If we construct a super Source Vertex connected to all the vertices on the left of the Bipartite Graph and the edge weight is the grid weight, at the same time, all the points on the right of the bipartite graph are connected to a super sink point and the edge weight is the weight of the grid, and the Edge Weight of the edge in the middle of the Bipartite Graph is set INF In this way, the minimum cut is the least vertex weight overwrite set.

Because if it is a minimum vertex weight overwrite set, remove these vertices and Their covered edges, and the source image will not work (if the edges connected to the source or sink are overwritten, therefore, all edges of a bipartite graph must be overwritten. Therefore, you only need to be able to overwrite the edges of a bipartite graph, therefore, the minimum vertex weight overwrite set is the minimum cut of the source image.

# Include <stdio. h>
# Include < String . H>
# Define Maxd 3000
# Define Maxm 36000
# Define INF 100000000
Int N, m, sum;
Int First [maxd], next [maxm], U [maxm], V [maxm], flow [maxm], E;
Int S [maxd], d [maxd], Q [maxd], work [maxd];
Int DX [] = {- 1 , 1 , 0 , 0 }, Dy [] = { 0 , 0 ,- 1 , 1 };
Void Add (Int A, Int B, Int W)
{
U [e] =;
V [e] = B;
Flow [e] = W;
Next [e] = first [a];
First [a] = E;
E ++;
}
Int Init ()
{
Int I, J, K,;
If (Scanf ( " % D " , & M, & N )! = 2 )
Return 0 ;
Memset (first ,- 1 , Sizeof (First ));
E = sum = 0 ;
For (I = 1 ; I <= m; I ++)
For (J = 1 ; J <= N; j ++)
{
Scanf ( " % D " , & );
Sum + =;
If (I + J) % 2 = 0 )
{
Add ( 0 , I * n + J, );
Add (I * n + J, 0 , 0 );
For (K = 0 ; K < 4 ; K ++)
{
Int X = I + dx [k];
Int Y = J + dy [k];
If (X> = 1 & X <= M & Y> = 1 & Y <= N)
{
Add (I * n + J, x * n + Y, INF );
Add (x * n + y, I * n + J, 0 );
}
}
}
Else
{
Add (I * n + J, 1 , );
Add ( 1 , I * n + J, 0 );
}
}
Return 1 ;
}
Int BFS ()
{
Int I, j, rear;
Memset (D ,- 1 , Sizeof (D ));
D [ 0 ] = 0 ;
Rear = 0 ;
Q [rear ++] = 0 ;
For (I = 0 ; I <rear; I ++)
For (J = first [Q [I]; J! =- 1 ; J = next [J])
If (Flow [J] & D [V [J] =- 1 )
{
D [V [J] = d [Q [I] + 1 ;
If (V [J] = 1 )
Return 1 ;
Q [rear ++] = V [J];
}
Return 0 ;
}
Int Dinic ()
{
Int I, Res = 0 , R, cur;
While (BFS ())
{
R = cur = 0 ;
Memcpy (work, first, Sizeof (First ));
For (;;)
{
If (Cur = 1 )
{
Int A = inf, minr = 0 ;
For (I = 0 ; I <r; I ++)
If (Flow [s [I] <)
{
A = flow [s [I];
Minr = I;
}
For (I = 0 ; I <r; I ++)
{
Flow [s [I]-=;
Flow [s [I] ^ 1 ] + =;
}
Res + =;
R = minr;
Cur = U [s [R];
}
For (I = work [cur]; I! =- 1 ; I = next [I])
{
If (Flow [I] = 0 )
Continue ;
If (D [V [I] = d [cur] + 1 )
Break ;
}
Work [cur] = I;
If (I! =- 1 )
{
S [R ++] = I;
Cur = V [I];
}
Else
{
D [cur] =- 1 ;
If (R = 0 )
Break ;
R --;
Cur = U [s [R];
}
}
}
Return Res;
}
Int Main ()
{
While (Init ())
{
Int Res = dinic ();
Printf ( " % D \ n " , Sum-res );
}
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.