题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2426

For any school, it is hard to find a feasible accommodation plan with every student assigned to a suitable apartment while keeping everyone happy, let alone an optimal one. Recently the president of University ABC, Peterson, is facing a similar problem. While Peterson does not like the idea of delegating the task directly to the class advisors as so many other schools are doing, he still wants to design a creative plan such that no student is assigned to a room he/she dislikes, and the overall quality of the plan should be maximized. Nevertheless, Peterson does not know how this task could be accomplished, so he asks you to solve this so-called "interesting" problem for him.
Suppose that there are N students and M rooms. Each student
is asked to rate some rooms (not necessarily all M rooms) by stating how he/she
likes the room. The rating can be represented as an integer, positive value
meaning that the student consider the room to be of good quality, zero
indicating neutral, or negative implying that the student does not like living
in the room. Note that you can never assign a student to a room which he/she has
not rated, as the absence of rating indicates that the student cannot live in
the room for other reasons.
With limited information available, you've
decided to simply find an assignment such that every student is assigned to a
room he/she has rated, no two students are assigned to the same room, and the
sum of rating is maximized while satisfying Peterson's requirement. The question
is … what exactly is the answer?
 
题意描述:学校里有n个学生和m个公寓房间,每个学生对一些房间有一些打分,如果分数为正,说明学生喜欢这个房间,若为0,对这个房间保持中立,若为负,则不喜欢这个房间。学生不会住进不喜欢的房间和没有打分的房间。问安排这n个学生来求最大的分数。
算法分析:KM算法。n个学生作为X集,m个房间作为Y集,然后调用KM算法就可以了。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
const int maxn=+; int n,m;
int lx[maxn],ly[maxn],visx[maxn],visy[maxn];
int link[maxn],slack[maxn],w[maxn][maxn]; int dfs(int x)
{
visx[x]=;
for (int y= ;y<=m ;y++) if (w[x][y]!=-)
{
if (visy[y]) continue;
int t=lx[x]+ly[y]-w[x][y];
if (t==)
{
visy[y]=;
if (link[y]==- || dfs(link[y]))
{
link[y]=x;
return ;
}
}
else if (slack[y]>t) slack[y]=t;
}
return ;
} int KM()
{
memset(link,-,sizeof(link));
memset(ly,,sizeof(ly));
for (int x= ;x<=n ;x++)
{
lx[x]=-inf;
for (int y= ;y<=m ;y++)
lx[x]=max(lx[x],w[x][y]);
}
for (int x= ;x<=n ;x++)
{
for (int i= ;i<=m ;i++) slack[i]=inf;
int flag=;
for (int i= ;i<=m ;i++) if (w[x][i]!=-) flag=;
while (flag)
{
memset(visx,,sizeof(visx));
memset(visy,,sizeof(visy));
if (dfs(x)) break;
int d=inf;
for (int i= ;i<=m ;i++)
if (!visy[i] && d>slack[i]) d=slack[i];
for (int i= ;i<=n ;i++)
if (visx[i]) lx[i] -= d;
for (int i= ;i<=m ;i++)
{
if (visy[i]) ly[i] += d;
else slack[i] -= d;
}
}
}
int ans=;
int vis[maxn];
memset(vis,,sizeof(vis));
for (int i= ;i<=m ;i++)
{
if (link[i]!=-)
{
ans += w[link[i] ][i];
vis[link[i] ]=;
}
}
int i=;
for (i= ;i<=n ;i++)
if (vis[i]==) return -;
return ans;
} int main()
{
int e;
int ncase=;
while (scanf("%d%d%d",&n,&m,&e)!=EOF)
{
memset(w,-,sizeof(w));
int a,b,c;
for (int i= ;i<e ;i++)
{
scanf("%d%d%d",&a,&b,&c);
a++ ;b++ ;
if (c>=) w[a][b]=c;
}
printf("Case %d: %d\n",ncase++,KM());
}
return ;
}

hdu 2426 Interesting Housing Problem 最大权匹配KM算法的更多相关文章

  1. HDU 2426 Interesting Housing Problem (最大权完美匹配)【KM】

    <题目链接> 题目大意: 学校里有n个学生和m个公寓房间,每个学生对一些房间有一些打分,如果分数为正,说明学生喜欢这个房间,若为0,对这个房间保持中立,若为负,则不喜欢这个房间.学生不会住 ...

  2. HDU 2426 Interesting Housing Problem(二分图最佳匹配)

    http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度 ...

  3. 二分图最大权匹配——KM算法

    前言 这东西虽然我早就学过了,但是最近才发现我以前学的是假的,心中感慨万千(雾),故作此篇. 简介 带权二分图:每条边都有权值的二分图 最大权匹配:使所选边权和最大的匹配 KM算法,全称Kuhn-Mu ...

  4. HDU2255 奔小康赚大钱 —— 二分图最大权匹配 KM算法

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2255 奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    ...

  5. Hdu2255 奔小康赚大钱(二分图最大权匹配KM算法)

    奔小康赚大钱 Problem Description 传说在遥远的地方有一个非常富裕的村落,有一天,村长决定进行制度改革:重新分配房子. 这可是一件大事,关系到人民的住房问题啊.村里共有n间房间,刚好 ...

  6. Uvalive 4043 Ants —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/UVALive-4043 题意: 给出n个白点和n个黑点的坐标, 要求用n条不相交的线段把他们连接起来,其中每条线段恰好连接一个白点和黑 ...

  7. HDU3488 Tour —— 二分图最大权匹配 KM算法

    题目链接:https://vjudge.net/problem/HDU-3488 Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit ...

  8. 二分图 最大权匹配 km算法

    这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...

  9. 【HDU 2255】奔小康赚大钱 (最佳二分匹配KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

随机推荐

  1. GridView 鼠标经过时变色两种方法

    第一种: 新建一个js文件 并引用 <script src="jquery.js" type="text/javascript"></scri ...

  2. (function ( ){})( ) 与 (function ( ){}( )) 有什么区别?

    js立即执行函数: (function ( ){})( ) 与 (function ( ){}( )) 有什么区别? 转自:http://www.jb51.net/article/75089.htm ...

  3. mysql 截取字符

    $where="(left(`type_id`,1)=1"; 返回 type_id 字段 从左边开始长度为1个字符 LEFT(str,len)返回字符串str的最左面len个字符. ...

  4. 支付宝收款连接 非API

    <a href="https://shenghuo.alipay.com/send/payment/fill.htm?_form_token=mMYOrAXfReOtBBCMmoaK7 ...

  5. javascript jquery each 影响 函数返回值

    实例如下: $(function(){ if(chkInput()){ alert('OK!') } }); function chkInput(){ var data = jsonData(略); ...

  6. Autofac的注入和web.config配合

    public static void BuildMvcContainer() { var builder = new ContainerBuilder(); var assemblys = AppDo ...

  7. sublimeLinter-jshint 配置

    这几天知道sublime3有可以对javascript进行语法检查的文件,折腾了一上午,搞定了. 记录一下步骤: 1.先安装nodejs. 2.npm install jshint -g 3.通过su ...

  8. 【6.24-AppCan移动开发大会倒计时】科大讯飞来了!

    6.24 AppCan移动开发者大会进入倒计时,报名通道即将关闭! 50多家移动圈服务商将出席此次大会,讯飞开放平台也将作为参展商,为参会者带去前沿的语音技术.参会者可现场体验最新连续语音识别技术,识 ...

  9. 在C#使用文件监控对象FileSystemWatcher的几种方案

    最近在项目中有这么个需求,就是得去实时获取某个在无规律改变的文本文件中的内容.首先想到的是用程序定期去访问这个文件,因为对实时性要求很高,间隔不能超过1S,而且每次获取到文本内容都要去分发给web服务 ...

  10. RTP、RTCP

    http://blog.chinaunix.net/uid-22670933-id-1771676.html