POJ1149 PIGS "Maximum Flow"

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16555 Accepted: 7416

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 DayMain Topic
mirko kept some pigs in some pigsty, and the pigsty was locked.
He doesn't have a key himself (Khan)
• Only customers who want to buy a pig have the key
• Customers in turn each customer will use his key to open some pigsty to buy
Take some pigs and lock them up.
• Before the lock Mirko had the opportunity to allocate these several open pigsty again
The Pig
• Now give the number of pigs in every pigsty a key to every customer
And the number of pigs to buy, ask Mirko to sell a few pigs.Puzzle : For each of the first purchasers of a pigsty, add a source to the side of the person, the number of pigs in the pigsty, for later people who want to buy the pigsty. Add a first person to buy the pigsty to the person's side. The right is the INF, then joins each person to the meeting point one edge, the weight is the number of pigs that the person wants to buy. At this point, the composition 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%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 Mark Nth            The first person of a um pigsty unicom} else g[-pig[num]][i] = inf;        } scanf ("%d", &g[i][sink]);    } printf ("%d\n", Dinic ()); } return 0;}

2015.4.20

#include <iostream> #include <cstdio> #include <cstring>using namespace std;const int maxn = 105;const int inf = 0x3f3f3f3f;int G[MAXN][MAXN], M, N, S, T;int pighouse[maxn*10];int dinic (int S, int T), void Getmap () {memset (    G, 0, sizeof (g)); S = 0;    T = N + 1;    int I, J, K, POS;    for (i = 1; I <= M; ++i) scanf ("%d", &pighouse[i]);        for (i = 1; I <= N; ++i) {scanf ("%d", &k);            while (k--) {scanf ("%d", &pos);                if (Pighouse[pos] >= 0) {G[s][i] + = Pighouse[pos];            Pighouse[pos] =-I.;            } else {g[-pighouse[pos]][i] = inf;    }} scanf ("%d", &g[i][t]); }}void solve () {cout << dinic (S, T) << Endl;}        int main () {while (CIN >> M >> N) {getmap ();    Solve (); } return 0;} int Queue[maxn];bool VIS[MAXN]; int Layer[maxn];bool Countlayer (int s, int t) {memset (layer, 0, sizeof (layer));     int id = 0, front = 0, now, I; Layer[s] = 1;    queue[id++] = s;        while (front < ID) {now = queue[front++]; for (i = s; I <= t; ++i) if (G[now][i] &&!                Layer[i]) {layer[i] = Layer[now] + 1;                if (i = = t) return true;            else queue[id++] = i; }} return false;}    Source point, Sink point, source point number must be minimum, meeting point number must be maximum int dinic (int s, int t) {int mincut, pos, maxflow = 0;    int i, id = 0, u, V, now;        while (Countlayer (s, t)) {memset (Vis, 0, sizeof (VIS)); Vis[s] = true;        queue[id++] = s;            while (ID) {now = queue[id-1];                if (now = = t) {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]] = false;  } else {for (i = s; I <= t; ++i) {if (G[now][i] && Layer[now] + 1 = = Layer[i]                    &&!vis[i]) {vis[i] = 1; queue[id++] = i; break;            }} if (i > t)--id; }}} return maxflow;}



Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

POJ1149 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.