Network stream (Max Stream): POJ 1149 PIGS

Source: Internet
Author: User

Pigstime limit:1000msmemory limit:10000kbthis problem'll be judged onPKU.
64-bit integer(integer) IO format: %lld Java class name: Main Mirko works on a pig farm the consists of M locked pig-houses and Mirko can ' t unlock any pighouse because he doesn ' t has The keys. Customers come to the farm one after another. Each of them have keys to some pig-houses and wants to buy a certain number of pigs.
All dataconcerning(about)Customers planning to visit the farm on this particular day is available to Mirko early in the morning so that he can mak e a sales-plan in order toMaximize(Take ... Maximum value)The number of pigs sold.
Moreprecisely(precisely), theprocedure(program)is as following:the customer arives, opens all pig-houses to which he have the key, Mirko sells a certain number of pigs F Rom all the unlocked pig-houses to him, and, if Mirko wants, he canRedistribute(Redistribution)The remaining pigs across the unlocked pig-houses.
An unlimited number of pigs can is placed in every pig-house.
Write a program, that would find the maximum number of pigs that he can sell on this day. Input the first line ofinput(Input)Containsintegers(integer)M and N, 1 <= m <=, 1 <= N <=, number of pighouses and number of customers. Pig houses is numbered from 1 to M and customers is numbered from 1 to N.
The next line contains M Integeres, for each pig-house initial number of pigs. The number of pigs in pig-house are 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 on the i-th customer is written in The (i+2)-th line):
A K1 K2 ... Ka B It means that this customer have key to the pig-houses marked with the numbers K1, K2, ..., Ka (sortednondecreasingly(not reduced)) and that he wants to buy B pigs. Numbers A and B can be equal to 0. Output the first and only line of theOutput(output)Should contain the number of sold pigs. Sample Input
3 33 1 102 1 2 22 1 3 31 2 6
Sample Output
7
  
Modeling problem, you need to pay attention to the optimization of space.
1#include <iostream>2#include <cstring>3#include <cstdio>4#include <queue>5 6 using namespacestd;7 Const intinf=2147483647;8 Const intmaxn=1010, maxm=4010;9 intCNT,FIR[MAXN],NXT[MAXM],CAP[MAXM],TO[MAXM],DIS[MAXN],GAP[MAXN],PATH[MAXN],USED[MAXN];Ten  One voidAddedge (intAintBintc) A { -nxt[++cnt]=Fir[a]; -to[cnt]=b; thecap[cnt]=C; -fir[a]=CNT; - } -  + BOOLBFS (intSintT) - { +memset (DIS,0,sizeof(DIS)); Adis[t]=1; atqueue<int>Q;q.push (T); -      while(!q.empty ()) -     { -         intNode=Q.front (); Q.pop (); -          for(intI=fir[node];i;i=Nxt[i]) -         { in             if(Dis[to[i]])Continue; -dis[to[i]]=dis[node]+1; to Q.push (To[i]); +         } -     } the     returnDis[s]; * } $ intFRON[MAXN];Panax Notoginseng intISAP (intSintT) - { the     if(!BFS (s,t)) +         return 0; A      for(intI=1; i<=t;i++) + +Gap[dis[i]]; the     intp=s,ret=0; +memcpy (Fron,fir,sizeof(FIR)); -      while(dis[s]<=T) $     { $         if(p==T) { -             intf=INF; -              while(p!=R) { thef=min (f,cap[path[p]]); -p=to[path[p]^1];Wuyi             } thep=t;ret+=F; -              while(p!=R) { Wucap[path[p]]-=F; -cap[path[p]^1]+=F; Aboutp=to[path[p]^1]; $             } -         } -         int&ii=Fron[p]; -          for(; ii;ii=Nxt[ii]) { A             if(!cap[ii]| | dis[to[ii]]+1!=Dis[p]) +                 Continue; the             Else  -                  Break; $         }         the         if(ii) { thep=To[ii]; thepath[p]=II; the         } -         Else{ in             if(--gap[dis[p]]==0) Break; the             intminn=t+1; the              for(intI=fir[p];i;i=Nxt[i]) About                 if(Cap[i]) theminn=min (Minn,dis[to[i]]); thegap[dis[p]=minn+1]++; thefron[p]=Fir[p]; +             if(p!=S) -p=to[path[p]^1];  the         }Bayi     } the     returnret; the } -  - voidInit () the { thememset (Fir,0,sizeof(FIR)); thememset (Used,0,sizeof(used));  theCnt=1; - } the intMain () the { the     intN,m,num,k,need;94      while(~SCANF ("%d%d",&m,&N)) the     { the Init (); the          for(intI=1; i<=m;i++){98scanf"%d",&num); AboutAddedge (0, i,num); -Addedge (I,0,0);101         }102          for(inti=m+1; i<=m+n;i++){103scanf"%d",&k);104              while(k--){ thescanf"%d",&num);106                 if(Used[num]) {107 Addedge (used[num],i,inf);108Addedge (I,used[num],0);109                 } the                 Else{111used[num]=i; the Addedge (num,i,inf);113Addedge (I,num,0); the                 } the                  the             }117scanf"%d",&need);118Addedge (i,n+m+1, need);119Addedge (n+m+1I0); -         }121printf"%d\n", ISAP (0, n+m+1));122     }123     return 0;124}

Network stream (Max Stream): POJ 1149 PIGS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.