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,
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.

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

2008 Asia Hangzhou Regional
Contest Online

题意: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;
}
时间: 2024-10-30 20:22:30

hdoj--2426--Interesting Housing Problem(最大费用流)的相关文章

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 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(二分图最佳匹配)

http://acm.hdu.edu.cn/showproblem.php?pid=2426 题意:每n个学生和m个房间,现在要为每个学生安排一个房间居住,每个学生对于一些房间有一些满意度,如果满意度为负就说明该学生不喜欢住在这房间.现在问如何安排可以使所有学生的满意度总和最大.(不能将学生安排到他不喜欢的房间或者他没有评价的房间) 思路: 二分图的最佳匹配,注意题目的要求即可. 1 #include<iostream> 2 #include<cstdio> 3 #include&

HDU 2426 Interesting Housing Problem (最大权完美匹配)【KM】

<题目链接> 题目大意: 学校里有n个学生和m个公寓房间,每个学生对一些房间有一些打分,如果分数为正,说明学生喜欢这个房间,若为0,对这个房间保持中立,若为负,则不喜欢这个房间.学生不会住进不喜欢的房间和没有打分的房间.问安排这n个学生来求最大的分数,如果不能够使这些学生全部入住房间,就输出-1,每个房间最多只能住一个学生. 解题分析: 因为需要求带权二分图,所以用KM算法,需要注意的是,边权为负的两点不能进行匹配,并且,最后需要判断是否符合题意,即是否所有学生都有房间. 1 #include

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

hdoj 3395 Special Fish 【最大费用流】

Special Fish Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1920    Accepted Submission(s): 717 Problem Description There is a kind of special fish in the East Lake where is closed to campus o

HDU2426:Interesting Housing Problem(还没过,貌似入门题)

#include <iostream> #include <queue> #include <stdio.h> #include <string.h> #include <algorithm> #define inf 1000001 using namespace std; struct node { int x,y,c,w; int next; } eg[400001]; int n,m,K,s,t,tt,head[2005],dis[2005

HDU2426 Interesting Housing Problem(KM匹配 )

题意:N个学生安排到M个宿舍,每个学生对宿舍有个评价,正数,0,负数,现在评价是负数的,不能让这个学生去这个房间,问怎么安排让所有的学生都住进宿舍且评价最大. 思路:建立图的权重时,筛选掉负数边. #include<cstdio> #include<iostream> #include<algorithm> #include<cmath> #include<set> #include<map> #include<string&g