POJ1149——PIGS(网络流)

PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 13349 Accepted: 5895
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

解析:

           这是一道最大流。。。只是建图较难

【建模方法】 
不难想象,这个问题的网络模型可以很直观地构造出来。就拿上面的例子来说,可以构造出图 1所示的模型(图中凡是没有标数字的边,容量都是∞): 
•  三个顾客,就有三轮交易,每一轮分别都有 3 个猪圈和 1 个顾客的结点。  
•  从源点到第一轮的各个猪圈各有一条边,容量就是各个猪圈里的猪的初始数量。 
•  从各个顾客到汇点各有一条边,容量就是各个顾客能买的数量上限。 
•  在某一轮中,从该顾客打开的所有猪圈都有一条边连向该顾客,容量都是∞。 
•  最后一轮除外,从每一轮的 i 号猪圈都有一条边连向下一轮的 i 号猪圈,容量都是∞,表示这一轮剩下的猪可以留到下一轮。 
•  最后一轮除外,从每一轮被打开的所有猪圈,到下一轮的同样这些猪圈,两两之间都要连一条边,表示它们之间可以任意流通。 

       这个网络模型的最大流量就是最多能卖出的数量。图中最多有+N+M×N≈100,000个结点。这个模型虽然很直观,但是结点数太多了,计算速
       度肯定会很慢。其实不用再想别的算法,就让我们继续上面的例子,用合并的方 法来简化这个网络模型。 
 

对刚才所说的简单模型缩点后重新建图的规律:

•  每个顾客分别用一个结点来表示。 
•  对于每个猪圈的第一个顾客,从源点向他连一条边,容量就是该猪圈里的
猪的初始数量。如果从源点到一名顾客有多条边,则可以把它们合并成一
条,容量相加。 
•  对于每个猪圈,假设有 n个顾客打开过它,则对所有整数 i∈[1, n),从该
猪圈的第i 个顾客向第 i + 1个顾客连一条边,容量为∞。 
•  从各个顾客到汇点各有一条边,容量是各个顾客能买的数量上限。 

代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int inf=100000000;

int n,m,sum[1010],open[110];
int pre[1010];
int map[1010][1010],l=0;
int head[1010],d[1010],sumd[1010];
struct node
{
    int u,v,c,next;
}edge[10101];

void addedge(int u,int v,int c)
{
    edge[l].u=u;
    edge[l].v=v;
    edge[l].c=c;
    edge[l].next=head[u];
    head[u]=l++;
    edge[l].u=v;
    edge[l].v=u;
    edge[l].c=0;
    edge[l].next=head[v];
    head[v]=l++;
}

void readdata()
{
    freopen("poj1149.in","r",stdin);
    freopen("poj1149.out","w",stdout);
    memset(map,0,sizeof(map));
    cin>>m>>n;
    {
        for(int i=1;i<=m;i++)scanf("%d",&sum[i]);
        for(int i=1;i<=n;i++)
        {
            int k;
            scanf("%d",&k);
            for(int j=1;j<=k;j++)
            {
                int u;
                scanf("%d",&u);
                if(pre[u]==0)
                {
                    map[0][i]+=sum[u];
                    pre[u]=i;
                }else map[pre[u]][i]=inf;
            }
            scanf("%d",&open[i]);
            map[i][n+1]+=open[i];
        }
    }
}

int max_flow(int u,int flow)
{
    if(u==n+1)return flow;
    int sum=0;
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].v;
        if(edge[i].c && d[u]==d[v]+1)
        {
            int t=max_flow(v,min(flow-sum,edge[i].c));
            edge[i].c-=t;edge[i^1].c+=t;
            sum+=t;
            if(sum==flow)return flow;
        }
    }
    if(d[0]>=n+2) return sum;
    sumd[d[u]]--;
    if(sumd[d[u]]==0)d[0]=n+2;
    sumd[++d[u]]++;
    return sum;
}

void work()
{
    memset(head,255,sizeof(head));
    memset(pre,0,sizeof(pre));
    for(int i=0;i<=n+1;i++)
        for(int j=0;j<=n+1;j++)
        {
            if(map[i][j])
            {
                //cout<<i<<' '<<j<<' '<<map[i][j]<<endl;
                addedge(i,j,map[i][j]);
            }
        }
    sumd[0]=n+2;
    int ans=0;
    while(d[0]<=n+1)
    {
        ans+=max_flow(0,2*inf);
    }
    cout<<ans<<endl;
}


int main()
{
    readdata();
    work();
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值