POJ 1149 PIGS

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20582 Accepted: 9389

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 Test instructions:

There are M pigsty, n individuals, each person can open some pigsty, from the pig in the pigsty, the pig in the current open pigsty can walk freely to open the pigsty, each guest after the pig sty will be closed again, each guest has the desired quantity, ask the maximum number of piglets ^ (* ̄ (oo)  ̄) ^

Analysis:

"This water inscribed what wrote"---from YSQ's taunt tat ...

There's really nothing to write about ...

However, it is important to think that it is possible to use the +∞ side to express the flow of traffic ...

For each pigsty we connect a volume from S to each pigsty for the side of the pig quantity, from each customer to the t even a volume for the side of the desired adoption quantity, for each guest, if this guest is the first time to open the guest of this pigsty, then the pigsty to this guest even +∞ side, Otherwise, the last guest who opened the pigsty attached a +∞ to the current guest ...

Code:
1#include <algorithm>2#include <iostream>3#include <cstring>4#include <cstdio>5 //by Neighthorn6 #defineINF 0x3f3f3f3f7 using namespacestd;8 //Dapeng Day with the wind, soaring 90,000 miles9 Ten Const intmaxn= -+5, maxm=800000+5; One  A intN,M,S,T,CNT,HD[MAXN],TO[MAXM],FL[MAXM],NXT[MAXM],PRE[MAXN],POS[MAXN],NUM[MAXN]; -  -InlinevoidAddintSintXinty) { thefl[cnt]=s;to[cnt]=y;nxt[cnt]=hd[x];hd[x]=cnt++; -fl[cnt]=0; to[cnt]=x;nxt[cnt]=hd[y];hd[y]=cnt++; - } -  +InlineBOOLBFsvoid){ -memset (pos,-1,sizeof(POS)); +     intHead=0, tail=0, Q[MAXN]; Aq[0]=s;pos[s]=0; at      while(head<=tail) { -         inttop=q[head++]; -          for(inti=hd[top];i!=-1; i=Nxt[i]) -             if(pos[to[i]]==-1&&Fl[i]) -pos[to[i]]=pos[top]+1, q[++tail]=To[i]; -     } in     returnpos[t]!=-1; - } to  +InlineintFindintVintf) { -     if(v==T) the         returnF; *     intres=0, T; $      for(inti=hd[v];i!=-1&&f>res;i=Nxt[i])Panax Notoginseng         if(pos[to[i]]==pos[v]+1&&Fl[i]) -T=find (To[i],min (fl[i],f-res)), res+=t,fl[i]-=t,fl[i^1]+=T; the     if(!Res) +pos[v]=-1; A     returnRes; the } +  -InlineintDinic (void){ $     intres=0, T; $      while(BFS ()) -          while(t=find (S,inf)) -res+=T; the     returnRes; - }Wuyi  theSigned Main (void){ -scanf"%d%d", &m,&n); cnt=0; Wus=0, t=n+m+1; Memset (hd,-1,sizeof(HD)); -      for(intI=1; i<=m;i++) Aboutscanf"%d", &num[i]), pre[i]=I,add (num[i],s,i); $      for(intI=1; i<=n;i++){ -         intX;SCANF ("%d",&x); -          for(intj=1, y;j<=x;j++) -scanf"%d", &y), add (inf,pre[y],i+m), pre[y]=i+m; Ascanf"%d", &x); Add (x,i+m,t); +     } theprintf"%d\n", Dinic ()); -     return 0; $}//The cap ou pas Cap. Cap;
View Code

by Neighthorn

POJ 1149 PIGS

Related Keywords:

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.