Interesting Housing Problem hdu 2426 KM模板 +二分图最佳匹配

Interesting Housing Problem

Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2301    Accepted Submission(s): 856


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?
 

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, S i, R i, V i, (0 <= S i < N, 0 <= R i < M, |V i| <= 10000), describing the rating V i given by student S ifor room R i. 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.
 

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 5 0 1 5 0 2 7 1 1 6 1 2 3 2 4 5 1 1 1 0 0 0 1 1 0
 

Sample Output
  
  
Case 1: 18 Case 2: 0 Case 3: -1
 

Source
 

Recommend
lcy







这个题目真是很不错的,题目的意思是说有N个学生,M个宿舍,每个学生对每个宿舍有喜欢和讨厌的,还有就是
对于没有说的,一律都不住,所以需要初始化为-M,这样就对了,然后套上KM算法的模板就可以了



#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define M 0x3f3f3f3f
#define N  600
int g[N][N];
int lx[N],ly[N];
int slack[N],match[N];
bool visx[N],visy[N];
int n,m,t;
bool dfs(int cur)
{
    visx[cur]=true;
    int i,j;
    for(i=1;i<=m;i++)
    {
        if(visy[i])
        continue;
        int t=lx[cur]+ly[i]-g[cur][i];
        if(t==0)
        {
            visy[i]=true;
            if(match[i]==-1||dfs(match[i]))
            {
                match[i]=cur;
                return true;
            }
        }
         else if(slack[i]>t)
        {
            slack[i]=t;
        }


    }
    return false;
}

int KM()
{
    memset(match,-1,sizeof(match));
    memset(ly,0,sizeof(ly));
    for(int i=1;i<=n;i++)
    {
        lx[i]=-M;
        for(int j=1;j<=m;j++)
        {
            if(g[i][j]>lx[i])
            lx[i]=g[i][j];
        }
    }
    for(int x=1;x<=n;x++)
    {
        for(int i=1;i<=m;i++)
        slack[i]=M;
        while(true)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(dfs(x))break;
            int d=M;
            for(int i=1;i<=m;i++)
            {
                if(!visy[i]&&d>slack[i])
                d=slack[i];
            }
            for(int i=1;i<=n;i++)
            if(visx[i])
            {
                lx[i]-=d;
            }
            for(int i=1;i<=m;i++)
            if(visy[i])ly[i]+=d;
            else slack[i]-=d;
        }
    }
    int reslut=0,flag=0;
    for(int i=1;i<=m;i++)
    {
        if(match[i]==-1||g[match[i]][i]==-M)
        continue;
        if(match[i]>-1)
        {
            reslut+=g[match[i]][i];
            flag++;
        }
    }
    if(flag<n)reslut=-1;//通过判断flag和n,来判断匹配个数
    return reslut;
}



int main()
{
    int E=1;
    while(scanf("%d%d%d",&n,&m,&t)!=EOF)
    {
        memset(g,0,sizeof(g));
        for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
        g[i][j]=-M;
        int a,b,c;
        for(int i=1;i<=t;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            a++;b++;//我们是从0开始遍历,但是这个模板是1的
            if(c<0)continue;
            g[a][b]=c;
        }
        int ans=KM();
        printf("Case %d: %d\n",E,ans);
        E++;
    }
    return 0;
}












评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值