POJ 1149--pigs "Max Flow && process heavy Edge && Classic Modeling"

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18123 Accepted: 8246

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

Test instructions: Mike works in a pig farm, pig farms have M pigsty, each pigsty has a certain number of pigs and are locked. Since Mike doesn't have a key, he can't open any pigsty. Customers who want to buy pigs come to the pig farm one after the other, each customer has a key to the pigsty, and they want to buy a certain number of pigs. Specific sales Process: When each customer arrives, he opens all the pigsty where he has the key, and Mike picks some pigs from the pigsty to sell them, and if Mike wants, he can redistribute the pigs in the open pigsty, and when the customer leaves, the pigsty is locked again. Note: There is no limit to the number of pigs that can be accommodated in a pigsty.

Now we know the number of pigs in each pigsty, all the keys each customer has and the number of pigs they want to buy, and ask how many pigs he can buy a day.


Composition method:

1: Set 0 as Source point, n + 1 as meeting point

2: The source point and the 1th customer of each pigsty are connected, the right side is the number of pigs in the pigsty at the beginning

3: If there is a heavy edge between the source point and a node, then the right is merged (so the flow from the source point is the number of pigs that all pigsty can provide)

4: Customer J immediately after the customer I open a pigsty, then the side < I, J > right is the INF, because, if the customer J immediately after the customer I open each pigsty, then Michael may be in accordance with the needs of the customer J in the other pig pens in the pigsty, so that customer J can buy the best possible Can be a lot of pigs.

5: The margin between each customer and meeting point is the number of pigs that the customer wants to buy (so the inflow of the meeting point is the number of pigs that each customer says to buy)


Composition of the data given in the question:

1: There are three customers, set as V1, V2, v3. Source point is 0, meeting point is n+1

2: The first customer of the first pigsty is V1, the first customer of the second pigsty is V1, the first customer of the third pigsty is V2, so that the source point to the V1 has a heavy edge, after merging, the weight is 3 + 1 = 4, the source point to the V2 has an edge, the weight value is 10.

3: Customer V2 immediately after the V1 opened the first pigsty, the customer V3 immediately behind the V1 open the second pigsty, so V1--v2, v1-->v3 have edges, the value of the INF

4: Each customer V1, V2, v3 to meeting point N+1 have an edge, the weights are 2,3,6 respectively.


#include <cstdio> #include <cstring> #include <iostream> #include <vector> #include <queue > #include <algorithm> #define MAXN 2200#define maxm 550000#define INF 0x3f3f3f3fusing namespace Std;int dist[ MAXN], Vis[maxn];int HEAD[MAXN], CUR[MAXN], cnt;vector<int>fir[maxn];//struct node{int u, V, cap, flow, next;}; Node Edge[maxm];int pig_num[maxn];//The number of pigs in each pigsty int buy_pig[maxn];//The number of pigs each customer wants to buy int m, n;    M is the number of pigsty, n is the number of customers void Init () {cnt = 0;    Memset (Head,-1, sizeof (head)); for (int i = 1; I <= m; ++i) Fir[i].clear ();}    void In_put () {for (int i = 1; I <= m; ++i) scanf ("%d", &pig_num[i]);        for (int i = 1; I <= n; ++i) {int keysum;        scanf ("%d", &keysum);            while (keysum--) {int k;            scanf ("%d", &k);        Fir[k].push_back (i);    } scanf ("%d", &buy_pig[i]);    }}void Add (int u, int v, int w) {int i; for (i = head[u]; I! = 1; i = edge[i].next) {if (edGE[I].V = = v) break;        } if (i = =-1) {//No heavy edges edge[cnt] = {u, V, W, 0, Head[u]};        Head[u] = cnt++;        EDGE[CNT] = {V, u, 0, 0, Head[v]};    HEAD[V] = cnt++;    } else {edge[i].cap + = W;        }}void Getmap () {for (int i =1; i <= m; ++i) {Add (0, fir[i][0], pig_num[i]);        for (int j = 0; J < fir[i].size ()-1; ++j) {Add (Fir[i][j], fir[i][j + 1], INF); }} for (int i = 1; I <= n; ++i) Add (i, n + 1, buy_pig[i]);}    BOOL BFS (int st, int ed) {queue<int>q;    memset (Dist,-1, sizeof (Dist));    memset (Vis, 0, sizeof (VIS));    DIST[ST] = 0;    VIS[ST] = 1;    Q.push (ST);        while (!q.empty ()) {int u = q.front ();        Q.pop ();            for (int i = head[u]; i =-1; i = Edge[i].next) {node E = Edge[i];                if (!VIS[E.V] && e.cap > E.flow) {vis[e.v] = 1;                DIST[E.V] = Dist[u] + 1;                if (e.v = = ed) return true;Q.push (E.V); }}} return false;}    int DFS (int x, int ed, int a) {if (x = = Ed | | a = = 0) return A;    int flow = 0, F;        for (int &i = cur[x]; I! =-1; i = Edge[i].next) {node &e = Edge[i];            if (dist[e.v] = = Dist[x] + 1 && (f = DFS (e.v, ed, Min (A, e.cap-e.flow))) > 0) {e.flow + = f;            edge[i ^ 1].flow-= f;            A-= f;            Flow + + F;        if (a = = 0) break; }} return flow;}    int Maxflow (int st, int ed) {int flowsum = 0;        while (BFS (St, ed)) {memcpy (cur, head, sizeof (head));    Flowsum + = DFS (St, Ed, INF); } return flowsum;}        int main () {while (scanf ("%d%d", &m, &n)! = EOF) {init ();        In_put ();        Getmap ();    printf ("%d\n", Maxflow (0, n + 1)); } return 0;}


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

POJ 1149--pigs "Max Flow && process heavy Edge && Classic Modeling"

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.