Problem Description
Legend has it that there is a very affluent village in faraway places, and one day the village chief decided to reform the system: redistribute the House.
It is a matter of great importance to the people's housing problem. The village has a total of n rooms, just have n people, considering every house to live (if there are people without houses, easy to cause instability factors), each must be assigned to a house and can only get a house.
On the other hand, the village chief and other village leaders hope to get the most benefit, so that the institutions of the village will be rich. As the people are richer, they can have a certain price for each house in their economy, for example, there are 3 houses, a common people can 100,000 for the first, 20,000 for the 2nd, 200,000 for the 3rd room. (Of course it's in their economy). The question now is how village leaders can allocate houses to make the most money. (even if the villagers have money to buy a house but not necessarily buy it, it depends on the village leaders).
Input
The input data contains many sets of test cases, the first row of each group of data input n, the number of the house (also the number of people), then there are n rows, each row n number of the name of the No. I village to the first room out of the price (n<=300).
Output
Please output the maximum revenue value for each set of data, one row for each group output.
Sample Input
2
15 23
Sample Output
123
train of Thought
Because the room and the people are independent, so it is easy to abstract the concept of two points, the establishment of common people to the side of the house, the right value for the bid, and then to find the maximum right to match the two-point graph.
AC Code
#include <bits/stdc++.h> using namespace std;
#define LL Long #define INF 0x3f3f3f const int MAXN = 305;
int G[MAXN][MAXN];
int nx,ny;
int LINKER[MAXN],LX[MAXN],LY[MAXN];
int SLACK[MAXN];
BOOL VISX[MAXN],VISY[MAXN];
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; x + +) {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 () {while (~SCANF ("%d", &nx)) {ny=nx;
for (int i=0; i<nx; i++) for (int j=0; j<nx; j + +) scanf ("%d", &g[i][j]);
printf ("%d\n", km ());
return 0; }