Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2255
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).Test Instructions Description: Because this is titled Chinese title, this is not translated here. algorithm Analysis: maximum weight matching problem,km algorithm for the entry question. Baidu km algorithm has a lot of relevant introduction, here no longer described. Explanation Point, km optimization: The slack array saves the value min (Lx[x]-ly[y]-w[x][y]) of each y in the Y-set (binary graph: X-Set and Y-set) that is not in the equality sub-graph when DFS is seeking an augmented path, and the final algorithm complexity is O (n^3). not very understanding of the compilation knowledge, why this problem g++ submitted tle and C + + submitted AC, feel very high and very advanced appearance.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7#include <vector>8#include <queue>9 #defineINF 0x7fffffffTen using namespacestd; One Const intmaxn= -+Ten; A - intN,nx,ny; - intLX[MAXN],LY[MAXN],VISX[MAXN],VISY[MAXN]; the intLINK[MAXN],SLACK[MAXN]; - intW[MAXN][MAXN]; - - intDfsintx) + { -visx[x]=1; + for(inty=1; y<=ny; y++.) A { at if(Visy[y])Continue; - intt=lx[x]+ly[y]-W[x][y]; - if(t==0) - { -visy[y]=1; - if(link[y]==-1||DFS (Link[y])) in { -link[y]=x; to return 1; + } - } the Else if(slack[y]>t) slack[y]=T; * } $ return 0;Panax Notoginseng } - the intKM () + { Amemset (Ly,0,sizeof(ly)); thememset (link,-1,sizeof(link)); + for(intx=1; x<=nx; x + +) - { $lx[x]=-inf; $ for(inty=1; y<=ny; y++.) -lx[x]=Max (lx[x],w[x][y]); - } the for(intx=1; x<=nx; x + +) - {Wuyi for(intj=1; j<=ny; j + +) slack[j]=inf; the while(1) - { Wumemset (VISX,0,sizeof(VISX)); -memset (Visy,0,sizeof(Visy)); About if(Dfs (x)) Break; $ intD=inf; - for(intj=1; j<=ny; j + +) - { - if(!visy[j] && slack[j]<d) AD=Slack[j]; + } the for(intI=1; i<=nx; i++.) - if(Visx[i]) lx[i]-=D; $ for(intI=1; i<=ny; i++.) the { the if(Visy[i]) ly[i] + =D; the ElseSlack[i]-=D; the } - } in } the intans=0; the for(intI=1; i<=ny; i++.) About if(link[i]!=-1) ans + =W[link[i]][i]; the returnans; the } the + intMain () - { the while(SCANF ("%d", &n)! =EOF)Bayi { thenx=ny=N; the for(intI=1; i<=n; i++.) - { - for(intj=1; j<=n; j + +) thescanf"%d",&w[i][j]); the } theprintf"%d\n", KM ()); the } - return 0; the}
HDU 2255 Ben-off make big money maximum weight match km