Legend has it that in faraway places there is a very wealthy village, and one day the village chief decided to reform the system: redistribute the House.
This is a big event, related to people's housing problems ah. There are n rooms in the village, just there are n common people, considering every house to have a room to live (if there are people do not live, easy to cause instability factors), each family must be assigned to a house and can only get a house.
On the other hand, village chiefs and other village leaders hope to get the most benefit, so that the village's institutions will be rich. Because people are richer, they can have a certain price for each house in their economy, for example, there are 3 houses, and a common man can give 100,000 to the first, 20,000 to the 2nd. 200,000 for the 3rd room. (in their economy, of course). The question now is how village leaders can allocate their homes to make the most of their income. (Villagers who have the money to buy a house but not necessarily can buy it depends on what the village leader assigns).
Binary graph best match, km algorithm bare topic
1#include <stdio.h>2#include <string.h>3#include <algorithm>4#include <math.h>5 using namespacestd;6 Const intinf=0x3f3f3f3f;7 Const intmaxn=505;8 intg[maxn][maxn],match[maxn],lx[maxn],ly[maxn],visx[maxn],visy[maxn],s[maxn],n,m;9 BOOLHungaryintu) {Tenvisx[u]=1; One for(intI=1; i<=n;++i) { A if(!visy[i]&&lx[u]+ly[i]==G[u][i]) { -visy[i]=1; - if(!match[i]| |Hungary (Match[i])) { thematch[i]=u; - return 1; - } - } + Else if(!visy[i]) s[i]=min (s[i],lx[u]+ly[i]-g[u][i]); - } + return 0; A } at intKM () { - for(intI=1; i<=n;++i) { - for(intj=1; j<=n;++j) s[j]=INF; - while(1){ -memset (VISX,0,sizeof(VISX)); -memset (Visy,0,sizeof(Visy)); in if(Hungary (i)) Break; - intD=INF; to for(intj=1; j<=n;++j) + if(!visy[j]) d=min (d,s[j]); - for(intj=1; j<=n;++j) { the if(Visx[j]) lx[j]-=D; * if(Visy[j]) ly[j]+=D; $ Elses[j]-=D;Panax Notoginseng } - } the } + intans=0; A for(intI=1; i<=n;++i) ans+=G[match[i]][i]; the returnans; + } - intMain () { $ while(SCANF ("%d", &n)! =EOF) { $memset (LX,0,sizeof(LX)); -memset (Ly,0,sizeof(ly)); -memset (Match,0,sizeof(Match)); the for(intI=1; i<=n;++i) { - for(intj=1; j<=n;++j) {Wuyiscanf"%d",&g[i][j]); thelx[i]=Max (lx[i],g[i][j]); - } Wu } -printf"%d\n", KM ()); About } $ return 0; -}
View Code
hdu2255 Ben well-off make money the best matching--km algorithm of binary graph