【POJ 1149】PIGS

【题目】

传送门

Description

Mirko works on a pig farm that consists of M locked pig-houses and Mirko can’t unlock any pighouse because he doesn’t have the keys. Customers come to the farm one after another. Each of them has keys to some pig-houses and wants to buy a certain number of pigs.

All data concerning customers planning to visit the farm on that particular day are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.

More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses.

An unlimited number of pigs can be placed in every pig-house.

Write a program that will find the maximum number of pigs that he can sell on that day.

Input

The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.

The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is greater or equal to 0 and less or equal to 1000.

The next N lines contains records about the customers in the following form ( record about the i-th customer is written in the (i+2)-th line):

A K1 K2 … KA B It means that this customer has key to the pig-houses marked with the numbers K1, K2, …, KA (sorted nondecreasingly ) and that he wants to buy B pigs. Numbers A and B can be equal to 0.

Output

The first and only line of the output should contain the number of sold pigs.

Sample Input

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7


【分析】

大致题意:有 m m m 个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依次来了 n n n 个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共最多能卖出多少头猪。

一道好的网络流建模

每个顾客打开了某几个猪圈,相当于在那个时间段里将那几个猪圈"合并"了

然后如果以后有人要打开其中的某一个猪圈,只用找到合并的这个点,然后连边,相当于是里面的猪都可以被调换过来。

对于每个猪圈维护 l a s t i last_i lasti,表示最后合并的那个猪圈,然后每次从 l a s t i last_i lasti 连边(不是 i i i),最后把 l a s t i last_i lasti 更新即可。

至于为什么不是从 i i i 连边,因为它可能应经被"合并"到其它猪圈了,直接从大猪圈连边就行了


【代码】

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10005
#define M 1000005
#define inf (1ll<<31ll)-1
using namespace std;
int n,m,s,t,num=1;
int v[M],w[M],next[M];
int d[N],f[N],last[N],first[N];
void add(int x,int y,int z)
{
	num++;
	next[num]=first[x];
	first[x]=num;
	v[num]=y;
	w[num]=z;
}
bool bfs()
{
	int x,y,i,j;
	memset(d,-1,sizeof(d));
	memcpy(f,first,sizeof(f));
	queue<int>q;d[s]=0;q.push(s);
	while(!q.empty())
	{
		x=q.front();q.pop();
		for(i=first[x];i;i=next[i])
		{
			y=v[i];
			if(w[i]&&d[y]==-1)
			{
				d[y]=d[x]+1;
				if(y==t)
				  return true;
				q.push(y);
			}
		}
	}
	return false;
}
int dinic(int now,int flow)
{
	if(now==t)  return flow;
	int x,delta,ans=0;
	for(int &i=f[now];i;i=next[i])
	{
		x=v[i];
		if(w[i]&&d[x]==d[now]+1)
		{
			delta=dinic(x,min(flow,w[i]));
			w[i]-=delta,w[i^1]+=delta;
			flow-=delta,ans+=delta;
			if(!flow)  return ans;
		}
	}
	return ans;
}
int main()
{
	int x,i,j,num,ans=0;
	scanf("%d%d",&m,&n);
	s=0,t=m+n+1;
	for(i=1;i<=m;++i)
	{
		last[i]=i;
		scanf("%d",&x);
		add(s,i,x),add(i,s,0);
	}
	for(i=1;i<=n;++i)
	{
		scanf("%d",&num);
		for(j=1;j<=num;++j)
		{
			scanf("%d",&x);
			add(last[x],i+m,inf);
			add(i+m,last[x],0);
			last[x]=i+m;
		}
		scanf("%d",&num);
		add(i+m,t,num),add(t,i+m,0);
	}
	while(bfs())
	  ans+=dinic(s,inf);
	printf("%d",ans);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值