PIGS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 23037 | Accepted: 10522 |
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
解析:
最大流。
建图是真的强啊。
首先发现每个猪圈有限制,买的猪有限制,求最大解,于是联想网络流,容量相当于就是限制,于是可以这样建图:
1.源点S向每个猪圈连一条容量为猪圈中猪的个数的边,保证不超过猪的数量。
2.对于每次买猪的操作,新建一个节点,将相应的猪圈连向这个新的节点容量为inf,再将这个节点向人连一条容量为B的边,保证买的数量不超过B。
3.最大流累加答案。
考虑到一次交易完成后相应的猪圈的猪相当于被变成一个猪圈了,于是可以将这些猪圈合并,具体看代码。
代码:
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
using namespace std;
const int inf=1e9;
const int Max=3000;
int n,m,s,t,id,size=1,sum,ans,tot;
int first[Max],fa[Max],dep[Max],tmp[Max];
struct shu{int to,next,len;};
shu edge[Max<<1];
inline int get_int()
{
int x=0,f=1;
char c;
for(c=getchar();(!isdigit(c))&&(c!='-');c=getchar());
if(c=='-') f=-1,c=getchar();
for(;isdigit(c);c=getchar()) x=(x<<3)+(x<<1)+c-'0';
return x*f;
}
inline void build(int x,int y,int z)
{
edge[++size].next=first[x],first[x]=size,edge[size].to=y,edge[size].len=z;
edge[++size].next=first[y],first[y]=size,edge[size].to=x,edge[size].len=0;
}
inline bool bfs()
{
memset(dep,0,sizeof(dep));
queue<int>q;q.push(s),dep[s]=1;
while(q.size())
{
int p=q.front();q.pop();
for(int u=first[p];u;u=edge[u].next)
{
int to=edge[u].to;
if(!dep[to]&&edge[u].len)
{
dep[to]=dep[p]+1,q.push(to);
if(to==t) return 1;
}
}
}
return 0;
}
inline int dinic(int p,int flow)
{
if(p==t) return flow;
int sum=0;
for(int &u=tmp[p];u&&sum<flow;u=edge[u].next)
{
int to=edge[u].to;
if(edge[u].len&&dep[to]==dep[p]+1)
{
int minn=dinic(to,min(flow-sum,edge[u].len));
if(!(flow-sum)) {dep[to]=0;break;}
edge[u].len-=minn,edge[u^1].len+=minn,sum+=minn;
}
}
return sum;
}
inline void solve()
{
while(bfs())
{
memcpy(tmp,first,sizeof(first));
ans+=dinic(s,inf);
}
}
int main()
{
m=get_int(),n=get_int(),tot=m;
for(int i=1;i<=m;i++)
{
int x=get_int();
build(s,i,x),fa[i]=i;
}
for(int i=1;i<=n;i++)
{
id=++tot,sum=get_int();
while(sum--)
{
int x=get_int();
build(fa[x],id,inf),fa[x]=id;
}
sum=get_int(),t=++tot,build(t-1,t,sum);
solve();
}
printf("%d",ans);
return 0;
}