POJ 1149 pigsty buy pig Build map too powerful!! Without a thorough understanding, slowly digesting

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 19575 Accepted: 8948

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 Have 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 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 Writte N 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 Network flow good problem. There are M pigsty (m≤1000), with several pigs initially in each pigsty. At first all the pigsty were closed. In turn, N customers (N≤100), each customer will open a specified number of pig pens, from which to buy several pigs. 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.
#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <map> #include <algorithm> #include <set>  using namespace std; #define MM (A, B) memset (A,b,sizeof (a)) typedef long Long ll;typedef unsigned long long ull;const int mod = 1000000007;const double EPS = 1e-10;const int inf = 0x3f3f3f3f;const int big=50000;int max (int a,int b) {return a>b? A:B;}; int min (int a,int b) {return a<b?a:b;}; struct edge{int to,cap,rev;}; vector<edge> g[105];vector<int> Topig[1005];int n,m,want[105],pignum[1005],cap[105],level[105],iter[    105];void add_edge (int u,int v,int cap) {G[u].push_back (Edge{v,cap,g[v].size ()}); G[v].push_back (Edge{u,0,g[u].size ()-1});    void BFs (int s) {queue<int> q;    Q.push (s);    Level[s]=1;        while (Q.size ()) {int Now=q.front (); Q.pop ();       for (int i=0;i<g[now].size (); i++) if (g[now][i].cap>0) {Edge E=g[now][i];                  if (level[e.to]<0) {level[e.to]=level[now]+1;              Q.push (e.to);    }}}}int dfs (int s,int t,int minn) {if (s==t) return Minn;        for (int &i=iter[s];i<g[s].size (); i++) {Edge &e=G[s][i];            if (level[e.to]>level[s]&&e.cap>0) {int K=dfs (e.to,t,min (Minn,e.cap));                 if (k>0) {e.cap-=k;                 G[e.to][e.rev].cap+=k;             return k; }}} return 0;}    int max_flow (int s,int t) {int ans=0,temp;    for (;;)        {memset (level) (level,-1,sizeof);        BFS (s);        if (level[t]<0) return ans;        memset (Iter,0,sizeof (ITER));    while ((Temp=dfs (S,t,inf)) >0) ans+=temp; } return ans; void Buildgraph () {for (int i=1;i<=m;i++) if (Topig[i].size ()) {int Man=topigI                [0];                Cap[man]+=pignum[i];            for (int j=1;j<topig[i].size (); j + +) Add_edge (Topig[i][j-1],topig[i][j],inf);            } for (int i=1;i<=n;i++) {Add_edge (0,i,cap[i]);        Add_edge (I,n+1,want[i]);    }}void init () {MM (cap,0);    for (int i=0;i<n+1;i++) g[i].clear (); for (int i=1;i<=m;i++) topig[i].clear ();}        int main () {while (~SCANF ("%d%d", &m,&n)) {init ();        for (int i=1;i<=m;i++) scanf ("%d", &pignum[i]);              for (int i=1;i<=n;i++) {int num,pig;              scanf ("%d", &num);                 for (int j=1;j<=num;j++) {scanf ("%d", &pig);              Topig[pig].push_back (i);          } scanf ("%d", &want[i]);        } buildgraph ();    printf ("%d\n", Max_flow (0,n+1)); } return 0;}

Refer to this article

POJ 1149 pigsty buy pig Build map too powerful!! Without a thorough understanding, slowly digesting

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.