POJ 1149 PIGS Maximum flow

Source: Internet
Author: User

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 18255 Accepted: 8300

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: The pig farm has a pig sty, and Mike doesn't have a pigsty key, so he can't open the pigsty. Customers who buy pigs have the keys to the pigsty, and when they come to buy a pig, they open all the pigsty they have, so that Mike can redistribute the pigs in each pigsty, and when the customer goes, lock all the doors again, and there is no limit to the number of pigs to be accommodated in each pigsty. Now we know all the customers who have the keys and the number of pigs they want to buy, and how they need to be arranged in order to make the biggest sales volume. Enter the number of pigs that are told to start each pigsty, as well as the number of key numbers each customer has and the quantity of pigs to buy.

Analysis: A topic of network flow. The key is to construct a capacity network. "Graph theory algorithm theory, implementation and application" This book has a very detailed knowledge of the network flow explained.

#include <iostream> #include <stdio.h> #include <string> #include <cstring> #include <cmath > #include <algorithm> #include <queue> #define N 1009#define INF 999999999using namespace Std;int pre[n];    int cus[n][n];int flow[n][n];int pig[n];int last[n];int q[n*2];int n,m;int num,k;int main () {int m,n;        while (~SCANF ("%d%d", &m,&n)) {for (int i=1;i<=m;i++) scanf ("%d", &pig[i]);        memset (last,0,sizeof last);        memset (cus,0,sizeof cus);        int s=0,t=n+1;            for (int i=1;i<=n;i++) {scanf ("%d", &num);                for (int j=0;j<num;j++) {scanf ("%d", &k);                if (last[k]==0) cus[s][i]+=pig[k];            Else cus[last[k]][i]=inf;//said first Last[k] took the pig in the K pigsty after I in Take last[k]=i;        } scanf ("%d", &cus[i][t]); } for (int i=0;i<=n+2;i++) for (int j=0;j<=n+2;j++) flow[i][j]=0;            while (1)//multiple BFS for augmented {int mmin=inf;            for (int i=0;i<=n+2;i++) pre[i]=-2;            int qs=0,qe=1; pre[0]=-1;q[qs]=0;//start from source point while (pre[t]==-2 && qs<qe)//Find an augmented path and update {int V                =q[qs++];                    for (int i=1;i<=t;i++) {int p=cus[v][i]-flow[v][i];                        if (pre[i]==-2 && p) {pre[i]=v;                        Q[qe++]=i; Mmin=min (MMIN,P);            Record the minimum value that can be augmented each time}}} if (pre[t]==-2) break;                for (int i=pre[t],j=t; i!=-1; J=i,i=pre[i]) {flow[i][j]+=mmin;            FLOW[J][I]=-FLOW[I][J];        }} int ans=0;        for (int i=1;i<t;i++) ans+=flow[i][t];    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.