奔小康赚大钱

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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
 
 
解题思路:最优完美匹配。套模板就行了。
 
#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; //标记X的匈牙利树节点
for(int y = 1; y <= ny; y++){
if(visy[y]) continue;
int tmp = lx[x] + ly[y] - G[x][y];
if(tmp == 0){ //找到了一条可以加入相等子图的新边
visy[y] = 1; //标记Y的匈牙利树节点
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)); //初始化Y节点顶标
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]; //初始化X节点顶标
}
}
}
for(int x = 1; x <= nx; x++){ //找每个X的增广路
for(int i = 1; i <= ny; i++){ //初始化松弛量
slack[i] = INF;
}
while(true){
memset(visx,0,sizeof(visx));
memset(visy,0,sizeof(visy));
if(dfs(x)) break; //找到了增广路
int d = INF;
for(int i = 1; i <= ny; i++){
if(!visy[i] && d > slack[i]){ //Yi不在匈牙利树(交错树)中
d = slack[i];
}
}
for(int i = 1; i <= nx; i++){ //Xi在匈牙利树中,更新顶标
if(visx[i]){
lx[i] -= d;
}
}
for(int i = 1; i <= ny; i++){
if(visy[i]){ //Yi在匈牙利树中,更新顶标
ly[i] += d;
}else{ //更新松弛量
slack[i] -= d;
}
}
}
}
int ret = 0;
for(int i = 1; i <= ny; i++){ //求和
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;
}

  

 

最新文章

  1. CodeTimerPerformance EasyPerformanceCounterHelper .NET 4.5
  2. python hashlib模块
  3. Prefab Assist插件
  4. YChaos生成混沌图像
  5. Web Component--01. 简介
  6. 图解Android - Binder 和 Service
  7. jeecg团队招新人(5人)
  8. js隔行变色
  9. VS2013中实现angular代码智能提示
  10. 获取所有input值 处理成json格式再利用$.post提交
  11. 全面理解SSD和NAND Flash
  12. Python s12 Day2 笔记及作业
  13. iis7 部署网站 403错误
  14. 58、js扩展
  15. Django_'utf-8' codec can't decode 问题解决
  16. 【开发技术】storyboard和nib的差别
  17. SpringMVC源码情操陶冶-ResourcesBeanDefinitionParser静态资源解析器
  18. 自动生成业务库码表insert脚本
  19. Winform中Picture控件图片的拖拽显示
  20. 解决java.lang.IllegalArgumentException: No converter found for return value of type: class java.util.ArrayList的问题

热门文章

  1. Login failed for user &#39;xxx&#39;
  2. Insus Paging Utility Version 2
  3. Mybatis基于代理Dao实现CRUD操作 及 Mybatis的参数深入
  4. 识别子串 (string)——后缀自动机+线段树
  5. 【51Nod 1363】最小公倍数之和(欧拉函数)
  6. 2、OpenCV Python 图像属性获取
  7. P4724 【模板】三维凸包
  8. 背包DP【洛谷P2113】 看球泡妹子
  9. luogu3911 最小公倍数之和(莫比乌斯反演)
  10. tomcat启动优化