PIGS POJ - 1149 (最大流SAP算法)

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

    题意:n个顾客,m个猪圈,开始猪圈都关闭,每个顾客来了,他有某些猪圈的钥匙,他从这些猪圈中买猪,但数量有限定,在下一个顾客来之前,这些猪圈剩下的猪可以互换猪圈,求最多能卖多少猪;

思路:主要是建图难,这题用下面的规律就可以了

规律 1. 如果几个节点的流量的来源完全相同,则可以把它们合并成一个。

规律 2. 如果几个节点的流量的去向完全相同,则可以把它们合并成一个。

规律 3. 如果从点 u 到点 v 有一条容量为 +∞ 的边,并且 u 是 v 的唯一流量来源,或者 v 是 u 的唯一流量去向,则可以把 u 和 v 合并成一个节点

构图文章:http://wenku.baidu.com/view/0ad00abec77da26925c5b01c.html

然后下面主要是SAP的模板了,学习了一波,当图复杂就很实用了,虽然这道题,EK算法也很快大笑

#include<cstdio>
#include<cmath>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=1005;
const int M=20010;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
struct node
{
    int from,to,next,w;
}edge[M];
int head[N],cur[N],dep[N],stacks[N],gap[N],vis[N],flag[N];
int ans,tot;
void init()
{
    tot=0;
    memset(head,-1,sizeof(head));
    memset(stacks,-1,sizeof(stacks));
    memset(dep,-1,sizeof(dep));
    memset(gap,0,sizeof(gap));
    memset(vis,0,sizeof(vis));
    memset(flag,0,sizeof(flag));
}
void add(int u,int v,int w)
{
    edge[tot].from=u;
    edge[tot].to=v;
    edge[tot].w=w;
    edge[tot].next=head[u];
    head[u]=tot++;
    edge[tot].from=v;
    edge[tot].to=u;
    edge[tot].w=0;
    edge[tot].next=head[v];
    head[v]=tot++;
}
int pig[N];
void SAP(int s,int t,int n)
{
    ans=0;
    for(int i=0;i<=n;i++)
        cur[i]=head[i];
    gap[0]=n;
    int i,u=s,top=0;
    while(dep[s]<n)
    {
        if(u==t)//增广路计算流量stacks存路径
        {
            int temp=inf,num;
            for(i=0;i<top;i++)
            {
                if(temp>edge[stacks[i]].w)
                {
                    temp=edge[stacks[i]].w;
                    num=i;
                }
            }
            for(i=0;i<top;i++)
            {
                edge[stacks[i]].w-=temp;
                edge[stacks[i]^1].w+=temp;
            }
            ans+=temp;
            top=num;//这条边容量为0
            u=edge[stacks[top]].from;
        }
        for(i=cur[u];i!=-1;i=edge[i].next)
            if(edge[i].w&&dep[u]==dep[edge[i].to]+1)
            break;
        if(i!=-1)//找到可行弧
        {
            cur[u]=i;
            stacks[top++]=i;
            u=edge[i].to;
        }
        else
        {
            int mins=n;
            for(i=head[u];i!=-1;i=edge[i].next)//寻找最小dep
            {
                if(edge[i].w==0) continue;
                if(mins>dep[edge[i].to])
                {
                    mins=dep[edge[i].to];
                    cur[u]=i;
                }
            }
            if(0==--gap[dep[u]])//gap判断,出现断层,跳出循环,相较于EK的优化地方 
                break;
            dep[u]=mins+1;
            ++gap[dep[u]];
            if(u!=s)        //由于无可行弧往前推
                u=edge[stacks[--top]].from;
        }
    }
}
int main()
{
    int m,n,s,e;
    while(~scanf("%d%d",&m,&n))
    {
        init();
        s=n+1;
        e=s+1;
        for(int i=1;i<=m;i++)
            scanf("%d",&pig[i]);
        for(int k=1;k<=n;k++)
        {
            int a,b,p;
            scanf("%d",&a);
            for(int i=1;i<=a;i++)
            {
                scanf("%d",&p);
                if(!vis[p])//猪圈v没有被开过
                {
                    if(!flag[k])//顾客k没有开过猪圈
                    {
                        add(s,k,pig[p]);
                        flag[k]=tot-2;
                    }
                    else
                        edge[flag[k]].w+=pig[p];
                    vis[p]=k;
                }
                else
                {
                    add(vis[p],k,inf);
                    vis[p]=k;
                }
            }
            scanf("%d",&b);
            add(k,e,b);
        }
        SAP(s,e,e);
        printf("%d\n",ans);
    }
    return 0;
}

内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值