您的位置:首页 > 理论基础 > 计算机网络

BZOJ1391 [Ceoi2008]order

2016-05-22 23:50 363 查看
题意: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
,nxt[M],f[M],to[M],ch
,cr
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息