hdoj--2426--Interesting Housing Problem(最大费用流)



Interesting Housing Problem

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


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

题意:n个人,m间房,每个人都有喜欢的还有不喜欢的房间,他们对一些房间也会保持中立状态,a,b,c表示第a个人对第b个房间的态度,c>0表示喜欢,等于零表示中立,小于零就是不喜欢,问分配之后最大的评价值可以是多少
很简单的最大费用流,超级源点超级汇点分别对人还有房间建边,边权为1,费用为,c>=0的时候a向c建边,边权为1,费用为c
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 5555
#define MAXM 501000
#define INF 0x3f3f3f3f
int pre[MAXN],vis[MAXN],head[MAXN],n,m,e,dis[MAXN],cnt;
int source,sink;
struct node
{
	int u,v,cap,flow,cost,next;
}edge[MAXM];
void init()
{
	memset(head,-1,sizeof(head));
	cnt=0;
}
void add(int a,int b,int c,int d)  
{  
    node E={a,b,c,0,d,head[a]};  
    edge[cnt]=E;  
    head[a]=cnt++;  
    node E1={b,a,0,0,-d,head[b]};  
    edge[cnt]=E1;  
    head[b]=cnt++;  
}  
bool BFS(int s,int t)  
{  
    memset(vis,0,sizeof(vis));  
    memset(dis,-INF,sizeof(dis));  
    memset(pre,-1,sizeof(pre));  
    vis[s]=1;  
    dis[s]=0;  
    queue<int>q;  
    q.push(s);  
    while(!q.empty())  
    {  
        int u=q.front();  
        q.pop();  
        vis[u]=0;  
        for(int i=head[u];i!=-1;i=edge[i].next)  
        {  
            node E=edge[i];  
            if(dis[E.v]<dis[u]+E.cost&&E.cap>E.flow)  
            {  
                dis[E.v]=dis[u]+E.cost;  
                pre[E.v]=i;  
                if(!vis[E.v])  
                {  
                    q.push(E.v);  
                    vis[E.v]=1;  
                }  
            }  
        }  
    }  
    return pre[t]!=-1;  
}  
void mcmf(int s,int t,int &cost,int &flow)  
{  
    flow=cost=0;  
    while(BFS(s,t))  
    {  
        int Min=INF;  
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])  
        {  
            node E=edge[i];  
            Min=min(Min,E.cap-E.flow);  
        }  
        for(int i=pre[t];i!=-1;i=pre[edge[i^1].v])  
        {  
            edge[i].flow+=Min;  
            edge[i^1].flow-=Min;  
            cost+=edge[i].cost*Min;  
        }  
        flow+=Min;  
    }  
}  
void getmap()
{
	int a,b,c;
	for(int i=0;i<e;i++)
	{
		scanf("%d%d%d",&a,&b,&c);
		if(c>=0)
			add(a+1,b+1+n,1,c);
	}
	for(int i=1;i<=n;i++)
		add(source,i,1,0);
	for(int i=1;i<=m;i++)
		add(i+n,sink,1,0);
}
int main()
{
	int k=1;
	while(scanf("%d%d%d",&n,&m,&e)!=EOF)
	{
		init();
		source=0,sink=n+m+1;
		getmap();
		int cost,flow;
		mcmf(source,sink,cost,flow);
		printf("Case %d: ",k++);
		if(flow==n)
			printf("%d\n",cost);
		else
			printf("-1\n");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值