HDU 2255 Ben-well-off hair big wealth

Source: Internet
Author: User

Transmission Door

Solution:

KM algorithm

There is an excellent document about the KM algorithm http://www.cse.ust.hk/~golin/COMP572/Notes/Matching.pdf

Implementation:

#include <cstdio>#include<algorithm>#include<cstring>#include<climits>using namespacestd;Const intN305);intW[n][n];intLx[n], ly[n], slack[n];BOOLS[n], t[n];intMatch[n];intN;BOOLDfsintu) {S[u]=true;  for(intv=1; v<=n; v++)    {        if(T[v]) {Continue; }         inttmp=lx[u]+ly[v]-W[u][v]; if(tmp==0) {T[v]=true; if(!match[v] | |DFS (Match[v])) {Match[v]=u; return true; }        }        Else{Slack[v]=min (slack[v], TMP); }    }    return false;}voidKM () {memset (match,0,sizeof(match)); memset (Lx,0x3f,sizeof(Lx)); memset (Ly,0x3f,sizeof(Ly));  for(intI=1; i<=n; i++)//Phase    {         for(intI=1; i<=n; i++) {Slack[i]=int_max;//Error-prone        }             for(intA;;) {memset (S),0,sizeof(S)); memset (T,0,sizeof(T)); if(Dfs (i)) Break; A=Int_max;  for(intj=1; j<=n; J + +)            {                if(!T[j]) {a=min (A, slack[j]); }            }             for(intj=1; j<=n; J + +)            {                if(S[j]) {Lx[j]-=A; }                if(T[j]) {Ly[j]+=A; }                Else{Slack[j]-=A; }            }        }    }    intres=0;  for(intI=1; i<=n; i++) {res+=W[match[i]][i]; } printf ("%d\n", res);}intMain () { for(; ~SCANF ("%d", &N); )    {         for(intI=1; i<=n; i++)        {             for(intj=1; j<=n; J + +) {scanf ("%d", w[i]+j);    }} miles (); }}

Error-prone:

I've initialized the LX, Ly, slack to 0x3f3f3f3f, resulting in DFS

Slack[v]=min (Slack[v], TMP);

Failure.

HDU 2255 Ben-well-off hair big wealth

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.