BZOJ1391 [Ceoi2008]order

题意:n个工作,m种机器(1 <= n,m <= 1200),每个机器可以买或者租,每个工作需要用指定的一些机器,如果租机器那么每个工作的租用费用不同,求最大利润。

分析:

比较裸的蕴含式最大获利问题。

源点向每个工作连一条容量为利润的边,每个机器向汇点连一条容量为购买价格的边,然后每个工作向其需要用的机器连一条容量为租用费用的边,则答案为工作利润之和-最小割。

不过这个交上去会TLE...

有一种优化方法叫当前弧优化,大概就是记录一下下回找增广路从第几条边开始找。

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
 
const int inf = 0x3fffffff, N = 2405, M = 3000000;
int n,m,x,y,s,t,c,d,e,sum,hd[N],nxt[M],f[M],to[M],ch[N],cr[N];
void add(int x, int y, int z) {
    to[e] = y, f[e] = z, nxt[e] = hd[x], hd[x] = e++;
    to[e] = x, f[e] = 0, nxt[e] = hd[y], hd[y] = e++;
}
 
bool tell() {
    memset(ch, -1, sizeof ch);
    queue<int> q;
    q.push(s), ch[s] = 0;
    while(!q.empty()) {
        int u = q.front(); q.pop();
        for(int i = hd[u]; ~i; i = nxt[i]) if(!~ch[to[i]] && f[i])
            ch[to[i]] = ch[u]+1, q.push(to[i]);
    }
    return ~ch[t];
}
int zeng(int a, int b) {
    if(a == t) return b;
    int r = 0;
    for(int i = cr[a]; ~i && b > r; i = nxt[i]) if(ch[to[i]] == ch[a]+1 && f[i]) {
        int w = zeng(to[i], min(b-r, f[i]));
        r += w, f[i] -= w, f[i^1] += w;
        if(f[i]) cr[a] = i;	//重要!! 
    }
    if(!r) ch[a] = -1;	//重要!! 
    return r;
}
int dinic() {
    int r = 0, w;
    while(tell()) {
        for(int i = 0; i <= t; i++) cr[i] = hd[i];
        while(w = zeng(s, inf)) r += w;
    }
    return r;
}
 
int main() {
    memset(hd, -1, sizeof hd);
    scanf("%d%d", &n, &m), t = n+m+1;
    for(int i = 1; i <= n; i++) {
        scanf("%d%d", &x, &y), sum += x, add(s, i, x);
        while(y--) scanf("%d%d", &c, &d), add(i, c+n, d);
    }
    for(int i = 1; i <= m; i++) scanf("%d", &x), add(n+i, t, x);
    printf("%d\n", sum-dinic());
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值