poj 1149 PIGS(最大流)

Language:
PIGS
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 16815 Accepted: 7551

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个猪圈  每个猪圈里面有一些猪  

有顾客过来买猪  顾客拥有猪圈的钥匙  可以打开猪圈  然后买该猪圈里面的猪

猪可以在从一个猪圈移到另外一个猪圈  

卖猪的想要卖出最多的猪  求做多猪的数量


建图:

设立一个源点和一个汇点

将源点与每个猪圈的第一个顾客之间建边  权值为该猪圈中猪的数量 如果出现重边  权值相加

将每个顾客与汇点之间建边 权值为顾客买猪的数量

对于同一猪圈 上一个顾客与下一个顾客之间建边 权值为INF


#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#include <vector>
#include <queue>

#define eps 1e-8
#define op operator
#define MOD  10009
#define MAXN  1100
#define INF 30000000
#define MEM(a,x)    memset(a,x,sizeof a)
#define ll __int64

using namespace std;

struct Dinic
{
    struct Edge
    {
        int from,to,cap,flow;
        Edge(){}
        Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
    };
    vector<Edge> edges;
    vector<int> G[MAXN];
    bool vis[MAXN];
    int d[MAXN];
    int cur[MAXN];
    int n,m,s,t,maxflow;

    void init(int n)
    {
        this->n=n;
        for(int i=0;i<=n;i++)
            G[i].clear();
        edges.clear();
    }

    void addedge(int from,int to,int cap)
    {
        edges.push_back(Edge(from,to,cap,0));
        edges.push_back(Edge(to,from,0,0));
        m=edges.size();
        G[from].push_back(m-2);
        G[to].push_back(m-1);
    }

    bool bfs()
    {
        MEM(vis,0);
        MEM(d,-1);
        queue<int> q;
        q.push(s);
        d[s]=maxflow=0;
        vis[s]=1;
        while(!q.empty())
        {
            int u=q.front(); q.pop();
            int sz=G[u].size();
            for(int i=0;i<sz;i++)
            {
                Edge e=edges[G[u][i]];
                if(!vis[e.to]&&e.cap>e.flow)
                {
                    d[e.to]=d[u]+1;
                    vis[e.to]=1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int a)
    {
        if(u==t||a==0)  return a;
        int sz=G[u].size();
        int flow=0,f;
        for(int &i=cur[u];i<sz;i++)
        {
            Edge &e=edges[G[u][i]];
            if(d[u]+1==d[e.to]&&(f=dfs(e.to,min(a,e.cap-e.flow)))>0)
            {
                e.flow+=f;
                edges[G[u][i]^1].flow-=f;
                flow+=f;
                a-=f;
                if(a==0)  break;
            }
        }
        return flow;
    }

    int Maxflow(int s,int t)
    {
        this->s=s;  this->t=t;
        int flow=0;
        while(bfs())
        {
            MEM(cur,0);
            flow+=dfs(s,INF);
        }
        return flow;
    }
}Dic;


int main()
{
//freopen("ceshi.txt","r",stdin);
    int n,m;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        int c[MAXN];//各个猪圈的容量
        int last[MAXN];//各个猪圈上一次打开的人
        MEM(last,0);
        for(int i=1;i<=m;i++)
            scanf("%d",&c[i]);
        Dic.init(n+1);
        for(int i=1;i<=n;i++)
        {
            int num;
            scanf("%d",&num);
            for(int j=0;j<num;j++)
            {
                int x;
                scanf("%d",&x);
                if(last[x]==0)
                {
                    last[x]=i;
                    Dic.addedge(0,i,c[x]);
                }
                else
                {
//                    last[x]=i;
                    Dic.addedge(last[x],i,INF);
                    last[x]=i;
                }
            }
            int y;
            scanf("%d",&y);
            Dic.addedge(i,n+1,y);
        }
        printf("%d\n",Dic.Maxflow(0,n+1));
    }
    return 0;
}

//以上的代码很好的解决重边问题  出现重边 直接建边 




   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值