PIGS
题目链接:http://poj.org/problem?id=1149
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
题目大意:有n个猪圈(猪圈是被锁起来的),m个顾客,接下来一行给出每个猪圈的猪的数量, 接下来的m行,给出 ki 表示第i个顾客有ki 把钥匙,接下来k个数为钥匙对应的猪圈,最后给出顾客所需要的猪的数量。求最多能卖出多少只猪
每个顾客来买猪时会把所有有钥匙的猪圈都打开,被打开的猪圈里的猪可以乱串,顾客走时再把所有猪圈关上
思路:求最大流,先建图。
流的是猪,猪是从猪圈里流向顾客,所以猪圈是起点,顾客是终点,但是有多个猪圈和顾客,所以可以建一个超级源点,连接所有的猪圈,容量为每个猪圈猪的数量,再建一个超级汇点,所有顾客与超级汇点相连,容量为顾客所需要的猪的数量。
接下来就要考虑从猪圈到顾客要怎么建了,因为每一个客户打开猪圈后,猪可以乱跑,所以下一位顾客只要能打开上个顾客打开的猪圈中的任意一个猪圈,就可以买到上个顾客打开的猪圈中的所有的猪 。
对于任意一个猪圈,向第一个打开它的顾客连一条inf的边,对于每一个顾客向下一个紧接着打开他开过的任意猪圈的顾客连一条inf的边
建好图后直接用dinic模板求最大流
代码:
#include<stdio.h>
#include<string.h>
#include<queue>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int M=2000005;
const int N=10005;
int first[N],vis[N],dis[N];
int tot;
struct node
{
int v,w,next;
node(){}
node(int _v,int _w,int _next){v=_v,w=_w,next=_next;};
}e[M];
void adde(int u,int v,int w) //建边
{
e[tot]=node(v,w,first[u]);
first[u]=tot++;
e[tot]=node(u,0,first[v]);
first[v]=tot++;
}
int bfs(int s,int t) //分层
{
memset(vis,0,sizeof(vis));
memset(dis,0,sizeof(dis));
queue<int>q;
q.push(s);
vis[s]=1;
while(!q.empty())
{
int u=q.front();
q.pop();
for(int i=first[u];~i;i=e[i].next)
if(!vis[e[i].v]&&e[i].w>0)
{
vis[e[i].v]=1;
dis[e[i].v]=dis[u]+1;
q.push(e[i].v);
}
}
return dis[t];
}
int dfs(int s,int t,int flow) //增广
{
if(s==t) return flow;
for(int i=first[s];~i;i=e[i].next)
if(dis[e[i].v]==dis[s]+1&&e[i].w>0)
{
int dd=dfs(e[i].v,t,min(e[i].w,flow));
if(dd)
{
e[i].w-=dd;
e[i^1].w+=dd;
return dd;
}
}
return 0;
}
int dinic(int s,int t)
{
int ans=0,flow;
while(bfs(s,t))
while(flow=dfs(s,t,inf))
ans+=flow;
return ans;
}
int main()
{
int n,m,x;
scanf("%d%d",&n,&m);
memset(first,-1,sizeof(first));
tot=0;
int s=0,t=n+m+1; //源点和终点
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
adde(s,i,x); //猪圈到超级源点
}
int k,c;
for(int i=1;i<=m;i++)
{
scanf("%d",&k);
for(int j=0;j<k;j++)
{
scanf("%d",&x);
if(vis[x]) adde(vis[x],i+n,inf);
else adde(x,i+n,inf);
vis[x]=i+n;
}
scanf("%d",&c);
adde(i+n,t,c); //顾客到汇点
}
printf("%d\n",dinic(s,t));
return 0;
}