POJ1149 PIGS [maximum flow map]

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20662 Accepted: 9435

Description

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 data concerning customers planning to visit the farm on that particular day is available to Mirko early in the mornin G So, he can make a sales-plan in order to maximize the number of pigs sold.
More precisely, the procedure was as following:the customer arrives, opens all pig-houses to which he had the key, Mirko s Ells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remain ing 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 of input contains integers 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 (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 33 1 102 1 2 22 1 3 31 2 6

Sample Output

7

Source

Croatia OI 2002 Final Exam-first Day Chinese noodle 1280:emmy sell pig pigs time limit:1 Sec Memory limit:162 MB
submit:183 solved:123
[Submit] [Status] [Discuss] Descriptionemmy work in a pig farm. The pig farm has M-locked pigsty, but Emmy has no keys. Customers will go to pig farms to buy pigs, one after the other. Each customer will have the keys to the pigsty, who will open the pigsty and buy a fixed number of pigs. All the customers have the keys and the number of pigs they need to buy in advance told the Emmy, so Emmy to book a plan, so that the most sold pigs. The process of buying and selling is like this: a customer comes in and opens all the pigsty he can open. The Emmy then pulls a fixed number of pigs from these pigsty to the customer (at most only as many as the customer needs) and can rearrange the pigs in the open pigsty. Each pigsty can store any number of pigs. Write a program that allows Emmy to sell as many pigs as possible. The first line of input has two integers: M and n, which indicate the number of pigsty and the number of customers. The second line has a m integer that indicates how many pigs were in each pigsty at the beginning. The next n lines describe each customer in the order in which they came, and each line is formatted as follows: A K1 K2 ... KA B represents the number of keys that the customer has, K1 ... Ka represents the pigsty for each key, and B indicates the number of pigs that the customer needs to buy. Output contains only an integer, which is the number of pigs that can be sold at most. Plain View of the words, because a person's purchase affects the next person, so you can purchase a tiered according to each pigsty and man as point, s even the beginning number of pigsty, the number of people even t buyEveryone (Buy) as a level, from the previous level to the next level of the same pigsty, and then can be purchased (can be merged together) connected to each other,so dot n+nm, Edge 2nm consider some side useless, there is no need for everyone's purchase to re-get a batch of Pigsty point, save each pigsty current to the man and then even the INF line, because the next person can buy this pigsty, the people who used to take the pigsty open all the pigsty canso point N, Edge nm
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespacestd;Const intn= the, m=1005, inf=1e9;inlineintRead () {CharC=getchar ();intx=0, f=1;  while(c<'0'|| C>'9'){if(c=='-') f=-1; C=GetChar ();}  while(c>='0'&&c<='9') {x=x*Ten+c-'0'; C=GetChar ();} returnx*F;}intm,n,s,t;intPig[m],now[m];structedge{intV,c,f,ne;} E[n*m<<1];intCnt,h[n];inlinevoidInsintUintVintc) {CNT++; E[CNT].V=v;e[cnt].c=c;e[cnt].f=0; e[cnt].ne=h[u];h[u]=CNT; CNT++; E[CNT].V=u;e[cnt].c=0; e[cnt].f=0; e[cnt].ne=h[v];h[v]=CNT;}intQ[n],head,tail,vis[n],d[n];BOOLBFs () {memset (Vis,0,sizeof(VIS)); memset (d,0,sizeof(d)); Head=tail=1; D[s]=0; vis[s]=1; Q[tail++]=s;  while(head!=tail) {        intu=q[head++];  for(intI=h[u];i;i=e[i].ne) {            intv=e[i].v; if(!vis[v]&&e[i].c>e[i].f) {Vis[v]=1; D[V]=d[u]+1; Q[tail++]=v; if(v==t)return true; }        }    }    return false;}intCur[n];intDfsintUinta) {    if(u==t| | a==0)returnA; intflow=0, F;  for(int&i=cur[u];i;i=e[i].ne) {        intv=e[i].v; if(d[v]==d[u]+1&& (F=dfs (V,min (A,E[I].C-E[I].F)) >0) {Flow+=F; E[I].F+=F; e[((i-1)^1)+1].f-=F; A-=F; if(a==0) Break; }    }    returnflow;}intDinic () {intflow=0;  while(BFS ()) { for(inti=s;i<=t;i++) cur[i]=H[i]; Flow+=DFS (S,inf); }    returnflow;}intMain () {//freopen ("In.txt", "R", stdin);M=read (); N=read (); s=0; t=n+1;  for(intI=1; i<=m;i++) pig[i]=read ();  for(intI=1; i<=n;i++){        intA=read (), b,x;  while(a--) {x=read (); if(!now[x]) ins (s,i,pig[x]), now[x]=i; ElseIns (now[x],i,inf), now[x]=i; } B=read ();    Ins (i,t,b); } printf ("%d", Dinic ());}

POJ1149 PIGS [maximum flow map]

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.