POJ 1149 PIGS (Network maximum flow dinic build the map you win)

Source: Internet
Author: User


PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 17598 Accepted: 7977

Description

mirko works on a pig farm the consists of M locked pig-houses and Mirko can ' t unlock any pig House 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 MO Rning 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, Mir Ko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the RE Maining pigs across the unlocked pig-houses.
an unlimited number of pigs can is placed in every pig-house.
Write A program, that'll find the maximum number of pigs that he can sell on the.

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

Topic Link: http://poj.org/problem?id=1149

The main topic: M pigsty, n customers, each customer has a key, K1,k2...ka, each customer wants to buy B pig, when each customer arrives, he will those who have the keys of the pigsty all open, Mike from these pigsty to pick out some pigs to sell them, if Mike is willing to, Mike can redistribute the pigs in the open pigsty, and when the customer leaves, the pigsty will be locked again. Note: There is no limit to the number of pigs that can be accommodated in a pigsty

Topic Analysis: The maximum flow of the problem, is to open the brain hole Map:
1) Set a single point of origin of a meeting point
2) The source point and the first customer of each pigsty are connected, the edge right is the number of pigs in the pigsty at the beginning
3) If there is a heavy edge between the source point and a point, then the right merge
4) customer J immediately after the customer I open a pigsty, the edge <i,j> rights for Infinity
5) Each customer and meeting point between the edge, the right edge for customers want to buy the number of pigs
build up the diagram, close your eyes dinic,0ms

#include <cstdio> #include <cstring> #include <algorithm> #include <queue>using namespace std; int const INF = 0x3fffffff;int Const MAXN = 105;int Const MAXM = 1005;int CAP[MAXN][MAXN], D[maxn];int PNUM[MAXM], Last[ma Xm];int m, n;int src, Sink;bool BFS () {memset (d,-1, sizeof (d)), queue <int> q;d[src] = 0;q.push (SRC), while (!q.empty ( ) {int cur = Q.front (); Q.pop (); for (int i = 0; I <= sink; i++) {if (Cap[cur][i] && d[i] = = 1) {D[i] = D[cur] + 1;q . push (i);}}} return D[sink]! =-1;}  int DFS (int t, int flow) {if (t = = Sink | | flow = = 0) return flow;int tmp = flow;for (int i = 0; I <= sink; i++) {if (d[i] = = D[t] + 1 && cap[t][i]) {int mi = DFS (i, min (flow, cap[t][i])), cap[t][i]-= mi;cap[i][t] + = mi;flow-= Mi;}} return tmp-flow;} void Dinic () {int ans = 0;while (BFS ()) ans + = DFS (src, INF);p rintf ("%d\n", ans);} int main () {memset (pnum, 0, sizeof (pnum)), memset (last, 0, sizeof), scanf ("%d%d", &m, &n), src = 0;sink = n + 1 ; for (int i = 1; I <= m; i++) scanf ("%d", &pnum[i]); for (int i = 1; I <= n; i++) {int num;scanf ("%d", &num), for (int j = 0; j < Num; J + +) {I NT ID;SCANF ("%d", &id); if (last[id] = = 0) cap[src][i] + = pnum[id];elsecap[last[id]][i] = Inf;last[id] = i;} scanf ("%d", &cap[i][sink]);} Dinic ();}


POJ 1149 PIGS (Network maximum flow dinic build the map you win)

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.