poj1149 PIGS(网络流最大流 + 建图)

PIGS

Time Limit: 1000MS Memory Limit: 10000K
Total Submissions:24778 Accepted: 11281

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

Croatia OI 2002 Final Exam - First day

题意:

有 m 个猪圈, n 个人,每个人有若干猪圈的钥匙,只能购买这些猪圈里的猪,且有数量上限, n 个人按读入顺序购买,当前面的人打开了某猪圈的门,卖家可以把当前被打开门的猪圈里的猪重新分配,以便下一个买家来买。问最多能卖掉多少猪。

思路:

最大流(反正我是没有想到。

对每个人建点,再加上源点和汇点。源点和每个猪圈的第一个买家之间连一条边,流量是猪圈中猪的初始数量,如果源点和一个买家之间有多条边,将他们合并,流量相加。前面的买家离开后,后面的买家相当于可以买之前被打开过门的猪圈中的猪,即钥匙可以传递,于是在同一猪圈有序的 x 个买家之间,连边 x[i] -> x[i + 1],流量为inf(极大值),表示之前猪圈中的猪可以传递到后面的买家。最后每个买家和汇点连边,流量为买家的需求数。

参考:https://blog.csdn.net/shahdza/article/details/6831946

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1005;
const int M = 2e4 + 10;
const int mod = 1e9 + 7;
const int inf = 0x3f3f3f3f;

int head[N], tot, n, m, s, t, d[N], val[N];
vector<int>mp[N];
struct Edge {
    int to, next, cap, flow;
}edge[M];

void init() {
    tot = 2;
    memset(head, -1, sizeof(head));
}

void addedge(int u, int v, int w, int rw = 0) {
    edge[tot].to = v;
    edge[tot].cap = w;
    edge[tot].flow = 0;
    edge[tot].next = head[u];
    head[u] = tot++;

    edge[tot].to = u;
    edge[tot].cap = rw;
    edge[tot].flow = 0;
    edge[tot].next = head[v];
    head[v] = tot++;
}

int Q[N];
int dep[N], cur[N], sta[N]; ///数组cur记录点u之前循环到了哪一条边
bool bfs(int s, int t, int n) {
    int fron = 0, tail = 0;
    memset(dep, -1, sizeof(dep[0]) * (n + 1));
    dep[s] = 0;
    Q[tail++] = s;
    while(fron < tail) {
        int u = Q[fron++];
        for(int i = head[u]; i != -1; i = edge[i].next) {
            int v = edge[i].to;
            if(edge[i].cap > edge[i].flow && dep[v] == -1) {
                dep[v] = dep[u] + 1;
                if(v == t) return true;
                Q[tail++] = v;
            }
        }
    }
    return false;
}

int dinic(int s, int t, int n) {
    int maxflow = 0;
    while(bfs(s, t, n)) {
        for(int i = 0; i <= n; ++i) cur[i] = head[i];
        int u = s, tail = 0;
        while(cur[s] != -1) {
            if(u == t) {
                int tp = inf;
                for(int i = tail - 1; i >= 0; --i)
                    tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);
                maxflow += tp;
                for(int i = tail - 1; i >= 0; --i) {
                    edge[sta[i]].flow += tp;
                    edge[sta[i] ^ 1].flow -= tp;
                    if(edge[sta[i]].cap - edge[sta[i]].flow == 0)
                        tail = i;
                }
                u = edge[sta[tail] ^ 1].to;
            }
            else if(cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) {
                sta[tail++] = cur[u];
                u = edge[cur[u]].to;
            }
            else {
                while(u != s && cur[u] == -1)
                    u = edge[sta[--tail] ^ 1].to;
                cur[u] = edge[cur[u]].next;
            }
        }
    }
    return maxflow;
}

int main() {
    init();
    int v, w, nn, mm, k, a;
    scanf("%d%d", &mm, &nn);
    n = nn + 2;
    for(int i = 1; i <= mm; ++i) {
        scanf("%d", &d[i]);
        mp[i].clear();
    }
    for(int i = 1; i <= nn; ++i) {
        scanf("%d", &k);
        for(int j = 1; j <= k; ++j) {
            scanf("%d", &a);
            mp[a].push_back(i);
        }
        scanf("%d", &a);
        addedge(i, nn + 1, a);
        val[i] = 0;
    }
    for(int i = 1; i <= mm; ++i)
        val[mp[i][0]] += d[i];
    for(int i = 1; i <= nn; ++i)
        addedge(0, i, val[i]);
    for(int i = 1; i <= mm; ++i)
        for(int j = 0; j < mp[i].size() - 1; ++j)
            addedge(mp[i][j], mp[i][j + 1], inf);
    printf("%d\n", dinic(0, nn + 1, n));    //源点0
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值