POJ 1149 PIGS

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000KB 64bit IO Format: %i64d &%i64u

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

The idea of building a map is very good

From Aaronpolaris's blog here:

Composition method:

① each customer as a node other than the source and sink points.

② an edge from the source point to the first customer of each pigsty, the volume of the first pig of the pigsty.

③ the front and back two customers of each pigsty have an edge, the capacity is positive infinity. Because the number of pigs in each pigsty can be arbitrarily assigned.

④ an edge from each customer to the meeting point, the capacity is the number of pigs to be purchased.

Transport complete

1 //POJ-1149 PIGS2 /*by Silvern*/3#include <iostream>4#include <algorithm>5#include <cstring>6#include <cstdio>7#include <cmath>8#include <queue>9 using namespacestd;Ten Const intinf=0xfffff; One Const intmxn=420;//Maximum number of customers A Const intmxm= -;//Maximum number of pigsty - ints,t; - intW[MXN][MXN];//capacity the intD[MXN];//Depth - intHOUSE[MXM];//Number of pigs in pigsty - intLAST[MXN]; - intM,n;//number of pigsty, number of customers +  - voidinit () { +     inti,j; A  atscanf"%d%d",&m,&N); -     intnum; -     intx; -s=0; t=n+1; -      for(i=1; i<=m;i++) scanf ("%d",&house[i]); -      for(i=1; i<=n;i++){ inscanf"%d",&num); -          for(j=1; j<=num;j++){ toscanf"%d",&x); +             if(!last[x]) w[s][i]+=House[x]; -             //if it is the first customer to come to this pigsty, from the source point of connection to the customer point, the capacity for the number of pigs in the pigsty the             Elsew[last[x]][i]=INF; *             //between the front and rear customers of the same pigsty $last[x]=i;Panax Notoginseng         } -scanf"%d",&x); theW[i][t]=x;//customers to the meeting point edge, capacity for the purchase amount +     } A     return; the } + BOOLBFS (ints) { -queue<int>Q; $memset (d,-1,sizeof(d)); $ Q.push (s); -d[s]=0; -     inti; the      while(!Q.empty ()) { -         intu=Q.front ();Wuyi         if(u==t)return true; the Q.pop (); -          for(i=0; i<=t;i++){ Wu             if(W[u][i] && d[i]==-1){ -d[i]=d[u]+1; About Q.push (i); $             } -         } -     } -     return false; A } + intDFS (intXintLow ) { the     if(x==t)returnLow ; -     intI,a; $      for(i=0; i<=t;i++){ the         if(w[x][i]>0&& d[i]==d[x]+1) the           if(a=DFS (I,min (W[x][i],low))) { thew[x][i]-=A; thew[i][x]+=A; -               returnA; in           } the     } the     return 0; About } the voidDinic () { the     intans=0; the     intflow=0; +      while(BFS (s)) { -          while(flow=DFS (S,inf)) { theans+=flow;Bayi         } the     } theprintf"%d\n", ans); -     return; - } the intMain () { the init (); the dinic (); the     return 0; -}

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.