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-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

题目大意:本大大有m个猪圈,每个猪圈里都有对应的肥猪,但是我并没有打开猪圈的钥匙(钥匙在买猪的人身上,扯淡啊),现在有n个人要来买猪,这n个人都是陆续来的,并不是1次全来,一次来一个人,每个人身上有k把钥匙,对应猪圈编号,还知道他要买的猪的上限,还有就是这个买猪的每次来都会打开他能打开的所有的猪圈,买完猪后,本大大便可以随意分配这几个猪圈的猪,这样后面的人来买的时候可能收益更多,问本大大最多能卖出多少的pigs。

分析:因为是人买猪,所以我的源点连的就是人的编号,流量为该人买猪上限,人再连猪圈的编号,猪圈再连汇点,流量为猪圈中猪的数量。大体框架出来了,接下来就是,怎么连边的问题,我们从第一个人开始连,首先对于当前这个买猪人,我们可以知道他能打开的猪圈中哪些猪圈已经被前面的人打开过了,我们会把每个猪圈第一次被那个买主打开的记录下来,如果当前这个买猪人能打开的其中一些猪圈没有被前面任何一人打开过(这就代表本大大并不能改变这个猪圈的猪的数量撒),那么我们就把这个人和这个猪圈建一条边,流量为这个人买猪的数量。如果他要打开的猪圈已经被打开过了,那么我们就连一条边到最近打开这个猪圈的买主,此时不是连猪圈,流量为inf即可,画画图就会明白的。

#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<algorithm>
#define maxn 2000
#define inf 0x7fffffff
using namespace std;
struct edge
{
    int from,to,cap,flow;
};
vector<edge> edges;
vector<int> G[maxn];
int d[maxn],cur[maxn];
bool vis[maxn];
int s,t;
void add(int from,int to,int cap)
{
    edges.push_back((edge){from,to,cap,0});
    edges.push_back((edge){to,from,0,0});
    int m=edges.size();
    G[from].push_back(m-2);
    G[to].push_back(m-1);
}
bool bfs()
{
    memset(vis,false,sizeof(vis));
    memset(d,0,sizeof(d));
    queue<int> q;
    q.push(s);
    d[s]=0,vis[s]=true;
    while(!q.empty())
    {
        int x=q.front();
        q.pop();
        for(int i=0;i<G[x].size();i++)
        {
            edge& e=edges[G[x][i]];
            if(!vis[e.to]&&e.cap>e.flow)
            {
                vis[e.to]=true;
                d[e.to]=d[x]+1;
                q.push(e.to);
            }
        }
    }
    return vis[t];
}
int dfs(int x,int a)
{
    if(x==t||a==0) return a;
    int flow=0,f;
    for(int& i=cur[x];i<G[x].size();i++)
    {
        edge& e=edges[G[x][i]];
        if(d[x]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
        {
            e.flow+=f;
            edges[G[x][i]^1].flow-=f;
            flow+=f;
            a-=f;
            if(a==0) break;
        }
    }
    return flow;
}
int maxflow()
{
    int flow=0;
    while(bfs())
    {
        memset(cur,0,sizeof(cur));
        flow+=dfs(s,inf);
    }
    return flow;
}
int num[1002],id[1002];
int main()
{
    int n,m;
    while(~scanf("%d%d",&m,&n))
    {
        for(int i=0;i<maxn;i++) G[i].clear();
        edges.clear();
        s=0,t=n+m+10;
        memset(id,0,sizeof(id));
        for(int i=1;i<=m;i++)
            scanf("%d",&num[i]);
        for(int i=1;i<=n;i++)
        {
            int k,g;
            scanf("%d",&k);
            if(k==0)
            {
                scanf("%d",&g);
                continue;
            }
            for(int j=1;j<=k;j++)
            {
                int x;
                scanf("%d",&x);
                if(id[x]==0)
                {
                    add(i,n+x,num[x]);
                    add(n+x,t,num[x]);
                }
                else add(i,id[x],2000000);
                id[x]=i;//标记这个猪圈最近被此买主打开
            }
            scanf("%d",&g);
            add(s,i,g);
        }
        printf("%d\n",maxflow());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值