hdu1150 Machine Schedule (Hungarian algorithm template)

Source: Internet
Author: User

The Hungarian algorithm is essentially a match, for the current processing of the object I, assuming that his matching object is J, but J has and V match well, then let V to find out if you can match others

V can match others I match J, can't I go find someone else to match

In addition, several definitions and conclusions are introduced:

1: Maximum number of matches + maximum Independent set = n + M (N,m is two nodes on both sides of the graph)

2: Minimum vertex coverage of a binary graph = maximum number of matches

3: Minimum path override = Maximum Independent set

Maximum independent set refers to the largest set of points in a binary graph, where the points in the set are not connected.

Minimum vertex overlay refers to: in a binary graph, with the fewest points, all edges are associated with at least one point.

Minimum path overrides are:

For a DAG (directed acyclic graph), select the fewest paths so that each vertex belongs to and belongs to only one path. The path length can be 0 (that is, a single point).

The maximum match is:

For all sides of a binary graph, look for a subset that satisfies two conditions,
1: Any two edges are independent of the same point.
2: Let the edges of this subset be as many as possible in the case of satisfying the condition one.

Then there is the template:

BOOLFindintx) {    inti,j;  for(j=1; j<=m;j++) {//Scan every sister        if(line[x][j]==true&& used[j]==false)              //if there is ambiguous and has not been marked (the mark here means that this search has tried to change the problem of the sister's attribution, but did not succeed, so there is no need to go blind){Used[j]=1; if(girl[j]==0||find (Girl[j])) {                 //no master or can make a place, use recursion heregirl[j]=x; return true; }        }    }    return false;}intMain () { for(i=1; i<=n;i++) {memset (used,0,sizeof(used));//This is emptied at every step.    ifFind (i) all+=1;} return 0; }

Then the subject code:

#include <cstdio>#include<cstring>#include<vector>#include<algorithm>#include<iostream>#include<map>#include<queue>#include<stack>#include<cmath>//#pragma COMMENT (linker, "/stack:102400000,102400000")using namespacestd;#definePF (x) cout << "Debug:" << x << "";#defineEL cout << Endl;#definePC (x) puts (x);typedefLong Longll;#defineCLR (x, v) sizeof (x, V, sizeof (x))using namespacestd;Const intINF =0x5f5f5f5f;Const intn=1000050;Const intmaxn= the;intN,M,K,USED[MAXN],LK[MAXN][MAXN],MARK[MAXN];BOOLFindintu) {     for(inti =1; I <= m;i++){        if(lk[u][i]&&!Used[i]) {Used[i]=1; if(Mark[i] = =0||find (Mark[i])) {Mark[i]=u; return true; }        }    }    return false;}intMain () {//freopen ("In.txt", "R", stdin);     while(~SCANF ("%d",&N)) {        if(n = =0)             Break; memset (LK,0,sizeof(LK)); memset (Mark,0,sizeof(Mark)); scanf ("%d%d",&m,&k); intI,a,b,c;  for(i =1; I <= k;i++) {scanf ("%d%d%d",&c,&a,&b); LK[A][B]=1; }        intans=0;  for(i =1; I <= n;i++) {memset (used,0,sizeof(used)); if(Find (i)) ans++; } cout<<ans<<Endl; }    return 0;}

hdu1150 Machine Schedule (Hungarian algorithm template)

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.