PIGS
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 14204 | Accepted: 6305 |
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.
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.
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
Source
题意:有M个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共最多能卖出多少头猪。(1 <= N <= 100, 1 <= M <= 1000)。
建图:
每个顾客分别用一个结点来表示。
对于每个猪圈的第一个顾客,从源点向他连一条边,容量就是该猪圈里的猪的初始数量。如果从源点到一名顾客有多条边,则可以把它们合并成一条,容量相加。
对于每个猪圈,假设有n个顾客打开过它,则对所有整数i∈[1, n),从该猪圈的第i个顾客向第i + 1个顾客连一条边,容量为∞。
从各个顾客到汇点各有一条边,容量是各个顾客能买的数量上限。
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
#define INF 9999999
int n,m;
int map[300][300],pre[300],flow[300][300],p[300],a[300];
int EK(int s,int t)
{
int sum=0;
queue<int>q;
memset(flow,0,sizeof(flow));
for(;;)
{
memset(a,0,sizeof(a));//记录残量
a[s]=INF;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=1; i<=t; i++)
if(!a[i]&&map[u][i]>flow[u][i])
{
p[i]=u;//记录i的父亲节的是u
q.push(i);
a[i]=a[u]<map[u][i]-flow[u][i]?a[u]:map[u][i]-flow[u][i];
}
}
if(!a[t])break;//如果残量是0的话,就找到最大流
for(int i=t; i!=s; i=p[i])//每条路加上最小残量
{
flow[p[i]][i]+=a[t];
flow[i][p[i]]-=a[t];
}
sum+=a[t];//记录流量
}
return sum;
}
int main()
{
int a,b,c,max;
int pig[1007],link[1007];
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(map,0,sizeof(map));
memset(p,0,sizeof(p));
memset(link,0,sizeof(link));
memset(pig,0,sizeof(pig));
int t=m+1;
for(int i=1;i<=n;i++)
scanf("%d",&pig[i]);
for(int i=1;i<=m;i++)
{
scanf("%d",&a);
for(int j=1;j<=a;j++)
{
scanf("%d",&b);
if(link[b]==0)
{
link[b]=i;
map[0][i]+=pig[b];
}
else
{
map[link[b]][i]=INF;
}
}
scanf("%d",&map[i][t]);
}
max=EK(0,t);
printf("%d\n",max);
}
return 0;
}