Beginner Hungary algorithm (2)

Source: Internet
Author: User

Here is an example to illustrate

Vote

Vote. pas/c/cpp

[Problem description]

Mr. k is playing A game. In the game, he plays the role of A circus owner. Now Mr. k needs to use A cat and B dog in the circus to hold A performance, before the show, he asked the audience to vote. The voting class is: I want to see the performances of CAT/dog ___, and I don't want to see the performances of CAT/dog. Note that every audience prefers cats or dogs, so the two empty spaces are bound with different contents. Audience who like cats will select cats at the end of the first empty box and dogs at the end of the second empty box. Otherwise, they will select dogs at the end of the second empty box and cats at the end of the second empty box. For each audience, only when TA's voting content is satisfied (that is, the animals he wants to see are displayed, and the animals he doesn't want to see are not displayed, TA will see the performance. Of course, you have to pay for the tickets to watch the performances. As the circus owner, Mr. k naturally hopes that the more people there will be, the better. He wants to find a scheme to arrange animal performances so that he can see as many audience as possible.

[Input format]

Three Positive Integers (n, m, and k) in row 1st indicate the number of cats, dogs, and audience respectively.

2nd ~ In k + 1 rows, each row describes the voting content of an audience.

Enter a character "C" or "D" followed by a number to indicate the animal that an audience wants to see. Then, separate the character "C" or "D" with a space, and then add a number to the character "C" or "D, indicates the animal that the audience does not want to see. Two C or two D s will not appear in the same line.

[Output format]

Output a positive integer in a row, indicating the number of viewers that a small k can attract in the optimal arrangement.

[Example input]

Vote. in

1 2 4

C1 D1

C1 D1

C1 D2

D2 C1

[Sample output]

Vote. out

3

[Data scale and Conventions]

For 25% of data, n, m <= 10, k <= 25

For 100% of data, n, m <= 300, k <= 500

Analysis,

This question draws a conclusion:

Minimum path overwrite = | P |-Maximum number of matches

(It's just a conclusion! Yes !)

I cannot understand the derivation of this conclusion .... Bytes

For details, refer to Baidu encyclopedia.Http://baike.baidu.com/view/2444809.htm

Then the plain Hungary... After finding the largest matching ans,

K-ans is the request.

In fact, we can see that this is a Bipartite Graph Match,

And figured out how to divide it into the left and right parts.


Program vote; var I, j, n, m, ans, k, x, y, l1, l2: longint; a: array [1 .. 600, 1 .. 600] of longint; B, c, d, s1, s2, flag1, flag2: array [1 .. 500] of longint; cc: char; function pd (x: longint): boolean; var I, j: longint; begin for I: = 1 to k do if (a [x, I] = 1) and (flag2 [I] = 0) then begin flag2 [I]: = 1; if (flag1 [I] = 0) or (pd (flag1 [I]) then begin flag1 [I]: = x; exit (true); end; exit (false); end; procedure hungary; // the specific process of hungary is not explained. Just like the bare one, var I, j: longint; begin fillchar (flag1, sizeof (flag1 ), 0); for I: = 1 to l1 do begin fillchar (flag2, sizeof (flag2), 0); if pd (I) then inc (ans); end; begin assign (input, 'vote. in '); reset (input); assign (output, 'vote. out'); rewrite (output); readln (n, m, k); for I: = 1 to k do begin read (cc); read (x ); if cc = 'D' then begin inc (l2); s2 [l2]: = I; B [I]: = x + 500; read (cc ); read (cc); read (y); c [I]: = y; end else begin inc (l1); s1 [l1]: = I; B [I]: = x; read (cc); read (y); c [I]: = y + 500; end; readln; end; // read + process, like a cat on one side, like a dog on one side for I: = l1 + 1 to k do s1 [I]: = s2 [i-l1]; for I: = 1 to l1 do for j: = l1 + 1 to k do if (B [s1 [I] = c [s1 [j]) or (c [s1 [I] = B [s1 [j]) then begin a [I, j]: = 1; a [j, I]: = 1; end; // Add edge ans: = 0; hungary; // hungary writeln (k-ans); // output close (input); close (output); end.

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.