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
题意:有M个猪圈和N个人,对于每个人,可以从某些猪圈里面挑一共多少头猪,然后剩下的猪可以在当前猪圈之间随意转换,求最多能卖多少猪。
其实还是网络流的最大流问题,但是因为不怎么熟,建边的错误改了很久。。
我们可以虚构一个起点和一个终点,然后在起点与人之间建边,人与猪圈之间建边,猪圈与终点之间建边(或者反过来),最后求起点到终点的最大流。需要注意的是,对于不同的方向,边的容量其实不一样的,而且边的方向也需要注意。
代码:
#include<queue>
#include<vector>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#define maxn 1205
#define INF 0x7f7f7f7f
using namespace std;
typedef long long LL;
struct Edge
{
LL from,to,cap,flow;
};
LL n,m,s,t,pre[maxn],ss[maxn],kk[maxn];
vector<Edge> edges;
vector<LL> G[maxn];
bool vis[maxn];
LL d[maxn];
LL cur[maxn];
bool BFS()
{
memset(vis,0,sizeof(vis));
queue<LL> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty())
{
LL x=Q.front();
Q.pop();
for(LL i=0;i<G[x].size();i++)
{
Edge&e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
void AddEdge(LL from,LL to,LL cap)
{
edges.push_back((Edge){from,to,cap,0});
edges.push_back((Edge){to,from,0,0});
int mm=edges.size();
G[from].push_back(mm-2);
G[to].push_back(mm-1);
}
LL DFS(LL x,LL a)
{
if(x==t||a==0)
return a;
LL flow=0,f;
for(LL &i=cur[x];i<G[x].size();i++)
{
Edge&e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0)
break;
}
}
return flow;
}
LL Maxflow()
{
LL flow=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
int main()
{
while(~scanf("%lld%lld",&n,&m))
{
s=0;
t=m+n+1;
edges.clear();
for(int i=0;i<=m+n+1;i++)
G[i].clear();
memset(pre,0,sizeof(pre));
for(int i=1;i<=n;i++)
{
scanf("%lld",&kk[i]);
AddEdge(s,i,kk[i]);
}
for(int i=1;i<=m;i++)
{
LL k;
scanf("%lld",&k);
for(int j=0;j<k;j++)
scanf("%lld",&ss[j]);
LL val;
scanf("%lld",&val);
for(int j=0;j<k;j++)
{
if(!pre[ss[j]])
{
pre[ss[j]]=i+n;
AddEdge(ss[j],i+n,kk[ss[j]]);
}
else
AddEdge(pre[ss[j]],i+n,INF);
}
AddEdge(i+n,t,val);
}
printf("%lld\n",Maxflow());
}
return 0;
}