题意:给出 I 和M个月和 的以下信息:mi, ni, pi, si, Ei,表示单位生产成本,最大产量,销售单价,当月最大销量,以及最大存储时间,当Ei=1时,最多只能存储到下个月,否则报废,其中 I 表示每单位元素存储一个月花费的代价。求最大利润。
思路:将每个月建立两个点 ai,bi,从源点s连接到ai,容量为最大产量,费用为单位生产成本,从bi连接到汇点T,容量为最大销售量,费用为负的销售单价,从ai连接到bi,容量为无限大,费用为0,在不超过Ei的情况下,再连接ai到bj,容量也是无限大,费用是(j - i +1)*I,那么在求得的最小费用流后,负的费用就是最优的答案。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
const int maxn=210;
const int inf=1e8;
typedef long long LL;
struct Edge
{
int from,to,cap,flow,cost;
};
struct MCMF
{
int n,m,s,t;
vector<Edge>edges;
vector<int>G[maxn];
int inq[maxn];
LL d[maxn];
int p[maxn];
LL a[maxn];
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap,int cost)
{
edges.push_back((Edge){from,to,cap,0,cost});
edges.push_back((Edge){to,from,0,0,-cost});
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool Bellmanford(int s,int t,LL& flow,LL& cost)
{
for(int i=0;i<n;i++)d[i]=inf;
memset(inq,0,sizeof(inq));
d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;
queue<int>Q;
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.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],(LL)e.cap-e.flow);
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]==inf||d[t]>0)return false;
flow+=a[t];
cost+=d[t]*a[t];
int u=t;
while(u!=s)
{
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
u=edges[p[u]].from;
}
return true;
}
LL Mincost(int s,int t)
{
LL flow=0,cost=0;
while(Bellmanford(s,t,flow,cost));
return cost;
}
}solver;
int main()
{
int T,kase=0;
scanf("%d",&T);
while(T--)
{
int M,I,m,n,p,s,e;
scanf("%d%d",&M,&I);
solver.init(M*2+2);
int start=0,end=2*M+1;
for(int i=1;i<=M;i++)
{
scanf("%d%d%d%d%d",&m,&n,&p,&s,&e);
solver.AddEdge(start,i,n,m);
solver.AddEdge(i+M,end,s,-p);
solver.AddEdge(i,i+M,inf,0);
for(int j=1;j<=e;j++)
if(i+j<=M)
solver.AddEdge(i,i+j+M,inf,j*I);
}
printf("Case %d: %lld\n",++kase,-solver.Mincost(start,end));
}
}