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个猪圈,每个猪圈都上了锁。由于迈克没有钥匙,所以他不能打开任何个猪圈。 要买猪的顾客个接个来到养猪场,每个顾客有些猪圈的销匙而且他们要买定数量的猪。某天,所有要到养猪场买猪的顾客,他们的信息是要提前让迈克知道的。这些信息包括:顾客所拥有的钥匙(详细到有几个猪圈的钥匙、有哪几个猪圈的钥匙、要购买的数量。这样对迈克很有好处,他可以安排销售计划以便卖出的猪的数目最大。 更详细的销售过程:当每个顾客到来时,他将那些他拥有钥匙的猪圈全部打开:迈克从这些猪属中挑出一些猪卖给他们: 如果迈克愿意,迈克可以重新分配这些被打开的猪圈中的猪:额客离开时,猪圈再次被锁上。注意:猪圈可容纳的猪的数量没有限制。
木题的关键在于如何构造个容量网络。在本题中, 方法如下:
1) 将顾客看作除源点和汇点以外的结点,另设2个节点:源点和汇点;
2)源点和每个猪圈的第一个顾客连边,边的权是开始时猪圈中猪的数目;
3)若源点和某个结点之间有重边,则将权合并(因此源点流出的流量就是所有的猪圈能提供猪的数目)
4)顾客j紧跟在喷客 i之后打开某个猪圈,则边< i , j >的权是正无穷。这是因为,如果顾客 j 紧跟在顾客 i 之后打开某个猪圈,那 么迈克就有可能根据顾客 j 的需求将其他猪圈中的猪调整到该猪圈,这样顾客j就能买到尽可能多的猪:
5)每个顾客和汇点之间连边,边的权是顾客所希望购买的猪的数日(因此汇点的流入最就是每个顾客所购买的猪的数目)。
Ford-Fulkerson算法
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int s,t; //源点 汇点
int customer[110][1010]; //节点之间的容量 Cij
int flow[110][1010]; //节点之间的流量 Fij
void init() //初始化函数,构造网络流
{
int M,N; //M 是猪圈的数目 , N是顾客的数目
int num; //每个顾客拥有钥匙的数目
int k; //第k个猪圈的钥匙
int house[1010]; //储存每个猪圈中猪的数目
int last[1010]; //储存每个猪圈前一个顾客的序号
memset(last,0,sizeof(last));
// memset(house,0,sizeof(house));
memset(customer,0,sizeof(customer));
scanf("%d%d",&M,&N);
s=0,t=N+1; //源点 汇点
for(int i=1;i<=M;i++)
scanf("%d",&house[i]);
for(int i=1;i<=N;i++) //构造网络流
{
scanf("%d",&num); //读入每个顾客钥匙的数目
//printf("%d\n",num);
for(int j=0;j0
if(prev[i]==-2&&(p=customer[v][i]-flow[v][i]))
{
prev[i]=v;
queue[qe]=i;
qe++;
minflow[i]=(minflow[v]
dinic算法
#include
#include
#include
#include
using namespace std;
const int INF=0x3f3f3f3f;
int S,T,M,N;
int level[210],house[1100],last[1010];
struct Dinic
{
int c,f;
} edge[210][210];
int dinic_bfs() //构建层次网络
{
queueq;
memset(level,0,sizeof(level)); //初始化顶点的层次为0
q.push(0);
level[0]=1;
int u,v;
while(!q.empty())
{
u=q.front();
q.pop();
for(v=0; v<=T; v++)
{
if(!level[v]&&edge[u][v].c>edge[u][v].f)//即顶点未被访问,顶点u ,v存在边
{
level[v]=level[u]+1; //给顶点标记层次
q.push(v);
}
}
}
return level[T] != 0; //若返回0表明 汇点不在层次网络中
}
int dinic_dfs(int u,int cp) //进行增广
{
int tmp=cp;
int v,t;
if(u==T)
return cp;
for(v=0; v<=T&&tmp; v++)
{
if(level[u]+1==level[v])
{
if(edge[u][v].c>edge[u][v].f)
{
t=dinic_dfs(v,min(tmp,edge[u][v].c-edge[u][v].f));//向下增广
edge[u][v].f+=t;
edge[v][u].f-=t;
tmp-=t;
}
}
}
return cp-tmp;
}
int dinic() //输出最大流
{
int sum=0,tf=0;
while(dinic_bfs())
{
while(tf=dinic_dfs(0,INF))
sum+=tf;
}
return sum;
}
int main()
{
scanf("%d%d",&M,&N);
memset(edge,0,sizeof(edge));
for(int i=1; i<=M; i++)
scanf("%d",&house[i]);
memset(last,0,sizeof(last));
S=0,T=N+1;
for(int i=1; i<=N; i++) //初始化 构造网络流
{
int k,num;
scanf("%d",&num);
for(int j=1; j<=num; j++)
{
scanf("%d",&k);
if(last[k]==0)
edge[S][i].c+=house[k];
else
edge[last[k]][i].c=INF;
last[k]=i;
}
scanf("%d",&edge[i][T]);
}
int ans=dinic();
printf("%d\n",ans);
}