hdu-2255.奔小康赚大钱(最大权二分匹配)

奔小康赚大钱

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


Problem Description
传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子。
这可是一件大事,关系到人民的住房问题啊。村里共有n间房间,刚好有n家老百姓,考虑到每家都要有房住(如果有老百姓没房子住的话,容易引起不安定因素),每家必须分配到一间房子且只能得到一间房子。
另一方面,村长和另外的村领导希望得到最大的效益,这样村里的机构才会有钱.由于老百姓都比较富裕,他们都能对每一间房子在他们的经济范围内出一定的价格,比如有3间房子,一家老百姓可以对第一间出10万,对第2间出2万,对第3间出20万.(当然是在他们的经济范围内).现在这个问题就是村领导怎样分配房子才能使收入最大.(村民即使有钱购买一间房子但不一定能买到,要看村领导分配的).
 
Input
输入数据包含多组测试用例,每组数据的第一行输入n,表示房子的数量(也是老百姓家的数量),接下来有n行,每行n个数表示第i个村名对第j间房出的价格(n<=300)。
 
Output
请对每组数据输出最大的收入值,每组的输出占一行。

 
Sample Input
2 100 10 15 23
 
Sample Output
123
 
Source
 
Recommend
lcy

抱着试一试的心态读了一下题,果然是裸模版题...敲了一波就过了

 1 /*************************************************************************
 2     > File Name: hdu-2255.奔小康赚大钱.cpp
 3     > Author: CruelKing
 4     > Mail: [email protected] 
 5     > Created Time: 2019年09月04日 星期三 00时01分32秒
 6     本题思路:最大权完备匹配裸题
 7  ************************************************************************/
 8 
 9 #include <cstdio>
10 #include <cstring>
11 using namespace std;
12 
13 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 }

猜你喜欢

转载自www.cnblogs.com/bianjunting/p/11456575.html