POJ 1149 PIGS (最大流Dinic)
2013-01-10 12:17
477 查看
PIGS
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.
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
Sample Output
Source
Croatia OI 2002 Final Exam - First day
题意:给你M个猪圈,N个客户,客户身上有对应的猪圈的钥匙,陆续来买猪,开完一个猪圈后,开着的猪圈之间猪的数量可以随意分配,问最多能卖出多少头猪。
思路:主要是构图问题。可以那么想,第一个客户打开的所有猪圈,对于之后的所有拥有其中一个猪圈的钥匙的客户,都可以交易第一个客户所有的猪圈。那么对于某个猪圈,当它第一次被打开时,可以从源点到当前客户连接一条权值为这个猪圈猪数量的边。当它不是第一次打开的时候,则从上一次打开它的客户到当前打开它的客户之间连接一条权值为无穷大的边。最后,对于每个客户到汇点连接一条当前客户所需要的猪数量的边。
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 13026 | Accepted: 5758 |
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
Source
Croatia OI 2002 Final Exam - First day
题意:给你M个猪圈,N个客户,客户身上有对应的猪圈的钥匙,陆续来买猪,开完一个猪圈后,开着的猪圈之间猪的数量可以随意分配,问最多能卖出多少头猪。
思路:主要是构图问题。可以那么想,第一个客户打开的所有猪圈,对于之后的所有拥有其中一个猪圈的钥匙的客户,都可以交易第一个客户所有的猪圈。那么对于某个猪圈,当它第一次被打开时,可以从源点到当前客户连接一条权值为这个猪圈猪数量的边。当它不是第一次打开的时候,则从上一次打开它的客户到当前打开它的客户之间连接一条权值为无穷大的边。最后,对于每个客户到汇点连接一条当前客户所需要的猪数量的边。
#include<cstdio> using namespace std; const int mm=1000000; const int mn=22222; const int oo=1000000000; int node,s,t,edge; int to[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; int pigs[mn],sign[mn]; int key_num,buy,buy_num; inline int min(int a,int b) { return a<b?a:b; } inline void init(int nn,int ss,int tt) { node=nn,s=ss,t=tt,edge=0; for(int i=0; i<node; ++i) head[i]=-1; } inline void add(int u,int v,int c) { to[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; to[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool bfs() { int i,u,v,l,r=0; for(i=0; i<node; ++i) dis[i]=-1; dis[q[r++]=s]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=to[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==t)return 1; } return 0; } int dfs(int u,int maxf) { if(u==t) return maxf; for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0; } int dinic() { int i,ret=0,delta; while(bfs()) { for(i=0; i<node; ++i) work[i]=head[i]; while(delta=dfs(s,oo))ret+=delta; } return ret; } int main() { int i,n,m; while(scanf("%d%d",&m,&n)!=-1) { for(i=1; i<=m; i++) sign[i]=0; for(i=1; i<=m; i++) scanf("%d",&pigs[i]); init(n+2,0,n+1); for(i=1; i<=n; i++) { scanf("%d",&key_num); while(key_num--) { scanf("%d",&buy); if(!sign[buy]) add(s,i,pigs[buy]); else add(sign[buy],i,oo); sign[buy]=i; } scanf("%d",&buy_num); add(i,t,buy_num); } printf("%d\n",dinic()); } return 0; }
相关文章推荐
- POJ 1149 PIGS (最大流Dinic)
- poj 1149 PIGS (网络流最大流Dinic)
- POJ-1149-PIGS(最大流 标号法)
- POJ 1149 PIGS 最大流 构图!
- POJ1149 PIGS 网络最大流
- POJ 1149 PIGS (最大流)
- poj 1149 PIGS【最大流】
- poj 1149 PIGS(最大流)
- poj 1149 PIGS (最大流)
- poj 1149 PIGS(最大流)
- POJ 1149 PIGS(最大流+建图)
- POJ 1149 PIGS 最大流建图
- POJ 1149 PIGS(最大流 经典构图)
- 最大流 poj 1149 pigs
- POJ1149 PIGS (最大流)
- POJ 1149 PIGS(最大流-Dinic)
- poj1149 PIGS(建图最大流)
- [最大流] poj 1149 pigs
- POJ1149 PIGS [最大流 建图]
- POJ 1149 PIGS (网络最大流 Dinic 建对图你就赢了)