uva 11613 Acme Corporation(费用流)

本文介绍了一种解决UVA11613 Acme Corporation问题的方法,通过构建费用流网络来确定公司在未来的最大利润。具体步骤包括建立超级源点和汇点,设置各节点间的流量和费用,最终利用最小费用流算法得出答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

uva 11613 Acme Corporation

题目大意:Acme公司生产一种X元素。给出该元素在未来M个月中每个月的生产成本,最大产量,单位售价,最大销售量,以及最大储存时间。你的任务是计算出公司能够赚到的最大利润。
解题思路:把每个月,拆成a, b两点。设置一个超级源点,连向所有月份的a,容量为该月份最大产量,费用为该月单位生产成本。设置一个超级汇点,使所有的月份的b连向它,容量为该月的最大销售量,费用为负的该月份的单位售价。然后每格月份的a向该月份产出产品所能售出的所有月份连边(自己也要连),容量为INF, 费用为储存到该月份所需的费用。跑费用流,最后求出最小费用取负数
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <queue>
using namespace std;
typedef long long ll; 
const int N = 500;
const int M = 15000;
const ll INF = 1e18;
int n, m, s, t, Case;
int pre[N], inq[N]; 
ll a[N], d[N];

struct Edge{
    int from, to;
    ll cap, flow;
    ll cos;
};

vector<Edge> edges;
vector<int> G[M];

void init() {
    for (int i = 0; i < n * (n + 2); i++) G[i].clear();
    edges.clear();
}

void addEdge(int from, int to, ll cap, ll cos) {
    edges.push_back((Edge){from, to, cap, 0, cos});
    edges.push_back((Edge){to, from, 0, 0, -cos});
    int m = edges.size();
    G[from].push_back(m - 2);
    G[to].push_back(m - 1);
}

int BF(int s, int t, ll& flow, ll& cost) {
    queue<int> Q;
    memset(inq, 0, sizeof(inq));
    memset(a, 0, sizeof(a));
    memset(pre, 0, sizeof(pre));
    for (int i = 0; i < N; i++) d[i] = INF;
    d[s] = 0;
    a[s] = INF;
    inq[s] = 1;
    pre[s] = 0;
    Q.push(s);
    while (!Q.empty()) {
        int u = Q.front(); Q.pop();
        inq[u] = 0;
        for (int i = 0; i < G[u].size(); i++) {
            Edge &e = edges[G[u][i]];
            if (e.cap > e.flow && d[e.to] > d[u] + e.cos) {
                d[e.to] = d[u] + e.cos;
                a[e.to] = min(a[u], e.cap - e.flow);
                pre[e.to] = G[u][i];
                if (!inq[e.to]) {
                    inq[e.to] = 1;
                    Q.push(e.to);
                }
            }   
        }
    }
    if (d[t] > 0) return 0;
    flow += a[t];
    cost += (ll)d[t] * (ll)a[t];
    for (int u = t; u != s; u = edges[pre[u]].from) {
        edges[pre[u]].flow += a[t];
        edges[pre[u]^1].flow -= a[t];
    }
    return 1;
}

int MCMF(int s, int t, ll& cost) {
    ll flow = 0;
    cost = 0;       
    while (BF(s, t, flow, cost));
    return flow;
}

void input() {
    scanf("%d %d", &n, &m);
    s = 0, t = 2 * n + 1;
    int a, b, c, d, e;
    for (int i = 1; i <= n; i++) {
        scanf("%d %d %d %d %d", &a, &b, &c, &d, &e);    
        addEdge(s, i, b, a);
        for (int j = 0; j <= e; j++) {
            if (i + j <= n) addEdge(i, i + j + n, INF, m * j);  
        }
        addEdge(i + n, t, d, -c);
    }

}

void solve() {
    ll cost;
    MCMF(s, t, cost);
    printf("Case %d: %lld\n", Case++, -cost);
}

int main() {
    int T;
    Case = 1;
    scanf("%d", &T);
    while (T--) {
        init();
        input();    
        solve();
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值