码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 1149 PIGS

时间:2015-04-19 21:20:31      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

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

3 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

Source

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cmath>
  4 #include<algorithm>
  5 #include<cstring>
  6 #include<queue>
  7 #define inf 0x3f3f3f3f
  8 using namespace std;
  9 const int maxn=100+10,maxm=1000+10,maxe=200000+10;
 10 int n,m,S,T;
 11 int cus[maxm][maxn],pig[maxm],buy[maxn],num[maxm];
 12 struct Tedge{int x,y,flow,cap,next;};
 13 struct Dinic{
 14     Tedge adj[maxe];int fch[maxn],ms;
 15     int n,m,S,T,d[maxn],cur[maxn];bool vis[maxn];
 16     void init(int n){
 17         this->n=n;
 18         ms=0;
 19         memset(fch,-1,sizeof(fch));
 20         return;
 21     }
 22     void AddEdge(int a,int b,int c){
 23         adj[ms]=(Tedge){a,b,0,c,fch[a]};fch[a]=ms++;
 24         adj[ms]=(Tedge){b,a,0,0,fch[b]};fch[b]=ms++;
 25         return;
 26     }
 27     bool BFS(){
 28         memset(vis,false,sizeof(vis));
 29         queue<int>Q;Q.push(S);vis[S]=true;d[S]=0;
 30         while(!Q.empty()){
 31             int u=Q.front();Q.pop();
 32             for(int i=fch[u];i!=-1;i=adj[i].next){
 33                 int v=adj[i].y;
 34                 if(!vis[v]&&adj[i].cap>adj[i].flow){
 35                     vis[v]=true;
 36                     d[v]=1+d[u];
 37                     Q.push(v);
 38                 }
 39             }
 40         } return vis[T];
 41     }
 42     int DFS(int u,int a){
 43         if(u==T||!a) return a;
 44         int flow=0,f;
 45         for(int& i=cur[u];i!=-1;i=adj[i].next){
 46             int v=adj[i].y;
 47             if(d[v]==d[u]+1&&(f=DFS(v,min(a,adj[i].cap-adj[i].flow)))>0){
 48                 flow+=f;
 49                 a-=f;
 50                 adj[i].flow+=f;
 51                 adj[i^1].flow-=f;
 52                 if(!a) break;
 53             }
 54         } return flow;
 55     }
 56     int MaxFlow(int S,int T){
 57         this->T=T;this->S=S;
 58         int flow=0;
 59         while(BFS()){
 60             for(int i=0;i<n;i++) cur[i]=fch[i];
 61             flow+=DFS(S,inf);
 62         } return flow;
 63     }
 64 }sol;
 65 inline int read(){
 66     int x=0,sig=1;char ch=getchar();
 67     while(!isdigit(ch)){if(ch==-) sig=-1;ch=getchar();}
 68     while(isdigit(ch)) x=10*x+ch-0,ch=getchar();
 69     return x*=sig;
 70 }
 71 inline void write(int x){
 72     if(x==0){putchar(0);return;} if(x<0) putchar(-),x=-x;
 73     int len=0,buf[15]; while(x) buf[len++]=x%10,x/=10;
 74     for(int i=len-1;i>=0;i--) putchar(buf[i]+0);return;
 75 }
 76 void init(){
 77     m=read();n=read();
 78     for(int i=0;i<m;i++) pig[i]=read();
 79     for(int i=0;i<n;i++){
 80         int a=read(),b;
 81         for(int j=0;j<a;j++){
 82             b=read();
 83             b--;
 84             cus[b][num[b]++]=i;
 85         }
 86         buy[i]=read();
 87     }
 88     return;
 89 }
 90 void work(){
 91     
 92     return;
 93 }
 94 void print(){
 95     S=n;T=n+1;
 96     sol.init(n+2);
 97     for(int i=0;i<n;i++) sol.AddEdge(i,T,buy[i]);
 98     for(int i=0;i<m;i++){
 99         if(num[i]>0) sol.AddEdge(S,cus[i][0],pig[i]);
100         for(int j=1;j<num[i];j++) sol.AddEdge(cus[i][j-1],cus[i][j],inf);
101     }
102     write(sol.MaxFlow(S,T));
103     return;
104 }
105 int main(){
106     init();work();print();return 0;
107 }

 

POJ 1149 PIGS

标签:

原文地址:http://www.cnblogs.com/chxer/p/4439772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!