Interesting Housing Problem HDU - 2426 (KM)

Interesting Housing Problem

HDU - 2426

题意:n个人,m个房间,安排住宿。要求每个人不能分到不喜欢的房间,且使满意度最大。

不用slack几乎要超时~

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int inf=0x3f3f3f3f;
 4 const int maxn=550;
 5
 6 int c[maxn][maxn];
 7 int vb[maxn],vg[maxn],eb[maxn],eg[maxn];
 8 int mc[maxn];
 9 int slack[maxn];
10 int n,m,r;
11
12 int dfs(int id){
13     vg[id]=1;
14     for(int i=0;i<m;i++){
15         int gap=eg[id]+eb[i]-c[id][i];
16         if(vb[i]) continue;
17         if(gap==0){
18             vb[i]=1;
19             if(mc[i]==-1||dfs(mc[i])){
20                 mc[i]=id;
21                 return 1;
22             }
23         }
24         else slack[i]=min(slack[i],gap);
25     }
26     return 0;
27 }
28
29 int KM()
30 {
31     memset(mc,-1,sizeof(mc));
32     memset(eb,0,sizeof(eb));
33     for(int i=0;i<n;i++){
34         eg[i]=c[i][0];
35         for(int j=1;j<m;j++)
36             eg[i]=max(eg[i],c[i][j]);
37     }
38
39     for(int i=0;i<n;i++){
40         memset(slack,inf,sizeof(slack));
41         while(1){
42             memset(vb,0,sizeof(vb));
43             memset(vg,0,sizeof(vg));
44             if(dfs(i)) break;
45             int d=inf;
46             for(int j=0;j<m;j++) if(!vb[j]) d=min(d,slack[j]);
47             if(d==inf) return -1;
48             for(int j=0;j<n;j++) if(vg[j]) eg[j]-=d;
49             for(int j=0;j<m;j++)
50                 if(vb[j]) eb[j]+=d;
51                 else slack[j]-=d;
52         }
53     }
54     int ans=0;
55     for(int i=0;i<m;i++){
56         if(mc[i]!=-1){
57             if(c[mc[i]][i]!=-inf) ans+=c[mc[i]][i];
58             else return -1;
59         }
60     }
61     return ans;
62 }
63
64 int main(){
65     int u,v,w;
66     int kase=0;
67     while(scanf("%d%d%d",&n,&m,&r)!=EOF){
68        for(int i=0;i<n;i++)
69         for(int j=0;j<m;j++)
70         c[i][j]=-inf;
71         for(int i=0;i<r;i++){
72             scanf("%d%d%d",&u,&v,&w);
73             if(w>=0) c[u][v]=w;
74         }
75         printf("Case %d: %d\n",++kase,KM());
76     }
77 }

slack

时间: 2024-10-12 03:04:19

Interesting Housing Problem HDU - 2426 (KM)的相关文章

hdu 2426 Interesting Housing Problem 最大权匹配KM算法

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

HDU 2426 Interesting Housing Problem(KM完美匹配)

HDU 2426 Interesting Housing Problem 题目链接 题意:n个学生,m个房间,给定一些学生想住房的喜欢度,找一个最优方案使得每个学生分配一个房间,并且使得喜欢度最大,注意这题有个坑,就是学生不会住喜欢度为负的房间 思路:很明显的KM最大匹配问题,喜欢度为负直接不连边即可 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm>

hdu 2426 Interesting Housing Problem (KM算法)

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

hdu 1874(Dijkstra )

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 27692    Accepted Submission(s): 10019 Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路

hdu 4194(模拟)

符合三者之一的则不满足规定,求不满足规定的个数.直接模拟. 1.被同一个人审查多次 2.被和自己同一组织的审查 3.被审查次数不等于k 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 17:36 5 * Filename :

Hdu 1402 (FFT)

题目链接 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12490    Accepted Submission(s): 2206 Problem Description Calculate A * B. Input Each line will contain two integers A and

hdu 1800 (map)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10830    Accepted Submission(s): 3472 Problem Description In the year 8888, t

hdu 1869 (Floyd)

http://acm.hdu.edu.cn/showproblem.php?pid=1869 六度分离 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4355    Accepted Submission(s): 1768 Problem Description 1967年,美国著名的社会学家斯坦利·米尔格兰姆提出了一个名为“小世界现象

hdu 4196(数论)

题意:问小于n的数的乘积能拼成的最大平方数是多少? 思路:给n!做质数分解在除去指数为奇数的那些质数,由于题目中需要模运算所以不能直接除,必须乘上摸逆. 代码如下: 1 /************************************************** 2 * Author : xiaohao Z 3 * Blog : http://www.cnblogs.com/shu-xiaohao/ 4 * Last modified : 2014-06-28 15:26 5 * Fi