hdu 2426 Interesting Housing Problem (KM算法)
来源:互联网 发布:批量拿webshell工具 编辑:程序博客网 时间:2024/05/16 10:49
Interesting Housing Problem
Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2388 Accepted Submission(s): 879
Problem Description
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?
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?
Input
There are multiple test cases in the input file. Each test case begins with three integers, N, M, and E (1 <= N <= 500, 0 <= M <= 500, 0 <= E <= min(N * M, 50000)), followed by E lines, each line containing three numbers, Si, Ri, Vi, (0 <= Si < N, 0 <= Ri < M, |Vi| <= 10000), describing the rating Vi given by student Sifor room Ri. It is guaranteed that each student will rate each room at most once.
Each case is followed by one blank line. Input ends with End-of-File.
Each case is followed by one blank line. Input ends with End-of-File.
Output
For each test case, please output one integer, the requested value, on a single line, or -1 if no solution could be found. Use the format as indicated in the sample output.
Sample Input
3 5 50 1 50 2 71 1 61 2 32 4 51 1 10 0 01 1 0
Sample Output
Case 1: 18Case 2: 0Case 3: -1
Source
2008 Asia Hangzhou Regional Contest Online
m!=n,把边权为负数的不读入就AC了
#include"stdio.h"#include"string.h"#define N 505#define mmax(a,b) ((a)>(b)?(a):(b))#define mmin(a,b) ((a)<(b)?(a):(b))const int inf=(int)1e8;int g[N][N],slack[N],n,m;int link[N],lx[N],ly[N];int visx[N],visy[N];int find(int k){ int i; visx[k]=1; for(i=0;i<m;i++) { if(visy[i]) continue; int d=lx[k]+ly[i]-g[k][i]; if(d==0) { visy[i]=1; if(link[i]==-1||find(link[i])) { link[i]=k; return 1; } } else slack[i]=mmin(slack[i],d); } return 0;}int KM(){ int i,j; memset(ly,0,sizeof(ly)); memset(link,-1,sizeof(link)); for(i=0;i<n;i++) { lx[i]=-inf; for(j=0;j<m;j++) { lx[i]=mmax(lx[i],g[i][j]); } } for(i=0;i<n;i++) { for(j=0;j<m;j++) { slack[j]=inf; } while(1) { memset(visx,0,sizeof(visx)); memset(visy,0,sizeof(visy)); if(find(i)) break; else { int d=inf; for(j=0;j<m;j++) { if(!visy[j]) d=mmin(slack[j],d); } for(j=0;j<n;j++) { if(visx[j]) lx[j]-=d; } for(j=0;j<m;j++) { if(visy[j]) ly[j]+=d; } } } } int ans=0,cnt=0; for(i=0;i<m;i++) { if(link[i]==-1) continue;if(g[link[i]][i]<0)break;cnt++; ans+=g[link[i]][i]; } if(cnt<n) return -1; return ans;}int main(){ int i,j,u,v,w,e,cnt=1; while(scanf("%d%d%d",&n,&m,&e)!=-1) { int max=mmax(n,m); for(i=0;i<max;i++) { for(j=0;j<max;j++) { g[i][j]=-inf; } } while(e--) { scanf("%d%d%d",&u,&v,&w); if(w>=0)g[u][v]=w; }printf("Case %d: %d\n",cnt++,KM()); } return 0;}
0 0
- hdu 2426 Interesting Housing Problem (KM算法)
- hdu 2426 Interesting Housing Problem 最大权值 && KM算法
- hdu 2426 Interesting Housing Problem (KM)
- hdu 2426 Interesting Housing Problem(KM)
- hdu 2426 Interesting Housing Problem【KM】
- HDU 2426 Interesting Housing Problem(KM完美匹配)
- Interesting Housing Problem----KM
- HDU 2426 Interesting Housing Problem 最小费用最大流 or KM算法
- HDU 2426 Interesting Housing Problem
- HDU-2426 Interesting Housing Problem
- hdu 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem
- HDU 2426 Interesting Housing Problem 二分匹配(KM模板)或者最小费用最大流
- Interesting Housing Problem hdu 2426 KM模板 +二分图最佳匹配
- hdu2426 Interesting Housing Problem (KM邻接表)
- HDU2426 Interesting Housing Problem(KM匹配 )
- hdoj 2246 Interesting Housing Problem 【最大费用最大流 or KM算法】
- Logistic Regression 分类算法
- Android开发使用GET方式向服务器请求和发送数据
- 五大常用算法之五:分支限界法
- 在使用Myeclipse项目转成eclipse时出现java.lang.NoClassDefFoundError:EDU错误的解决方案
- 手机格式化了图片怎么恢复
- hdu 2426 Interesting Housing Problem (KM算法)
- python3.x的异常处理
- POJ 1459 Power Network
- 每周一多校联合
- MySQL range分区(二)
- 两个手机号合并生成64位整数
- UVA10305
- 视频播放器
- sakai开发环境及运行环境搭建