UVA11613 Acme Corproation 生产销售计划
题目大意
A公司生产一种元素,给出该元素在未来M个月中每个月的单位售价,最大生产量,生产成本,最大销售量和最大存储时间,和每月存储代价,问这家公司在M个月内所能赚大的最大利润(题意来源:http://blog.csdn.net/l123012013048/article/details/47962965)
题解
每个月拆成两个点,用于表示存储。S向X集合点连生产成本,X集合向Y集合连存储成本,Y集合向T连收益。
跑最小费用流。不要求最大流。
不(书)难(上)发(写)现(道),d[T]随着增广会逐渐增大,于是等d[T] >= 0,就不要继续增广了。此时得到的就是最小费用流。
第一次WA了,接着我同时完成如下事情:开大空间、变longlong、上网看输出格式有没有多空格换行……
然后A了
做UVA时间久了
踩过的坑多了去了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
inline long long max(long long a, long long b){return a > b ? a : b;}
inline long long min(long long a, long long b){return a < b ? a : b;}
inline long long abs(long long x){return x < 0 ? -x : x;}
inline void swap(long long &x, long long &y){long long tmp = x;x = y;y = tmp;}
inline void read(long long &x)
{
x = 0;char ch = getchar(), c = ch;
while(ch < '0' || ch > '9') c = ch, ch = getchar();
while(ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
if(c == '-') x = -x;
}
const long long INF = 0x3f3f3f3f3f3f3f3f;
struct Edge
{
long long u,v,w,c,nxt;
Edge(long long _u, long long _v, long long _w, long long _c, long long _nxt){u = _u;v = _v;w = _w;c = _c;nxt = _nxt;}
Edge(){}
}edge[1000010];
long long head[1000010], cnt = 1, S, T, q[1000010], he, ta, d[1000010], vis[1000010], from[1000010], ans;
inline void insert(long long a, long long b, long long c, long long d)
{
edge[++ cnt] = Edge(a, b, c, d, head[a]), head[a] = cnt;
edge[++ cnt] = Edge(b, a, 0, -d, head[b]), head[b] = cnt;
}
bool spfa()
{
memset(d, 0x3f, sizeof(d)), d[S] = 0, he = 0, ta = 1, q[0] = S, vis[S] = 1;
while(he < ta)
{
long long now = q[he ++];if(he > 1000000) he = 0;
for(long long pos = head[now];pos;pos = edge[pos].nxt)
{
long long v = edge[pos].v;
if(edge[pos].w && d[v] > d[now] + edge[pos].c)
{
d[v] = d[now] + edge[pos].c, from[v] = pos;
if(!vis[v])
{
q[ta ++] = v;
if(ta > 1000000) ta = 0;
}
}
}
vis[now] = 0;
}
return d[T] != INF;
}
long long flow()
{
long long mi = INF;
for(long long i = from[T];i;i = from[edge[i].u]) mi = min(mi, edge[i].w);
for(long long i = from[T];i;i = from[edge[i].u]) edge[i].w -= mi, edge[i ^ 1].w += mi, ans += edge[i].c * mi;
}
void mcf()
{
while(spfa())
{
if(d[T] >= 0) return;
flow();
}
}
long long t, n, c, cb[10000], cl[10000], dj[10000], masl[10000], mat[10000];
//n:月数
//c:存每个单位放一个月的代价
//cb[i]:每单元生产成本
//cl[i]:最大产量
//dj[i]:销售单价
//masl[i]:最大销售量
//mat[i]:储存最大时间
int main()
{
read(t);
S = 1000000, T = S + 1;
for(long long ca = 1;ca <= t;++ ca)
{
cnt = 1, ans = 0, memset(head, 0, sizeof(head));
read(n), read(c);
for(long long i = 1;i <= n;++ i)
{
read(cb[i]), read(cl[i]), read(dj[i]), read(masl[i]), read(mat[i]);
insert(S, i, cl[i], cb[i]);
insert(n + i, T, masl[i], - dj[i]);
}
for(long long i = 1;i <= n;++ i)
for(long long k = 0, j = i + k;k <= mat[i] && j <= n;++ k, ++ j)
insert(i, n + j, INF, c * k);
mcf();
printf("Case %lld: %lld
", ca, -ans);
}
return 0;
}