hdu-2255. better off big money (maximum weight bipartite matching)

Well-off big money

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 17652    Accepted Submission(s): 7436


Problem Description
Legend in faraway places have a very wealthy village, one day, the mayor decided to carry out institutional reforms: re-distribution house.
This is a major event in relation to the housing problems of the people ah. In the village there are n rooms, there are n just ordinary people, taking into account every household should have room to live (if there are people out of homes, it is easy to cause unrest), 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 maximum benefit, this village institutions will have the money. Because people are more prosperous, they can each house a certain price within the range of their economic , for example, there are three houses, one can people out 100,000 pairs between the first, the first two out of 20,000, to 200,000 between the first three. (of course, within the scope of their economy). now the problem is that village how to lead the largest distribution house to make income. (the villagers even have money to buy a house, but not necessarily be able to buy, look at the distribution of village leaders).
 

 

Input
Comprising a plurality of sets of test input data, a first data input line of each n, represents the number of (the number of people are at home) of the house, then there are n rows, each row number n i denotes the j th village name the room price (n <= 300).
 

 

Output
Please deliver maximum revenue values ​​for each set of data, each set of output per line.

 

 

Sample Input
2 100 10 15 23
 

 

Sample Output
123
 

 

Source
 

 

Recommend
lcy

 

Try holding the mentality to read a little problem, it really is bare template title ... knock on after a wave

1  / * *********************************************** *************************
 2      > the Name File:. 2255 HDU-off society a lot of money .cpp
 3      > Author: CruelKing
 4      > Mail: 2,016,586,625 qq.com @ 
 5      > the Created time: 2019 Nian 09 Yue 04 Wednesday, 00 minutes and 32 seconds 01
 6      this question ideas: maximum weight perfect matching problem naked
 7  ***************** ************************************************** **** * / 
. 8  
. 9 #include <cstdio>
 10 #include <CString>
 . 11  the using  namespace STD;
 12 is  
13 is  const  int MAXN = 300 + . 5, inf = 0x3f3f3f3f;
14 int n, g[maxn][maxn];
15 int linker[maxn], lx[maxn], ly[maxn], slack[maxn];
16 bool visy[maxn], visx[maxn];
17 
18 bool dfs(int x) {
19     visx[x] = true;
20     for(int y = 1; y <= n; y ++) {
21         if(visy[y]) continue;
22         int temp = lx[x] + ly[y] - g[x][y];
23         if(temp == 0) {
24             visy[y] = true;
25             if(linker[y] == -1 || dfs(linker[y])) {
26                 linker[y] = x;
27                 return true;
28             }
29         }
30         else if(slack[y] > temp) slack[y] = temp;
31     }
32     return false;
33 }
34 
35 int km() {
36     memset(linker, -1, sizeof linker);
37     memset(ly, 0, sizeof ly);
38     for(int i = 1; i <= n; i ++) {
39         lx[i] = -inf;
40         for(int j = 1; j <= n; j ++) 
41             if(g[i][j] > lx[i])    lx[i] = g[i][j];
42     }
43     for(int x = 1; x <= n; x ++) {
44         for(int i = 1; i <= n; i ++)
45             slack[i] = inf;
46         while(true) {
47             memset(visx, false, sizeof visx);
48             memset(visy, false, sizeof visy);
49             if(dfs(x)) break;
50             int d = inf;
51             for(int i = 1; i <= n; i ++) 
52                 if(!visy[i] && d > slack[i]) d = slack[i];
53             for(int i = 1; i <= n; i ++)
54                 if(visx[i]) lx[i] -= d;
55             for(int i = 1; i <= n; i ++) {
56                 if(visy[i]) ly[i] += d;
57                 else slack[i] -= d;
58             }
59         }
60     }
61     int res = 0;
62     for(int i = 1; i <= n; i ++) 
63         if(linker[i] != -1) res += g[linker[i]][i];
64     return res;
65 }
66 
67 int main() {
68     while(~scanf("%d", &n)) {
69         for(int i = 1; i <= n; i ++) {
70             for(int j = 1; j <= n; j ++) {
71                 scanf("%d", &g[i][j]);
72             }
73         }
74         printf("%d\n", km());
75     }
76     return 0;
77 }

 

Guess you like

Origin www.cnblogs.com/bianjunting/p/11456575.html