poj 1149 pigs

题意: 有m个猪舍,每个猪舍刚开始有一定量的猪,有n个顾客,他们按次序来到猪场,这些顾客每个人有k把猪舍的钥匙,顾客来的时候把这些猪舍打开,期间可以随意调整这些猪舍内猪所在的猪舍,顾客走后会把这些猪舍都关闭,每个顾客想买一定数量的猪,问猪场主人最多可以卖出去多少猪

思路: 最大流,流的是猪,建图:从源点向每个第一个打开猪舍的顾客连一条边,容量为猪舍内猪的数量,其余所有的顾客由他有的钥匙的猪舍的上一位打开的顾客到他连一条inf的边,所有顾客向汇点连一条容量为他要买的猪的数量的边,

“每个顾客能买的猪可以看做是上一个顾客留给他的”

觉得这里的想法建图很妙,mark下

code

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>
#include <map>
#include <stack>
using namespace std;
typedef long long ll;

struct Edge {
    ll from, to, cap, flow;
    Edge(ll a, ll b, ll c, ll d) : from(a), to(b), cap(c), flow(d) {}
};

struct Dinic {
    static const ll maxn = 110;
    static const ll inf = 0x3f3f3f3f3f3f3f3f;
    ll N, M, S, T;
    vector<Edge> edges;
    vector<ll> G[maxn];
    bool vis[maxn];
    ll d[maxn];
    ll cur[maxn];

    void AddEdge(ll from, ll to, ll 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() {
        memset(vis, 0, sizeof(vis));
        queue<ll> Q;
        Q.push(S);
        d[S] = 0;
        vis[S] = 1;
        while (!Q.empty()) {
            ll x = Q.front();
            Q.pop();
            for (ll i = 0; i < G[x].size(); i++) {
                Edge& e = edges[G[x][i]];
                if (!vis[e.to] && e.cap > e.flow) {
                    vis[e.to] = 1;
                    d[e.to] = d[x] + 1;
                    Q.push(e.to);
                }
            }
        }
        return vis[T];
    }

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

    ll Maxflow(ll S, ll T) {
        this->S = S, this->T = T;
        ll flow = 0;
        while (BFS()) {
            memset(cur, 0, sizeof(cur));
            flow += DFS(S, inf);
        }
        return flow;
    }
} MF;

const int N = 1010;
const int inf = 0x3f3f3f3f;
int mp[N], pre[N];

int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	
	memset(pre, -1, sizeof pre);
	
	int m, n;
	cin >> m >> n;
	int s = 0, t = n + 1;
	
	for(int i = 1; i <= m; ++ i) {
		cin >> mp[i];
	}
	for(int i = 1, num, b; i <= n; ++ i) {
		cin >> num;
		for(int j = 0, x; j < num; ++ j) {
			cin >> x;
			if(pre[x] == -1) {
				MF.AddEdge(s, i, mp[x]);
			}
			else {
				MF.AddEdge(pre[x], i, inf);
			}
			pre[x] = i;
		}
		cin >> b;
		MF.AddEdge(i, t, b);
	}
	
	cout << MF.Maxflow(s, t);
	
	return 0;
} 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值