[ACM] HDU 3395 Special Fish (Maximum Weight Matching in bipartite graph, KM algorithm), hdu3395

Source: Internet
Author: User

[ACM] HDU 3395 Special Fish (Maximum Weight Matching in bipartite graph, KM algorithm), hdu3395

Special Fish

Problem DescriptionThere is a kind of special fish in the East Lake where is closed to campus of Wuhan University. it's hard to say which gender of those fish are, because every fish believes itself as a male, and it may attack one of some other fish who is believed to be female by it.
A fish will spawn after it has been attacked. each fish can attack one other fish and can only be attacked once. no matter a fish is attacked or not, it can still try to attack another fish which is believed to be female by it.
There is a value we assigned to each fish and the spawns that two fish spawned also have a value which can be calculated by XOR operator through the value of its parents.
We want to know the maximum possibility of the sum of the spawns.
InputThe input consists of multiply test cases. the first line of each test case contains an integer n (0 <n <= 100), which is the number of the fish. the next line consists of n integers, indicating the value (0 <value <= 100) of each fish. the next n lines, each line contains n integers, represent a 01 matrix. the I-th fish believes the j-th fish is female if and only if the value in row I and column j if 1.
The last test case is followed by a zero, which means the end of the input.
OutputOutput the value for each test in a single line.
Sample Input
31 2 30111011100
 
Sample Output
6
 
Authormomodi @ whu
SourceThe 5th Guangting Cup Central China Invitational Programming Contest
Recommendnotonlysuccess | We have carefully selected several similar problems for you: 1533 1853 2448 3388


Solution:

There are n special fish, each of which has a value. If I think "Fish j has a different gender, attack it, the value of the breeding offspring is v [I] ^ v [j]. Each fish can only attack or be attacked once, and the maximum value of the final breeding descendant is asked.

The maximum weight of a beam Binary Graph does not match. Edge I and j have the same weight as v [I] ^ v [j].

Http://blog.csdn.net/sr_19930829/article/details/40650359

Code:

# Include <iostream> # include <stdio. h> # include <algorithm> # include <string. h> using namespace std; const int maxn = 102; const int inf = 0x3f3f; int nx, ny; // int v [maxn]; // valueint g [maxn] [maxn] of each fish; // The adjacent matrix int linked [maxn]; // The right vertex and the left vertex connect int lx [maxn], ly [maxn]; // int slack [maxn]; // slack [j] indicates the minimum bool value of lx [I] + ly [j]-w [I] [j] for all the edges of vertex j on the right. visx [maxn], visy [maxn]; bool DFS (int x) // hungary calculates the augmented path {visx [x] = t Rue; for (int y = 0; y <ny; y ++) {if (visy [y]) continue; int tmp = lx [x] + ly [y]-g [x] [y]; if (tmp = 0) {visy [y] = true; if (linked [y] =-1 | DFS (linked [y]) {linked [y] = x; return true ;}} else if (slack [y]> tmp) slack [y] = tmp;} return false;} int KM () {memset (linked,-1, sizeof (linked )); memset (ly, 0, sizeof (ly); for (int I = 0; I <nx; I ++) {lx [I] =-inf; for (int j = 0; j <ny; j ++) if (g [I] [j]> lx [I]) lx [I] = g [I] [j];} for (int x = 0; x <Nx; x ++) {for (int y = 0; y <ny; y ++) slack [y] = inf; while (true) {memset (visx, 0, sizeof (visx); memset (visy, 0, sizeof (visy); if (DFS (x) break; int d = inf; for (int y = 0; y <ny; y ++) if (! Visy [y] & d> slack [y]) d = slack [y]; for (int I = 0; I <nx; I ++) if (visx [I]) lx [I]-= d; for (int I = 0; I <ny; I ++) {if (visy [I]) ly [I] + = d; else slack [I]-= d ;}} int ans = 0; for (int y = 0; y <ny; y ++) {if (linked [y]! =-1) ans + = g [linked [y] [y];} return ans;} int main () {int n; while (scanf ("% d ", & n )! = EOF & n) {nx = ny = n; for (int I = 0; I <n; I ++) scanf ("% d ", & v [I]); int ch; for (int I = 0; I <n; I ++) {for (int j = 0; j <n; j ++) {scanf ("% 1d", & ch); // input format 1d if (ch = 1) g [I] [j] = v [I] ^ v [j]; else g [I] [j] = 0 ;}} printf ("% d \ n ", KM ();} return 0 ;}




Hdu 1269 tarjan WA explained

No loop call

Program code of kM algorithm and Hungary Algorithm

// Binary Graph best match, kuhn munkras algorithm, adjacent array form, complexity O (m * n)
// Return the optimal matching value. input the size of the bipartite graph m, n, and the adjacent matrix mat to indicate the weight.
// Match2 and match2 return an optimal match. The unmatched vertex match value is-1.
// Pay attention to m <= n; otherwise, the cycle cannot be terminated.
// Obtain the opposite number for the minimum weight matching.
# Include <string. h>
# Define maxn310
# Define inf 1000000000
# Define _ clr (x) memset (x, 0xff, sizeof (int) * n) int kuhn_munkras (int m, int n, int mat [] [MAXN], int * match2, int * match2 ){
Int s [MAXN], t [MAXN], l1 [MAXN], l2 [MAXN], p, q, ret = 0, I, j, k;
For (I = 0; I <m; I ++)
For (l1 [I] =-inf, j = 0; j <n; j ++)
L1 [I] = mat [I] [j]> l1 [I]? Mat [I] [j]: l1 [I];
For (I = 0; I <n; l2 [I ++] = 0 );
For (_ clr (match2), _ clr (match2), I = 0; I <m; I ++ ){
For (_ clr (t), s [p = q = 0] = I; p <= q & mattings [I] <0; p ++)
For (k = s [p], j = 0; j <n & mattings [I] <0; j ++)
If (l1 [k] + l2 [j] = mat [k] [j] & t [j] <0 ){
S [++ q] = match2 [j], t [j] = k;
If (s [q] <0)
For (p = j; p> = 0; j = p)
Match2 [j] = k = t [j], p = match1 [k], match1 [k] = j;
}
If (mattings [I] <0 ){
For (I --, p = inf, k = 0; k <= q; k ++)
For (j = 0; j <n; j ++)
If (t [j] <0 & l1 [s [k] + l2 [j]-mat [s [k] [j] <p)
P = l1 [s [k] + l2 [j]-mat [s [k] [j];
For (j = 0; j <n; l2 [j] + = t [j] <0? 0: p, j ++ );
For (k = 0; k <= q; l1 [s [k ++]-= p );
}
}
For (I = 0; I <m; I ++)
Ret + = mat [I] [mate8 [I];
Return ret;
========================================================== ========================================/// Maximum matching of a bipartite graph, hungary algorithm, adjacent array form, complexity O (m * n)
// Returns the maximum number of matching results. The size of the input bipartite graph is m, n, and the adjacent matrix mat. A non-zero element indicates an edge.
// Match2 and match2 return a maximum match. The unmatched vertex match value is-1.
# Include <string. h>
# Define maxn310
# Define _ clr (x) memset (x, 0xff, sizeof (I ...... the remaining full text>

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.