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)
上一篇 2022年1月10日 上午11:00
下一篇 2022年1月10日 上午11:00


相关推荐

  • java 递归方法卡住_递归算法怎么理解

    java 递归方法卡住_递归算法怎么理解Java递归方法1.说明定义:一个方法体内调用它自己方法递归是一种隐式的循环,它会重复的执行某段代码,但这种重复执行无须循环控制递归一定要向着已知的方向递归,否则这种递归就变成了无穷递归,类似于死循环2.code举例publicclassRecursionTest{publicstaticvoidmain(String[]args){RecursionTestx=newRecursionTest();System.

    2025年12月10日
    6
  • HMAC的图解

    HMAC的图解一 nbsp 什么是 HMACHMAC 是一种使用单向散列函数来构造消息认证码的方法 其中 HMAC 中的 H 就是 Hash 的意思 HMAC 中所使用的单向散列函数并不仅限于一种 任何高强度的单向散列函数都可以被用于 HMAC 如果将来设计出的新的单向散列函数 也同样可以使用 使用 SHA 1 SHA 224 SHA 256 SHA 384 SHA 512 所构造的 HMAC 分别称为 HMAC SHA1 HMAC S

    2026年3月16日
    2
  • C++ eigen_c++第三方库

    C++ eigen_c++第三方库前言Eigen就是一个线性代数的C++库。它对矩阵(MatrixMatrix)和向量(VectorVector)等相关线性代数的运算操作进行了比较系统的实现。一、矩阵1.定义矩阵模板函数共包含六个参数template<typename_Scalar,int_Rows,int_Cols,int_Options,int_MaxRows,int_MaxC…

    2022年10月19日
    4
  • springboot 与spring的区别_SpringBootVFS

    springboot 与spring的区别_SpringBootVFS一、概念1、SpringSpring是一个开源容器框架,可以接管web层,业务层,dao层,持久层的组件,并且可以配置各种bean,和维护bean与bean之间的关系。其核心就是控制反转(IOC),和面向切面(AOP),简单的说就是一个分层的轻量级开源框架。2、SpringMVCSpringMVC属于SpringFrameWork的后续产品,已经融合在SpringWebFlow里面。SpringMVC是一种web层mvc框架,用于替代servlet(处理|响应请求,获取表单参数,表单校

    2022年8月20日
    11
  • UML建模之状态图(Statechart Diagram)

    UML建模之状态图(Statechart Diagram)一 活动图的组成元素 ActivityDiag 活动状态图 Activity 2 动作状态 Actions 3 动作状态约束 ActionConstr 4 动作流 ControlFlow 5 开始节点 InitialNode 6 终止节点 FinalNode 7 对象 Objects 8 数据存储对象 DataStore

    2026年3月18日
    2
  • PetShop4架构设计分析(三,四)

    PetShop4架构设计分析(三,四) petshop4.0详解之三(PetShop数据访问层之消息处理)三、PetShop数据访问层之消息处理在进行系统设计时,除了对安全、事务等问题给与足够的重视外,性能也是一个不可避免的问题所在,尤其是一个B/S结构的软件系统,必须充分地考虑访问量、数据流量、服务器负荷的问题。解决性能的瓶颈,除了对硬件系统进行升级外,软件设计的合理性尤为重要。在前面我曾提到,分层式结构设计可能会在一定

    2022年10月16日
    4

发表回复

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

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