POJ1149 PIGS 【最大流量】

POJ1149 PIGS 【最大流量】

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

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

题目大意
 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;
}

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;
}
// 源点,汇点,源点编号必须最小,汇点编号必须最大
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;
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/117136.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Postman使用教程图解

    Postman使用教程图解postman的主要功能1、模拟HTTPrequests的一些方法:get、post、put等2、Collection:测试集合,你每测试一个项目建立一个collection,把请求放在一起,方便日后查阅,而且还能Import或者Share,整个团队的人都可以看到;3、Response形式多样一般在用其他工具来测试的時候,response的内容通常都是纯文字的raw,但如果是JSON,就是塞成一整行的JSON。这会造成阅读的障碍,而Postman可以针对response

    2022年5月31日
    47
  • 写给大忙人看的 – Java中从MinIO服务器中下载文件(3)[通俗易懂]

    写给大忙人看的 – Java中从MinIO服务器中下载文件(3)[通俗易懂]前面两章介绍了MinIO文件服务器的环境搭建,以及在Java中上传文件至MinIO文件服务器中,现在,一起来看下如何从MinIO文件服务器中下载文件吧1、获取文件对象我们在MinIO工具类中,获取文件对象的方法,即获取文件的输入流对象/***获取文件**@parambucketNamebucket名称*@paramobjectName文件名称*@return二进制流*/@SneakyThrowspublicInputStreamge

    2022年7月12日
    271
  • mos管开关工作原理图解_双光耦开关电源电路图

    mos管开关工作原理图解_双光耦开关电源电路图在使用MOS管设计开关电源或者马达驱动电路的时候,大部分人都会考虑MOS的导通电阻,最大电压等,最大电流等,也有很多人仅仅考虑这些因素。这样的电路也许是可以工作的,但并不是优秀的,作为正式的产品设计也是不允许的。下面是我对MOSFET及MOSFET驱动电路基础的一点总结,其中参考了一些资料,非全部原创。包括MOS管的介绍,特性,驱动以及应用电路。1、MOS管种类和结构MOSFET管是FET的…

    2022年9月20日
    0
  • 10分钟拿下 HashMap「建议收藏」

    10分钟拿下 HashMap「建议收藏」请相信我,你一定会更优秀!文章目录:1、什么是HashMap?什么时候选择HashMap?2、HashMap数据结构及其工作原理?2.1数据结构2.2工作原理3、HashMap和HashTable的异同?4、如何优化HashMap?1、什么是HashMap?什么时候选择HashMap?说到容器,你肯定会想到Java中对象存储容器还有Arr…

    2022年4月19日
    37
  • js替换所有的回车换行符[通俗易懂]

    js替换所有的回车换行符[通俗易懂]//替换所有的回车换行functionTransferString(content){varstring=content;try{string=string.replace(/\r\n/g,””)string=string.replace(/\n/g,””);}catch(e){

    2022年5月24日
    84
  • 归并排序算法详细图解_归并排序算法描述

    归并排序算法详细图解_归并排序算法描述一、什么是归并排序1.概念归并排序(Mergesort)是建立在归并操作上的一种有效的排序算法,归并排序对序列的元素进行逐层折半分组,然后从最小分组开始比较排序,合并成一个大的分组,逐层进行,最终所有的元素都是有序的2.算法原理这是一个无序数列:4、5、8、1、7、2、6、3,我们要将它按从小到大排序。按照归并排序的思想,我们要把序列逐层进行拆分序列逐层拆分如下然后从下往上逐层合并,首先对第一层序列1(只包含元素4)和序列2(只包含元素5)进行合并创建一个大序列,序列长度为两个小序列长度

    2022年8月12日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号