POJ 1149 迈克卖猪问题(PIGS) 最大流

来源:互联网 发布:网络票务 编辑:程序博客网 时间:2024/05/29 08:32

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <vector>#define INF 0x3f3f3f3fusing namespace std;int M,N;vector<int> que[1010];int pig[1010];int cus[110];const int maxe=1e4+1000;const int maxv=110;struct edge{    int to,w,next;}e[maxe<<1];int head[maxv<<1],depth[maxv],cnt;void init(){    memset(head,-1,sizeof(head));    cnt=-1;}void add_edge(int u,int v,int w){    e[++cnt].to=v;    e[cnt].w=w;    e[cnt].next=head[u];    head[u]=cnt;}void _add(int u,int v,int w){    add_edge(u,v,w);    add_edge(v,u,0);}void get_map(){    init();    for(int i=1;i<=M;i++)        _add(0,que[i][0],pig[i]);    for(int i=1;i<=N;i++)        _add(i,N+1,cus[i]);    for(int i=1;i<=M;i++)        for(int j=0;j<que[i].size()-1;j++)            _add(que[i][j],que[i][j+1],INF);}bool bfs(){    queue<int> q;    while(!q.empty()) q.pop();    memset(depth,0,sizeof(depth));    depth[0]=1;    q.push(0);    while(!q.empty())    {        int u=q.front();q.pop();        for(int i=head[u];i!=-1;i=e[i].next)        {            if(!depth[e[i].to] && e[i].w>0)            {                depth[e[i].to]=depth[u]+1;                q.push(e[i].to);            }        }    }    if(!depth[N+1]) return false;    return true;}int dfs(int u,int flow){    if(u==N+1) return flow;    int ret=0;    for(int i=head[u];i!=-1 && flow;i=e[i].next)    {        if(depth[u]+1==depth[e[i].to] && e[i].w!=0)        {            int tmp=dfs(e[i].to,min(e[i].w,flow));            if(tmp>0)            {                flow-=tmp;                ret+=tmp;                e[i].w-=tmp;                e[i^1].w+=tmp;            }        }    }    return ret;}void Dinic(){    int ans=0;    while(bfs())    {        ans+=dfs(0,INF);    }    cout<<ans<<endl;}int main(){    ios::sync_with_stdio(false);    while(cin>>M>>N)    {        memset(pig,0,sizeof(pig));        for(int i=0;i<1010;i++) que[i].clear();        memset(cus,0,sizeof(cus));        for(int i=1;i<=M;i++)            cin>>pig[i];        for(int i=1;i<=N;i++)        {            int k;            cin>>k;            while(k--)            {                int num;                cin>>num;                que[num].push_back(i);            }            cin>>cus[i];        }        get_map();        Dinic();    }    return 0;}

题目链接:https://cn.vjudge.net/problem/10509/origin


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 33 1 102 1 2 22 1 3 31 2 6
Sample Output
7


分析:

构造容量网络

(1)将顾客看作除源点和汇点以外的结点,并且另外设两个结点:源点和汇点

(2)源点和每个猪圈的第一个顾客连边,边的权是开始时猪圈的猪的数目

(3)每一个顾客和汇点连边,边的权是顾客想买的猪的数目

(4)顾客j紧跟着顾客i之后打开某个猪圈,则边<i,j>的权为正无穷大



参考:图论算法理论、实现及应用