HDU 2426 Interesting Housing Problem

Interesting Housing Problem

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


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
解题思路:KM算法的模板题
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <vector>
#include <cmath>
#include <cstdlib>
#define L(rt) (rt<<1)
#define R(rt) (rt<<1|1)
#define ll long long
#define eps 1e-6
using namespace std;

const int maxn=505;
const int INF=1000000000;
int G[maxn][maxn],lx[maxn],match[maxn],ly[maxn],slack[maxn];
bool visx[maxn],visy[maxn];
int n,m;
bool find(int u)
{
    visx[u]=true;
    for(int i=0; i<m; i++)
    {
        if(visy[i]) continue;
        if(lx[u]+ly[i]==G[u][i])
        {
            visy[i]=true;
            if(match[i]==-1||find(match[i]))
            {
                match[i]=u;
                return true;
            }
        }
        else slack[i]=min(slack[i],lx[u]+ly[i]-G[u][i]);
    }
    return false;
}
void KM()
{
    memset(match,-1,sizeof(match));
    for(int i=0; i<n; i++) lx[i]=-INF;
    memset(ly,0,sizeof(ly));
    for(int i=0; i<n; i++)
        for(int j=0; j<m; j++)
            lx[i]=max(lx[i],G[i][j]);
    for(int i=0; i<n; i++)
    {
        for(int j=0; j<m; j++) slack[j]=INF;
        while(1)
        {
            memset(visx,false,sizeof(visx));
            memset(visy,false,sizeof(visy));
            if(find(i)) break;
            else
            {
                int temp=INF;
                for(int j=0; j<m; j++)
                    if(!visy[j]&&temp>slack[j]) temp=slack[j];
                for(int j=0; j<n; j++)
                    if(visx[j]) lx[j]-=temp;
                for(int j=0; j<m; j++)
                {
                    if(visy[j]) ly[j]+=temp;
                    else slack[j]-=temp;
                }
            }
        }
    }
}
int main()
{
    int cost,a,b,e,c=0;
    while(~scanf("%d%d%d",&n,&m,&e))
    {
        for(int i=0; i<n; i++)
            for(int j=0; j<m; j++)
                G[i][j]=-INF;
        while(e--)
        {
            scanf("%d%d%d",&a,&b,&cost);
            if(cost<0) continue;
            G[a][b]=cost;
        }
        KM();
        printf("Case %d: ",++c);
        int cnt=0,ans=0;
        for(int i=0; i<m; i++)
        {
            if(match[i]==-1||G[match[i]][i]==-INF) continue;
            ans+=G[match[i]][i];
            cnt++;
        }
        if(cnt<n) printf("-1\n");
        else printf("%d\n",ans);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值