POJ 1149 猪圈买猪 建图太强大!! 没有透彻领悟 慢慢消化

Posted smilesundream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1149 猪圈买猪 建图太强大!! 没有透彻领悟 慢慢消化相关的知识,希望对你有一定的参考价值。

PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 19575   Accepted: 8948

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

网络流好题。有 M 个猪圈(M ≤ 1000),每个猪圈里初始时有若干头猪。 一开始所有猪圈都是关闭的。依次来了 N 个顾客(N ≤ 100),每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共最多能卖出多少头猪。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
struct edge{
   int to,cap,rev;
};

vector<edge> G[105];
vector<int> topig[1005];
int n,m,want[105],pignum[1005],cap[105],level[105],iter[105];

void add_edge(int u,int v,int cap)
{
    G[u].push_back(edge{v,cap,G[v].size()});
    G[v].push_back(edge{u,0,G[u].size()-1});
}

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    level[s]=1;
    while(q.size())
    {
        int now=q.front();q.pop();
        for(int i=0;i<G[now].size();i++)
        if(G[now][i].cap>0)
        {
            edge e=G[now][i];
            if(level[e.to]<0)
              {
                  level[e.to]=level[now]+1;
                  q.push(e.to);
              }
        }
    }
}
int dfs(int s,int t,int minn)
{
    if(s==t)
        return minn;
    for(int &i=iter[s];i<G[s].size();i++)
    {
        edge &e=G[s][i];
        if(level[e.to]>level[s]&&e.cap>0)
        {
            int k=dfs(e.to,t,min(minn,e.cap));
            if(k>0)
             {
                 e.cap-=k;
                 G[e.to][e.rev].cap+=k;
                 return k;
             }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int ans=0,temp;
    for(;;)
    {
        memset(level,-1,sizeof(level));
        bfs(s);
        if(level[t]<0)
            return ans;
        memset(iter,0,sizeof(iter));
        while((temp=dfs(s,t,inf))>0)
            ans+=temp;
    }
    return ans;
}

void buildgraph()
{
    for(int i=1;i<=m;i++)
        if(topig[i].size())
            {
                int man=topig[i][0];
                cap[man]+=pignum[i];
                for(int j=1;j<topig[i].size();j++)
                    add_edge(topig[i][j-1],topig[i][j],inf);
            }

    for(int i=1;i<=n;i++)
        {
            add_edge(0,i,cap[i]);
            add_edge(i,n+1,want[i]);
        }
}

void init()
{
    MM(cap,0);
    for(int i=0;i<n+1;i++) G[i].clear();
    for(int i=1;i<=m;i++) topig[i].clear();
}

int main()
{
    while(~scanf("%d %d",&m,&n))
    {
        init();
        for(int i=1;i<=m;i++)
            scanf("%d",&pignum[i]);
        for(int i=1;i<=n;i++)
          {
              int num,pig;
              scanf("%d",&num);
              for(int j=1;j<=num;j++)
              {
                 scanf("%d",&pig);
                 topig[pig].push_back(i);
              }
              scanf("%d",&want[i]);
          }

        buildgraph();

        printf("%d\n",max_flow(0,n+1));
    }
    return 0;
}

  参考了这篇题解

以上是关于POJ 1149 猪圈买猪 建图太强大!! 没有透彻领悟 慢慢消化的主要内容,如果未能解决你的问题,请参考以下文章

POJ1149:PIGS——题解

POJ - 1149 PIGS (建图思维+最大流)

poj1149 PIGS

poj 1149 PIGS最大流

POJ 1149 PIGS (最大流)

PIGS POJ - 1149(水最大流)