POJ1149 PIGS 【最大流】

PIGS

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

Description

Mirko works on a pig farm that 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 has 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 are available to Mirko early in the morning so that he can make a sales-plan in order to maximize the number of pigs sold.

More precisely, the procedure is as following: the customer arrives, opens all pig-houses to which he has the key, Mirko sells a certain number of pigs from all the unlocked pig-houses to him, and, if Mirko wants, he can redistribute the remaining pigs across
the unlocked pig-houses.

An unlimited number of pigs can be placed in every pig-house.

Write a program that will find the maximum number of pigs that he can sell on that day.

Input

The first line of input contains two integers M and N, 1 <= M <= 1000, 1 <= N <= 100, number of pighouses and number of customers. Pig houses are numbered from 1 to M and customers are numbered from 1 to N.

The next line contains M integeres, for each pig-house initial number of pigs. The number of pigs in each pig-house is 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 about the i-th customer is written in the (i+2)-th line):

A K1 K2 ... KA B It means that this customer has 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 3
3 1 10
2 1 2 2
2 1 3 3
1 2 6

Sample Output

7

Source

Croatia OI 2002 Final Exam - First day

题目大意

 Mirko养着一些猪 猪关在一些猪圈里面 猪圈是锁着的

他自己没有钥匙(汗)

 只有要来买猪的顾客才有钥匙

 顾客依次来 每个顾客会用他的钥匙打开一些猪圈 买

走一些猪 然后锁上

 在锁上之前 Mirko有机会重新分配这几个已打开猪圈

的猪

 现在给出一开始每个猪圈的猪数 每个顾客所有的钥匙

和要买走的猪数 问Mirko最多能卖掉几头猪

题解:对于每个猪圈的第一个购买的人,添加一条源点到这个人的边,权为这个猪圈的猪数,对于后来的且想要购买该猪圈的人,添加一条第一个购买该猪圈的人到该人的边,权为inf,然后添加每个人到汇点一条边,权值为该人想要购买的猪的头数。至此,构图完成。

#include <stdio.h>
#include <string.h>
#define inf 0x3fffffff
#define maxn 110
#define maxm 1002

int 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; // 这里是标记第num个猪圈联通的第一个人
                } else G[-pig[num]][i] = inf;
            }
            scanf("%d", &G[i][sink]);
        }
        printf("%d\n", Dinic());
    }
    return 0;
}
时间: 2024-08-11 01:34:20

POJ1149 PIGS 【最大流】的相关文章

poj1149 PIGS --- 最大流EK

有m个猪圈,给出初始时每个猪圈里有几头猪,有n个顾客,每个顾客可能在某k个猪圈里买猪,总共要买a头. 顾客依次买猪,每次买完后,猪圈主人可以把猪圈里的猪转移到别的猪圈.每个猪圈的容量是无限大的. 问一天最多能卖多少猪. 整体读下来可以知道,要卖更多的猪,就要在每个顾客买之前,把尽量多的猪转移到下一个顾客要可以买的k个猪圈里. 也就是一个最大流问题. 把相邻两个顾客所选的猪圈之间建边,容量是inf. 添加一个源点与第一个顾客建边,权值为每个猪圈里猪的头数,第一次的容量是受猪圈里猪的数量限制的而不是

POJ1149 PIGS [最大流 建图]

PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20662   Accepted: 9435 Description Mirko works on a pig farm that consists of M locked pig-houses and Mirko can't unlock any pighouse because he doesn't have the keys. Customers come t

poj1149 PIGS 最大流(神奇的建图)

一开始不看题解,建图出错了.后来发现是题目理解错了.  if Mirko wants, he can redistribute the remaining pigs across the unlocked pig-houses. 题目中的这句话非常关键,没理解就错掉了.有很多人写的题解只告诉我们怎么做,却没告诉我们为什么要那样做. 这句话是的意思是可以重组打开过的猪圈.也就是当客人打开猪圈(他能打开的都打开了)以后,他选择了需要买的猪以后,Mirko可以随意把剩下的猪分配到打开的猪圈中.当然,我

POJ1149 PIGS 最大流-建模

题目链接: POJ1149 题意: 麦克是农场主有N个猪圈,每个猪圈都有一把锁但麦克没有钥匙.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的猪.当每个顾客到来时,他将那些他拥有钥匙的猪圈全部打开:迈克从这些猪圈中挑出一些猪卖给他们:如果迈克愿意,迈克可以重新分配这些被打开的猪圈中的猪:当顾客离开时,猪圈再次被锁上.给出每个猪圈的初始的猪的数量,求麦克能卖出猪的最大数量 思路: 注意顾客是一个接一个有顺序来的 网络流以顾客为节点建模 1) 将顾客看作除源点和汇点

POJ1149 PIGS【特殊建图,最大流】

看题解做的,看懂了怎么建图后,套上挑战icpc的最大流模版,就A了,上题解 题意:M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买这些有钥匙的猪圈里的猪,而且要买一定数量的猪,每个猪圈有已知数量的猪,但是猪圈可以重新打开,将猪的个数,重新分配,以达到卖出的猪的数量最多. 思路:刚学网络流,表示很菜很菜很菜~~①构造网络,将顾客看成源点和汇点以外的结点,并设另外两个节点:源点和汇点.②源点和每个猪圈的第一个顾客连边,边的权是开始时候猪圈中猪的数量.③ 若源点和某个节点之间有重边,则将权合并

解题报告 之 POJ1149 PIGS

解题报告 之 POJ1149 PIGS Description Mirko works on a pig farm that 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 has keys to some pig-house

POJ 1149 PIGS 最大流

第一次做网络流,看着教材里面的题解做的= = 用的是Ford,应该是最好理解的把,就是不断的找有没有从源点到汇点的增广路然后更新. 建图真是难啊,而且感觉细节要注意的地方比较多,一开始没有考虑反向弧,WA了两发,sad... #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <strin

POJ 1149 PIGS(最大流+建图)

题目链接:http://poj.org/problem?id=1149 题意:M个猪圈,N个顾客,每个顾客有一些的猪圈的钥匙,只能购买能打开的猪圈里的猪,而且要买一定数量的猪,每个猪圈有已知数量的猪, 但是猪圈可以重新打开,将猪的个数,重新分配,但是只能将猪往当前打开状态的猪圈里赶,以达到卖出的猪的数量最多. 思路:还是4部分,源点->猪圈->猪圈->汇点 Accepted 976K 63MS C++ 能用EK水的,当然用EK水 #include <iostream> #in

POJ1149.PIGS(迈克卖猪问题)——最大流

http://poj.org/problem?id=1149 题目描述: 迈克在一个养猪场工作,养猪场里有M 个猪圈,每个猪圈都上了锁.由于迈克没有钥匙,所以他不能打开任何一个猪圈.要买猪的顾客一个接一个来到养猪场,每个顾客有一些猪圈的钥匙,而且他们要买一定数量的猪.某一天,所有要到养猪场买猪的顾客,他们的信息是要提前让迈克知道的.这些信息包括:顾客所拥有的钥匙(详细到有几个猪圈的钥匙.有哪几个猪圈的钥匙).要购买的数量.这样对迈克很有好处,他可以安排销售计划以便卖出的猪的数目最大.更详细的销售