POJ 2400 KM algorithm minimum weight matching backtracking output all optimal matching schemes

Source: Internet
Author: User

A tough question

First, the input is very painful. According to the online experts, the question matrix is reversed. Then I gave it back.

If it is a match between n and n, there is no pressure to directly obtain the negative value of KM.

However, if the points on both sides are different, it is said that there will be problems.

 

 

[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <vector>
# Include <queue>
# Define maxn505
# Define MAXM 555555
# Define INF 1000000000
Using namespace std;
Int n, m, ny, nx;
Int w [MAXN] [MAXN];
Int lx [MAXN], ly [MAXN];
Int linky [MAXN];
Int visx [MAXN], visy [MAXN];
Int slack [MAXN], ans;
Int res [MAXN], v [MAXN];
Bool find (int x)
{
Visx [x] = 1;
For (int y = 1; y <= ny; y ++)
{
If (visy [y]) continue;
Int t = lx [x] + ly [y]-w [x] [y];
If (t = 0)
{
Visy [y] = 1;
If (linky [y] =-1 | find (linky [y])
{
Linky [y] = x;
Return true;
}
}
Else if (slack [y]> t) slack [y] = t;
}
Return false;
}
Int KM ()
{
Memset (linky,-1, sizeof (linky ));
For (int I = 1; I <= nx; I ++) lx [I] =-INF;
Memset (ly, 0, sizeof (ly ));
For (int I = 1; I <= nx; I ++)
For (int j = 1; j <= ny; j ++)
If (w [I] [j]> lx [I]) lx [I] = w [I] [j];
For (int x = 1; x <= nx; x ++)
{
For (int I = 1; I <= ny; I ++) slack [I] = INF;
While (true)
{
Memset (visx, 0, sizeof (visx ));
Memset (visy, 0, sizeof (visy ));
If (find (x) break;
Int d = INF;
For (int I = 1; I <= ny; I ++)
If (! Visy [I]) d = min (d, slack [I]);
For (int I = 1; I <= nx; I ++)
If (visx [I]) lx [I]-= d;
For (int I = 1; I <= ny; I ++)
If (visy [I]) ly [I] + = d;
Else slack [I]-= d;
}
}
Ans = 0;
For (int I = 1; I <= ny; I ++)
If (linky [I]> 0) ans-= w [linky [I] [I];
Return ans;
}
Int cnt;
Void dfs (int u, int sum)
{
If (sum> ans) return;
If (u> n)
{
If (sum! = Ans) return;
Printf ("Best Pairing % d \ n", ++ cnt );
For (int I = 1; I <= n; I ++)
Printf ("Supervisor % d with Employee % d \ n", I, res [I]);
}
Else
{
For (int I = 1; I <= n; I ++)
If (! V [I])
{
Res [u] = I;
V [I] = 1;
Dfs (u + 1, sum-w [u] [I]);
V [I] = 0;
}
}
}
Int main ()
{
Int x, y, z, e, cas = 0, T;
Scanf ("% d", & T );
While (T --)
{
If (cas) printf ("\ n ");
Scanf ("% d", & n );
Nx = n, ny = n, m = n;
For (int I = 1; I <= n; I ++)
For (int j = 1; j <= m; j ++)
W [I] [j] = 0;
For (int I = 1; I <= n; I ++)
For (int j = 0; j <n; j ++)
{
Scanf ("% d", & x );
W [x] [I]-= j;
}
For (int I = 1; I <= n; I ++)
For (int j = 0; j <n; j ++)
{
Scanf ("% d", & x );
W [I] [x]-= j;
}
Printf ("Data Set % d, Best average difference: % f \ n", ++ cas, 0.5 * KM ()/n );
Cnt = 0;
Memset (v, 0, sizeof (v ));
Dfs (1, 0 );
}
Return 0;
}


Author: sdj222555

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.