poj1149 PIGS

PIGS
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 21917   Accepted: 10034

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 个猪圈,每个猪圈里初始时有若干头猪。一开始所有猪圈都是关闭的。依
次来了 N 个顾客,每个顾客分别会打开指定的几个猪圈,从中买若干头猪。每
个顾客分别都有他能够买的数量的上限。每个顾客走后,他打开的那些猪圈中的
猪,都可以被任意地调换到其它开着的猪圈里,然后所有猪圈重新关上。问总共
最多能卖出多少头猪。(1 <= N <= 100, 1 <= M <= 1000
举个例子来说。有 3 个猪圈,初始时分别有 31 10 头猪。依次来了 3 个顾客,
第一个打开 1 号和 2 号猪圈,最多买 2 头;第二个打开 1 号和 3 号猪圈,最多买
3 头;第三个打开 2 号猪圈,最多买 6 头。那么,最好的可能性之一就是第一个
顾客从 1 号圈买 2 头,然后把 1 号圈剩下的 1 头放到 2 号圈;第二个顾客从 3
号圈买 3 头;第三个顾客从 2 号圈买 2 头。总共卖出 2+3+2=7 头。
分析:直观地建图,合并点,得到更优的建图方式.具体可以参看Edelweiss的网络流建模汇总这篇文章.
#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int maxn = 120,maxm = 2020,inf = 0x7fffffff;
int S,T,head[maxn],to[maxm],nextt[maxm],tot = 2,c[maxm],w[maxm];
int n,m,flag[maxm],ans,vis[maxn];
vector <int> G[maxn];

void add(int x,int y,int z)
{
    w[tot] = z;
    to[tot] = y;
    nextt[tot] = head[x];
    head[x] = tot++;
}

bool bfs()
{
    queue <int> q;
    memset(vis,-1,sizeof(vis));
    vis[S] = 0;
    q.push(S);
    while (!q.empty())
    {
        int u = q.front();
        q.pop();
        if (u == T)
            return true;
        for (int i = head[u];i;i = nextt[i])
        {
            int v = to[i];
            if (w[i] && vis[v] == -1)
            {
                vis[v] = vis[u] + 1;
                q.push(v);
            }
        }
    }
    return false;
}

int dfs(int u,int f)
{
    if (u == T)
        return f;
    int res = 0;
    for (int i = head[u];i;i = nextt[i])
    {
        int v = to[i];
        if (vis[v] == vis[u] + 1 && w[i])
        {
            int temp = dfs(v,min(f - res,w[i]));
            res += temp;
            w[i] -= temp;
            w[i ^ 1] += temp;
            if (res == f)
                return res;
        }
    }
    if (!res)
        vis[u] = -1;
    return res;
}

void dinic()
{
    while (bfs())
        ans += dfs(S,inf);
}

int main()
{
    scanf("%d%d",&m,&n);
    S = 0;
    T = n + 1;
    for (int i = 1; i <= m; i++)
        scanf("%d",&c[i]);
    for (int i = 1; i <= n; i++)
    {
        int num;
        scanf("%d",&num);
        for (int j = 1; j <= num; j++)
        {
            int x;
            scanf("%d",&x);
            G[i].push_back(x);
        }
        scanf("%d",&num);
        add(i,T,num);
        add(T,i,0);
    }
    for (int i = 1; i <= n; i++)
    {
        for (int j = 0;j < G[i].size(); j++)
        {
            int v = G[i][j];
            if (!flag[v])
            {
                flag[v] = i;
                add(S,i,c[v]);
                add(i,S,0);
            }
            else
            {
                add(flag[v],i,inf);
                add(i,flag[v],0);
                flag[v] = i;
            }
        }
    }
    dinic();
    printf("%d
",ans);

    return 0;
}
原文地址:https://www.cnblogs.com/zbtrs/p/8137681.html