Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=2255
Problem description legend in a faraway place there is a very wealthy village, 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).
Input data contains multiple sets of test cases, the first row of each set of data input n, indicating the number of houses (also the number of people home), followed by n rows, the number of n per row represents the price of the room of the second village name (n<=300).
Output make the maximum revenue value for each set of data, one row for each set of outputs.
Sample Input
2100 1015 23
Sample Output
123
Sourcehdoj Summer Exercise (4)-Buffet Dinner
Ps:
KM algorithm solves the maximum minimum matching problem with weights!
The code is as follows:
#include <stdio.h> #include <string.h> #include <algorithm> #include <iostream>using namespace STD; #define INF 0x3f3f3f3f/* miles algorithm * complexity O (nx*nx*ny) * For maximum weight matching * If minimum weight matching is obtained, the weights can be reversed, the result is the opposite number * point number starting from 0 */const int N = 317 int nx,ny;//two points int g[n][n];//binary map describes the point matching state in int linker[n],lx[n],ly[n];//y, the dot designator in x, y, int slack[n];bool visx[n],visy[n] ; bool DFS (int x) {visx[x] = true; for (int y = 0; y < ny; y++) {if (visy[y]) continue; int tmp = Lx[x] + ly[y]-g[x][y]; if (TMP = = 0) {Visy[y] = true; if (linker[y] = =-1 | | DFS (Linker[y])) {linker[y] = x; return true; }} else if (Slack[y] > tmp) slack[y] = tmp; } return false;} int KM () {memset (LINKER,-1,SIZEOF (linker)); memset (ly,0,sizeof (ly)); for (int i = 0; i < NX; i++) {lx[i] =-inf; for (int j = 0; J < NY; j + +) if (G[i][j] > Lx[i]) Lx[i] = G[i][j]; } for (int x = 0; x < NX; + +) {for (int i = 0; i < NY; i++) Slack[i] = INF; while (true) {memset (visx,false,sizeof (VISX)); memset (visy,false,sizeof (Visy)); if (DFS (x)) break; int d = INF; for (int i = 0; i < NY; i++) if (!visy[i] && d > slack[i]) d = slack[i]; for (int i = 0; i < NX; i++) if (Visx[i]) lx[i]-= D; for (int i = 0; i < NY; i++) {if (Visy[i]) ly[i] + = D; else Slack[i]-= D; }}} int res = 0; for (int i = 0; i < NY; i++) if (linker[i]! = 1) Res + = G[linker[i]][i]; return res;} int main () {int n; while (~SCANF ("%d", &n)) {for (int i = 0; i < n; i++) {for (int j = 0; J < N; j + +) scanf ("%d", &g[i][j]);} NX = NY = n; int ans = KM (); printf ("%d\n", ans); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 2255 Ben-off make a lot of money (binary matching km algorithm)