POJ 1149 PIGS (maximum flow)

Source: Internet
Author: User

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


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


"Main topic" (Ext: http://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html)
There was a pig sty, and in each pigsty there were several pigs at the beginning. At first all the pigsty were closed. In turn, there are N customers, each of whom will open a number of pig pens and buy some pigs from them. Each customer has the upper limit of the quantity he can buy separately. After each customer walks, the pig in the pigsty he opens can be arbitrarily swapped into the other open pigsty, and all the pigsty will be closed again. Ask how many pigs can be sold in total. (1 <= N <=, 1 <= M <= 1000)
For instance. There were 3 pigsty, initially with 3, 1 and 10 pigs. In turn, 3 customers, the first to open the 1th and 2nd pigsty, buy up to 2 head, the second to open the 1th and 3rd pigsty, buy 3 head, the third to open the 2nd pigsty, up to buy 6 head. One of the best possibilities, then, is that the first customer buys 2 heads from Circle 1th, and then puts the remaining 1 head of the 1th ring into the 2nd ring; the second customer buys 3rd heads from the 3 ring, and the third customer buys 2nd heads from the 2 ring. Sell 2+3+2=7 head altogether.

"Modeling Methods"
It is not difficult to imagine that the network model of the problem can be very intuitive to construct. Take the example above, you can construct the model shown in Figure 1 (usually without the number of edges, the capacity is ∞):
? Three customers, there are three rounds of trading, each round has 3 pigsty and a customer node. Each pigsty from the source to the first round has one side, and the capacity is the initial number of pigs in each pigsty.
? From each customer to the meeting point each has a side, capacity is the number of customers can buy the limit. In one round, all the pigsty opened from the customer have an edge connected to the customer, the capacity is ∞.
? Except for the last round, each round of the I-Pen has an edge-to-round I-pen, the capacity is ∞, indicating that the rest of the pig can be left to the next round.
? Except for the last round, from all the pigsty that were opened in each round, to the next round of the same these pigsty, 22 have to be connected to one side, indicating that they can circulate freely between

The code is as follows:

#include <cstdio> #include <cmath> #include <cstring> #include <string> #include <iostream > #include <algorithm>using namespace std; #define INF 0x3f3f3f3f#define maxn 500047#define maxm 1247int head[ MAXM], Pre[maxm];int DEP[MAXM], CUR[MAXM], Gap[maxm];int en;int con[maxm][maxm];//record whether the merchant is connected to pigsty struct edge{int to,next , Cap,flow;} EDGE[MAXN];  Note is Maxmint Tol;int K, C, M;int S, e;//source point, Meeting Point int map[maxm][maxm];//plus edge, unidirectional figure three parameters, bidirectional figure four parameters void Addedge (int u,int v,int w,int rw    = 0) {edge[tol].to = v;    Edge[tol].cap = W;    Edge[tol].flow = 0;    Edge[tol].next = Head[u];    Head[u] = tol++;    edge[tol].to = u;    Edge[tol].cap = RW;    Edge[tol].flow = 0;    Edge[tol].next = Head[v]; HEAD[V] = tol++;}    int q[maxn];void BFS (int start,int end) {memset (dep,-1,sizeof (DEP));    memset (Gap,0,sizeof (GAP));    Gap[0] = 1;    int front = 0, rear = 0;    Dep[end] = 0;    q[rear++] = end;        while (front! = rear) {int u = q[front++]; for (int i = Head[u]; I! =-1;            i = edge[i].next) {int v = edge[i].to;            if (dep[v]! =-1) continue;            q[rear++] = v;            DEP[V] = Dep[u] + 1;        gap[dep[v]]++;    }}}int s[maxn];//input parameter: The number of the start, end point, number of points//points is not affected, as long as the total number of input points int sap (int start,int end,int N) {BFS (start,end);    memcpy (cur,head,sizeof (head));    int top = 0;    int u = start;    int ans = 0;            while (Dep[start] < N) {if (U = = end) {int Min = INF;            int inser;                    for (int i = 0; i < top; i++) if (Min > Edge[s[i]].cap-edge[s[i]].flow) {                    Min = Edge[s[i]].cap-edge[s[i]].flow;                Inser = i;                } for (int i = 0; i < top; i++) {edge[s[i]].flow + = Min;            Edge[s[i]^1].flow = Min;            } ans + = Min;            top = Inser;            u = edge[s[top]^1].to;        Continue } bool FLAG = FALSE;        int V;            for (int i = cur[u]; I! =-1; i = edge[i].next) {v = edge[i].to;                if (edge[i].cap-edge[i].flow && dep[v]+1 = = Dep[u]) {flag = true;                Cur[u] = i;            Break            }} if (flag) {s[top++] = Cur[u];            U = V;        Continue        } int Min = N; for (int i = head[u]; i =-1; i = edge[i].next) if (Edge[i].cap-edge[i].flow && dep[edge[i].to] < M                IN) {Min = dep[edge[i].to];            Cur[u] = i;        } gap[dep[u]]--;        if (!gap[dep[u]]) return ans;        Dep[u] = Min + 1;        gap[dep[u]]++;    if (u! = start) u = edge[s[--top]^1].to; } return ans;    void Init () {memset (head,-1,sizeof (head));    memset (Con,0,sizeof (con)); EN = 0;}    int main () {int m, n;        while (~SCANF ("%d%d", &m,&n)) {init (); int nUm            for (int i = 1; I <= m; i++) {scanf ("%d", &num);        Addedge (0, I, num);//source Point} int A, b, C[MAXM];            for (int i = 1; I <= n; i++) {scanf ("%d", &a);                for (int j = 1; J <= A; j + +) {scanf ("%d", &c[j]); Addedge (C[j], m+i, INF);//Pig and merchant con[i][c[j]] = 1;//I can buy scanf ("%d", &b);//can buy a few            Head Addedge (m+i, m+n+1, b);//Merchant and meeting point} for (int i = 1; i < n; i++)//between merchant and merchant, before and after {            The int vis[maxm];//record has been added to the Edge memset (vis,0,sizeof (VIS)); for (int j = 1; j <= M; j + +) {if (Con[i][j])//If merchant can buy {for ( int k = i+1; K <= N;                             k++) {if (Con[k][j] &&!vis[k])//determine if merchant K and Pigsty J are connected {                    Addedge (M+i, M+k, INF);        Vis[k] = 1; }}}}} int sta = 0;//start int end = m+n+1;//meeting point I        NT N = m+n+2;//summary points int ans = SAP (STA,END,N);    printf ("%d\n", ans); } return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1149 PIGS (maximum flow)

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.