"km algorithm" hdu2255-Ben-off make money

Source: Internet
Author: User

KM algorithm of nudity. O (n^4) template , in fact, in the augmented path is still redundant, you can use BFS optimization to O (n^3).

1#include <iostream>2#include <cstdio>3#include <cmath>4#include <cstring>5#include <cstdlib>6#include <algorithm>7 using namespacestd;8 Const intmaxn= -+5;9 Const intinf=0x7fffffff;Ten intn,m; One intW[MAXN][MAXN]; A intX[MAXN],Y[MAXN]; - intVISX[MAXN],VISY[MAXN],SLACK[MAXN]; - intLK[MAXN]; the  - intDfsintu) - { -visx[u]=1; +      for(intI=1; i<=n;i++) -     { +         intwt=x[u]+y[i]-W[u][i]; A         if(!visy[i] && wt==0) at         { -visy[i]=1; -             if(lk[i]==-1||DFS (Lk[i])) -             { -lk[i]=u; -                 return 1; in             } -         } to         Else if(SLACK[I]&GT;WT) slack[i]=wt; +     } -     return 0; the } *  $ intKM ()Panax Notoginseng { -memset (lk,-1,sizeof(LK)); the      for(intI=1; i<=n;i++) +     { Ay[i]=0; thex[i]=-INF; +          for(intj=1; j<=n;j++) x[i]=Max (x[i],w[i][j]); -     } $      for(intI=1; i<=n;i++) $     { -memset (VISX,0,sizeof(VISX)); -memset (Visy,0,sizeof(Visy)); thememset (Slack,127,sizeof(slack));  -          while(!DFS (i))Wuyi         { the             intDelta=INF; -              for(intj=1; j<=n;j++) Wu                 if(!visy[j] && Slack[j]<delta) delta=Slack[j]; -              for(intj=1; j<=n;j++)  About             { $                 if(Visx[j]) -                 { -x[j]-=Delta; -visx[j]=0; A                 } +                 if(Visy[j]) the                 { -y[j]+=Delta; $visy[j]=0; the                 } the             } the         }  the     } -     intret=0; in      for(intI=1; i<=n;i++) ret+=x[i]+Y[i]; the     returnret; the } About  the voidInit () the { theMemset (W,0,sizeof(w)); +      for(intI=1; i<=n;i++) -     { the          for(intj=1; j<=n;j++) Bayiscanf"%d",&w[i][j]); the     } the } -  - intMain () the { the      while(~SCANF ("%d",&N)) the     { the init (); -printf"%d\n", KM ()); the     } the     return 0; the}

"km algorithm" hdu2255-Ben-off make money

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.