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.
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.
The first and only line of the output should contain the number of sold pigs.
3 3 3 1 10 2 1 2 2 2 1 3 3 1 2 6
7
【题目大意】 有M个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。
依次来了N个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。
每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,
然后所有猪圈重新关上。问总共最多能卖出多少头猪。(1 <= N <= 100, 1 <= M <= 1000)
思路:主要是建模; 建模链接:https://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html
代码:
#include<stdio.h> //这道题的核心就是建模;
#include<string.h> // 因为一个人去买猪,若拥有多个猪圈的钥匙,那么这几个猪圈的猪就可以互流;
#include<queue> // 这样建模太难建了,不妨把一个猪圈中的猪,全部流向第一个来买猪的人,
#include<vector> // 以后再有人来这个猪圈买猪的话,就找来这个猪圈买猪的前一个人,让前个人流向这个人,容量为INF;
#include<algorithm> // 形成 人与人之间的流动
using namespace std;
#define INF 0x3f3f3f3f
#define MaxN 120
#define MaxM 1010
struct node
{
int to,cap,rev;
};
int last[MaxM],home[MaxM],book[MaxN],bb[MaxN][MaxN];
// last[t] 记录上一个来过t猪圈的人的编号;
// home 每个猪圈猪的数量;
// book 数组 从源点到人的在vector容器,记录容量的位置,同时起标记作用;
// bb[i][j] 起标记作用,从i到j是不是已经流过;
int level[MaxN];
vector<node >v[MaxN];
int n,m;
void init()
{
int i,j;
for(i=0;i<=n;i++)
v[i].clear();
memset(last,0,sizeof(last));
memset(book,-1,sizeof(book));
memset(bb,0,sizeof(bb));
}
void insert(int s,int i,int cap)
{
v[s].push_back((node){i,cap,v[i].size()});
v[i].push_back((node){s,0,v[s].size() - 1});
}
int bfs(int s,int t)
{
memset(level,-1,sizeof(level));
queue<int > q;
level[s]=0;
q.push(s);
while(!q.empty())
{
int tt=q.front();
q.pop();
for(int i=0;i<v[tt].size();i++)
{
node e=v[tt][i];
if(e.cap>0&&level[e.to]<0)
{
level[e.to]=level[tt]+1;
q.push(e.to);
}
}
}
if(level[t]<0)
return 0;
else return 1;
}
int dfs(int k,int t,int f)
{
if(k==t)
return f;
int s = f;
for(int i=0;i<v[k].size();i++)
{
node &e=v[k][i];
if(e.cap>0&&level[e.to]==level[k]+1)
{
int d = dfs(e.to,t,min(f,e.cap));
if(d>0)
{
e.cap-=d;
v[e.to][e.rev].cap+=d;
f=f-d;
}
}
}
return s-f;
}
int max_flow(int s,int t) // 最大流dinic 算法的模板
{
int flow=0;
while(bfs(s,t))
{
flow+=dfs(s,t,INF);
}
return flow;
}
int main()
{
int i,j,k,t;
while(~scanf("%d%d",&m,&n))
{
for(i=1;i<=m;i++)
scanf("%d",&home[i]);
init();
int s=0;
for(i=1;i<=n;i++)
{
scanf("%d",&k);
for(j=0;j<k;j++)
{
scanf("%d",&t);
if(last[t]==0)
{
if(book[i]==-1)
{
insert(s,i,home[t]);
book[i]= v[s].size() - 1;
}
else
{
int tt=book[i];
v[s][tt].cap+=home[t];
}
last[t] = i;
}
else
{
int tt=last[t];
if(!bb[tt][i])
{
insert(tt,i,INF);
bb[tt][i]=1;
}
last[t] = i;
}
}
int kk;
scanf("%d",&kk);
insert(i,n+1,kk);
}
printf("%d",max_flow(0,n+1));
}
return 0;
}