bzoj 1221(费用流)

12 篇文章 0 订阅

1221: [HNOI2001] 软件开发

Time Limit: 10 Sec   Memory Limit: 162 MB
Submit: 1209   Solved: 671
[ Submit][ Status][ Discuss]

Description

某软件公司正在规划一项n天的软件开发计划,根据开发计划第i天需要ni个软件开发人员,为了提高软件开发人员的效率,公司给软件人员提供了很多的服务,其中一项服务就是要为每个开发人员每天提供一块消毒毛巾,这种消毒毛巾使用一天后必须再做消毒处理后才能使用。消毒方式有两种,A种方式的消毒需要a天时间,B种方式的消毒需要b天(b>a),A种消毒方式的费用为每块毛巾fA, B种消毒方式的费用为每块毛巾fB,而买一块新毛巾的费用为f(新毛巾是已消毒的,当天可以使用);而且f>fA>fB。公司经理正在规划在这n天中,每天买多少块新毛巾、每天送多少块毛巾进行A种消毒和每天送多少块毛巾进行B种消毒。当然,公司经理希望费用最低。你的任务就是:为该软件公司计划每天买多少块毛巾、每天多少块毛巾进行A种消毒和多少毛巾进行B种消毒,使公司在这项n天的软件开发中,提供毛巾服务的总费用最低。

Input

第1行为n,a,b,f,fA,fB. 第2行为n1,n2,……,nn. (注:1≤f,fA,fB≤60,1≤n≤1000)

Output

最少费用

Sample Input

4 1 2 3 2 1
8 2 1 6

Sample Output

38


解题思路:经典的费用流。对于费用流的问题,见图如果遇到

像最后要汇到汇点的流要留到下个点时,可以拆成两个点,

然后将新开的点作为之前要流出去的点。


#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int n,a,bg,fg,fa,fb,len,S,T,ans;
int to[21000],from[20000],next[20000],h[20000],f[20000],w[20000];
int dis[2101],q[1000000],pre[2101];
const int INF=0x7fffffff;
bool b[2101];


inline int read()
{
char y; int x=0,f=1; y=getchar();
while (y<'0' || y>'9') {if (y=='-') f=-1; y=getchar();}
while (y>='0'&& y<='9') {x=x*10+int(y)-48; y=getchar();}
return x*f;
}


void insert(int x,int y,int flow,int v)
 {
  ++len; to[len]=y; from[len]=x; next[len]=h[x]; h[x]=len; f[len]=flow; w[len]=v;
 }


bool spfa()
 {
  memset(dis,0x7f,sizeof(dis)); dis[S]=0;
  memset(b,true,sizeof(b)); b[S]=false;
  int tail=1,head=0; ++tail; q[tail]=S;
  while (head<tail)
  {
  ++head;
  int u=h[q[head]];
  while (u!=0)
  {
  if (f[u]>0 && dis[to[u]]>dis[q[head]]+w[u])
  {
  dis[to[u]]=dis[q[head]]+w[u];
  pre[to[u]]=u;
  if (b[to[u]])
  {
  b[to[u]]=false;
  ++tail; q[tail]=to[u];
}
}
u=next[u];
}
b[q[head]]=true;
 }
if (dis[T]<100000000) return true;else return false;
 }


void mcf()
 {
  int now=T; int mx=0x7fffffff;
  while (now!=S)
  {
   mx=min(mx,f[pre[now]]);
 now=from[pre[now]];
}
now=T; 
while (now!=S)
{
ans+=w[pre[now]]*mx;
f[pre[now]]-=mx; f[pre[now]^1]+=mx;
now=from[pre[now]];
}
 }


int main()
{
   n=read(); a=read(); bg=read(); fg=read(); fa=read(); fb=read();
   S=0; T=2001; len=1;
   for (int i=1;i<=n;++i)
    {
    int x=read();
        insert(S,i,x,0); insert(i,S,0,0);
    insert(i+n,T,x,0); insert(T,i+n,0,0);
    insert(S,i+n,INF,fg); insert(i+n,S,0,-fg);
    if (i!=n) {insert(i+n,i+n+1,INF,0); insert(i+n+1,i+n,0,0);}
   if (i+a+1<=n) insert(i,i+a+1+n,INF,fa),insert(i+a+1+n,i,0,-fa);
   if (i+bg+1<=n) insert(i,i+bg+1+n,INF,fb),insert(i+bg+1+n,i,0,-fb);
    }
   ans=0;
   while (spfa())
    {
    mcf();
}
   printf("%d",ans);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值