Poj1149 (maximum Stream]

Source: Internet
Author: User

Pigs
Time limit:1000 ms   Memory limit:10000 K
Total submissions:16555   Accepted:7416

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. MERs 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 pig into ss 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 operate 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 MERs. 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 pig. The number of pig in each pig-house is greater or equal to 0 and less or equal to 1000.
The next n lines contains records about the MERs 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 shoshould contain the number of sold pig.

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
Mirko held some pig and locked them in some pig circles.
He has no key (Khan)
Only customers who want to buy pig have the key
Each customer will use his key to open some pigsty buying in sequence.
Take some pig and lock it.
Mirror has the opportunity to re-allocate the opened pigsty before locking it.
Pig
Butler now gives the number of pigs in each pigsty at the beginning, all the keys for each customer.
And the number of pig to be bought ask Mirko how many pig can sell at most: Add a source point to the side of the person who buys the first pig, the weight is the number of pigs in the pigsty. For people who want to buy the pigsty, add the first person who buys the pigsty to the side of the pigsty. The weight is INF, then add each person to the vertex with an edge. The weight is the number of Pig heads that the person wants to buy. Now, the diagram is complete.


# Include <stdio. h> # include <string. h> # define INF 0x3fffffff # define maxn 110 # define maxm 1002int pig [maxm], M, N, sink; int G [maxn] [maxn], queue [maxn]; bool vis [maxn]; int layer [maxn]; bool countlayer () {memset (layer, 0, sizeof (layer); int id = 0, front = 0, now, i; layer [0] = 1; queue [ID ++] = 0; while (front <ID) {now = queue [Front ++]; for (I = 0; I <= sink; ++ I) if (G [now] [I] &! Layer [I]) {layer [I] = layer [now] + 1; if (I = sink) return true; else queue [ID ++] = I ;}} return false;} int dinic () {int mincut, POs, maxflow = 0; int I, id = 0, U, V, now; while (countlayer ()) {memset (VIS, 0, sizeof (VIS); vis [0] = 1; queue [ID ++] = 0; while (ID) {now = queue [ID-1]; If (now = sink) {mincut = inf; for (I = 1; I <ID; ++ I) {u = queue [I-1]; V = queue [I]; If (G [u] [v] <mincut ){ Mincut = G [u] [v]; Pos = u ;}} maxflow ++ = mincut; for (I = 1; I <ID; ++ I) {u = queue [I-1]; V = queue [I]; G [u] [v]-= mincut; G [v] [u] + = mincut ;} while (queue [ID-1]! = POS) vis [queue [-- id] = 0;} else {for (I = 0; I <= sink; ++ I) {If (G [now] [I] & layer [now] + 1 = layer [I] &! Vis [I]) {vis [I] = 1; queue [ID ++] = I; break ;}} if (I> sink) -- id ;}}} return maxflow;} int main () {// freopen ("stdin.txt", "r", stdin); int I, keys, num; while (scanf ("% d", & M, & n) = 2) {sink = n + 1; for (I = 1; I <= m; ++ I) scanf ("% d", & Pig [I]); memset (G, 0, sizeof (g); for (I = 1; I <= N; ++ I) {scanf ("% d", & keys); While (keys --) {scanf ("% d", & num ); if (pig [num]> = 0) {G [0] [I] + = pig [num]; // 0 is source pig [num] =-I; // here is the first person to mark the num pigsty connection} else G [-pig [num] [I] = inf;} scanf ("% d ", & G [I] [sink]);} printf ("% d \ n", dinic ();} return 0 ;}


Poj1149 (maximum Stream]

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.