P4177 [CEOI2008]order 最小割

\(\color{#0066ff}{ 题目描述 }\)

有N个工作,M种机器,每种机器你可以租或者买过来. 每个工作包括若干道工序,每道工序需要某种机器来完成,你可以通过购买或租用机器来完成。 现在给出这些参数,求最大利润

\(\color{#0066ff}{输入格式}\)

第一行给出 N,M(1<=N<=1200,1<=M<=1200) 下面将有N组数据。

每组数据第一行给出完成这个任务能赚到的钱(其在[1,5000])及有多少道工序

接下来若干行每行两个数,分别描述完成工序所需要的机器编号及租用它的费用(其在[1,20000]) 最后M行,每行给出购买机器的费用(其在[1,20000])

\(\color{#0066ff}{输出格式}\)

最大利润

\(\color{#0066ff}{输入样例}\)

2 3
100 2
1 30
2 20
100 2
1 40
3 80
50
80
110

\(\color{#0066ff}{输出样例}\)

50

\(\color{#0066ff}{数据范围与提示}\)

none

\(\color{#0066ff}{ 题解 }\)

显然每个工作不是必须要选的,根据数据范围,显然是网络流

利润=总收益-成本,显然要最小割

目前有三个值,收益,租费,购买费

S到每个工作连收益的边

每个工作向需要机器连租费的边

每个机器向T连购买费的边

这样租金只会对当前工作有影响,而收益和购买费会对全局有影响

这样求出最小割,用总收益一减即可

#include<bits/stdc++.h>
#define LL long long
LL in() {
    char ch; LL x = 0, f = 1;
    while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
    for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
    return x * f;
}
const int maxn = 1e5 + 10;
const int inf = 0x7fffffff;
struct node {
    int to, dis;
    node *nxt, *rev;
    node(int to = 0, int dis = 0, node *nxt = NULL): to(to), dis(dis), nxt(nxt) { rev = NULL; }
    void *operator new(size_t) {
        static node *S = NULL, *T = NULL;
        return (S == T) && (T = (S = new node[1024]) + 1024), S++;
    }
};
node *head[maxn], *cur[maxn];
int dep[maxn], n, m, s, t;
void add(int from, int to, int dis) {
    head[from] = new node(to, dis, head[from]);
}
void link(int from, int to, int dis) {
    add(from, to, dis);
    add(to, from, 0);
    head[from]->rev = head[to];
    head[to]->rev = head[from];
}
bool bfs() {
    for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
    std::queue<int> q;
    q.push(s);
    dep[s] = 1;
    while(!q.empty()) {
        int tp = q.front(); q.pop();
        for(node *i = head[tp]; i; i = i->nxt) 
            if(!dep[i->to] && i->dis)
                dep[i->to] = dep[tp] + 1, q.push(i->to);
    }
    return dep[t];
}
int dfs(int x, int change) {
    if(x == t || !change) return change;
    int flow = 0, ls;
    for(node *i = cur[x]; i; i = i->nxt) {
        cur[x] = i;
        if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(i->dis, change)))) {
            change -= ls;
            flow += ls;
            i->dis -= ls;
            i->rev->dis += ls;
            if(!change) break;
        }
    }
    return flow;
}
int dinic() {
    int flow = 0;
    while(bfs()) flow += dfs(s, inf);
    return flow;
}
int main() {
    int ans = 0;
    n = in(), m = in();
    s = 0, t = n + m + 1;
    for(int i = 1; i <= n; i++) {
        int r = in();
        ans += r;
        link(s, i, r);
        for(int T = in(); T --> 0;) {
            int id = in(), v = in();
            link(i, n + id, v);
        }
    }
    for(int i = 1; i <= m; i++) link(n + i, t, in());
    printf("%d\n", ans - dinic());
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/olinr/p/10369567.html
今日推荐