Ben-off make a lot of moneyTime
limit:1000MS
Memory Limit:32768KB
64bit IO Format:%i64d &%i64 U SubmitStatusPracticeHDU 2255
Description
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).
Input
The input data contains multiple sets of test cases, the first line of each set of data entered N, indicating the number of houses (also the number of people in the home), followed by n rows, the number of n per row represents the first village name of the room price (n<=300).
Output
Please output the maximum revenue value for each set of data, one row for each group of outputs.
Sample Input
2100 1015 23
Sample Output
123 Problem-solving ideas: the best perfect match. Set of templates on the line.
#include <stdio.h> #include <algorithm> #include <string.h> #include <vector>using namespace std;const int MAXN = 550;const int INF = 0x3f3f3f3f;int NX, NY, LX[MAXN], Ly[maxn];bool VISX[MAXN], Visy[maxn];int linker[ MAXN], slack[maxn];int g[maxn][maxn];bool dfs (int x) {visx[x] = 1; The Hungarian tree node for Mark X for (int y = 1; y <= ny; y++) {if (visy[y]) continue; int tmp = Lx[x] + ly[y]-g[x][y]; if (TMP = = 0) {//Found a new edge visy[y] = 1 that can be added to the equal sub-graph; The Hungarian tree node labeled y if (linker[y] = = 1 | | DFS (LINKER[Y])) {//found the augmented path linker[y] = x; return true; }}else if (Slack[y] > tmp) {//Update slack amount slack[y] = tmp; }} return false;} int KM () {memset (LINKER,-1,SIZEOF (linker)); Match Array memset (ly,0,sizeof (ly)); Initializes the Y-node top-mark for (int i = 1; I <= nx; i++) {lx[i] =-inf; for (int j = 1; J <= NY; j + +) {if (Lx[i] < g[i][j]) { Lx[i] = G[i][j]; Initialize the X-node top-mark}}} for (int x = 1; x <= NX; + x + +) {//Find the augmented path for each x for (int i = 1; I <= n Y i++) {//Initialize relaxation amount slack[i] = INF; } while (true) {memset (visx,0,sizeof (VISX)); memset (visy,0,sizeof (Visy)); if (Dfs (x)) break; Found the augmented path int d = INF; for (int i = 1; I <= ny; i++) {if (!visy[i] && d > slack[i]) {//yi Not in the Hungarian tree (interlaced tree) d = slack[i]; }} for (int i = 1; I <= nx; i++) {//xi in the Hungarian tree, update the top if (Visx[i]) { Lx[i]-= D; }} for (int i = 1; I <= ny; i++) {if (Visy[i]) {//yi in the Hungarian tree, update the top label Ly[i] + = D; }else{//Update relaxation amount Slack[i]-= D; }}}} intret = 0; for (int i = 1; I <= ny; i++) {//summation if (linker[i]! =-1) {ret + = G[linker[i]][i]; }} return ret;} int main () {int n; while (scanf ("%d", &n)!=eof) {memset (g,0,sizeof (G)); int C; for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j + +) {scanf ("%d", &c); G[I][J] = c; }} NX = NY = n; int res = KM (); printf ("%d\n", res); } return 0;}
HDU 2255--ben off to make money —————— "km algorithm bare topic"