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;
}