题目链接: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 ;
}

最新文章

  1. shareSDK实现分享操作时只显示英文字体
  2. 【mysql】Infobright和mysql数据入库性能测试
  3. WebStorm 10.0.4注册码
  4. 配置 Cocoapods的简单配置及胡思乱想
  5. How to: Read Object Data from an XML File
  6. 近期最久未使用页面淘汰算法———LRU算法(java实现)
  7. Java遍历所有网卡打印对应IP
  8. 使用的 SQL Server 版本不支持数据类型“datetime2”解决办法
  9. struts2 OGNL 表达式
  10. HDU 1005(周期问题)
  11. BZOJ 1620: [Usaco2008 Nov]Time Management 时间管理( 二分答案 )
  12. ufldl学习笔记和编程作业:Feature Extraction Using Convolution,Pooling(卷积和汇集特征提取)
  13. java SpringWeb 接收安卓android传来的图片集合及其他信息入库存储
  14. Java第8次实验(IO流)
  15. Redis的java客户端jedis
  16. EJB的魅惑来源
  17. C# WebAPI中使用Swagger
  18. MySQL-事务隔离级别设置
  19. nexus 使用Raw Repositories 进行maven site 发布
  20. 自学git心得-5

热门文章

  1. SQL Server编程(05)游标
  2. PHP伪造referer突破防盗链
  3. PHP函数:生成N个不重复的随机数
  4. ubuntu 12.04 LTS(64位)安装apache2
  5. 【转】mysql字符串函数
  6. Delphi XE5 for android 调用Java类库必看的文件
  7. Mapreduce中的字符串编码
  8. 03-树2 List Leaves
  9. sqlalchemy - day2
  10. 【J2EE】Java连接SQL Server 2000问题:“com.microsoft.sqlserver.jdbc.SQLServerException:用户&#39;sa&#39;登录失败。该用户与可信SQL Server连接无关联”