HDU (1498 and 2119) balloons with minimum point coverage

Source: Internet
Author: User

I will give you a 0/1 matrix, a gun, and a gun. A gun can destroy 1 in the same row or column. How many times can you change all 1 to 0?

Idea: we use X and Y axes as the vertices to create a bipartite graph. If the number is 1, even one side is actually converted into the problem of least vertex coverage, then, use the Ed algorithm to obtain the maximum binary matching.

Note:

1. the maximum number of matching vertices should be N, so for (INT I = 1; I <= N; I ++) Ed (I );

2. The matrix data range is 1 ~ 100, so the maximum number of edges should be 100*100. Otherwise, re.

# Include <stdio. h>

# Include <string. h>

Int map [1000] [1000];

Int link [1000], Mark [1000];

Int M, N;

Int find (int K) // process a color. Therefore, two values are required for a variable .. ,

{

Int I;

For (I = 1; I <= m; I ++)

{

If (MARK [I] = 0 & map [k] [I])

{

Mark [I] = 1;

If (link [I] = 0 | find (link [I])

{

Link [I] = K;

Return 1;

}

}

}

Return 0;

}

Int main ()

{

Int I, J, K, sum;

While (scanf ("% d", & N), n)

{

Scanf ("% d", & M );

Memset (MAP, 0, sizeof (MAP ));

Memset (link, 0, sizeof (Link ));

For (I = 1; I <= N; I ++)

For (j = 1; j <= m; j ++)

{

Scanf ("% d", & K );

If (k = 1)

Map [I] [J] = 1;

}

Sum = 0;

For (I = 1; I <= N; I ++)

{

Memset (mark, 0, sizeof (Mark ));

If (find (I ))

Sum ++;

}

Printf ("% d \ n", sum );

}

Return 0;

}

You can first perform HDU 2119. You can only delete one row or one column at a time. The result is how many times can 1 be deleted, and a behavior subset is used as a subset, if map [I] [J] = 1 is to create an edge between I and j and delete 1, that is, each edge is to be deleted, that is, the minimum point coverage problem, this is also the question that only one row or column of balloons of the same color can be eliminated at a time. It is required to delete the balloon of each color at least how many times. If it is larger than K, it cannot be deleted, the requirement is that it cannot be deleted. Each type of balloon is equivalent to HDU.
1 of 2119 is also the minimum point coverage ..

1498

Question: Give You a n * n matrix, a balloon with S colors distributed in the matrix, K attempts to crack the balloon, each operation can break a line, or a column of balloons of the same color. Ask which types of balloons cannot be completely broken after K operations.

Solution:

Use bipartite graph matching to find the maximum matching values for each color (the rows and columns are set a and Set B respectively; m [I, j] indicates a match ), if the value is greater than K, "-1" is output; otherwise, the ascending sequence of colors is output.

Note: Find the least vertex in the bipartite graph, and associate each edge with at least one vertex. This is the "minimum vertex overwrite" of the Bipartite Graph ".

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
Int map [101] [101];

Int color [51], link [101];

Int mark [101], p [51];

Int N;

Int CMP (const void * a, const void * B)

{

Return * (int *) A-* (int *) B;

}

Int find (int K, int H)

{

Int I;

For (I = 1; I <= N; I ++)

{

If (MARK [I] = 0 & map [k] [I] = h) // match with the same color (emphasis)

{

Mark [I] = 1;

If (link [I] = 0 | find (link [I], H) ---- find two quantities ..

{

Link [I] = K;

Return 1;

}

}

}

Return 0;

}

Int main ()

{

Int I, j, M, K, H, sum;

While (scanf ("% d", & N, & M), N | m)

{

K = 1;

Memset (MAP, 0, sizeof (MAP ));

Memset (mark, 0, sizeof (Mark ));

Memset (color, 0, sizeof (color ));

For (I = 1; I <= N; I ++)

For (j = 1; j <= N; j ++)

{

Scanf ("% d", & map [I] [J]);

If (MARK [map [I] [J] = 0)

{

Mark [map [I] [J] = 1;

Color [k] = map [I] [J]; // number of colors

K ++;

}

}

H = 0;

For (I = 1; I <= K; I ++)/calculate the minimum number of times each color is deleted (each operation is the same as the previous one, here there are multiple colors, so you need to process them multiple times) initialize each time ))

{

Memset (link, 0, sizeof (Link ));

Sum = 0;

For (j = 1; j <= N; j ++)

{

Memset (mark, 0, sizeof (Mark ));

Sum + = find (J, color [I]);

}

If (sum> m)

{

P [H] = color [I];/counts colors that cannot be deleted

H ++;

}

}

Qsort (p, H, sizeof (P [0]), CMP); sort

If (H = 0)

Printf ("-1 \ n ");

Else

{

For (I = 0; I

Printf ("% d", P [I]);

Printf ("% d \ n", P [h-1]);

}

}

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.