您的位置:首页 > 其它

BZOJ1221: [HNOI2001] 软件开发

2015-12-24 17:03 162 查看

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

最小费用最大流
将每天拆成2个点i、i`,之间连一条容量为n[i]的边,为了保证这些边必须加满,可以将这些边的费用减去一个合适的值。
S向左边的点连容量为inf,费用为f的边。右边的点向T连容量为inf,费用为0的边。
右边的点i`向i+a-1连一条容量为inf,权值为fa的边;向i+b-1连一条容量为inf,费用为fb的边。
因为消毒后的毛巾不一定马上要用,将i向i+1连一条容量为inf,费用为0的边。
算一下不固定容量的费用流,最后再把权值加回去就行了。

#include<cstdio>
#include<cctype>
#include<queue>
#include<cmath>
#include<cstring>
#include<algorithm>
#define rep(i,s,t) for(int i=s;i<=t;i++)
#define dwn(i,s,t) for(int i=s;i>=t;i--)
#define ren for(int i=first[x];i!=-1;i=next[i])
using namespace std;
inline int read() {
int x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
}
typedef long long ll;
const int inf=1e9;
const int maxn=2010;
const int maxm=40010;
struct ZKW {
int n,m,s,t,Cnt,inq[maxn];
int first[maxn],next[maxm];
struct Edge {int from,to,flow,cost;}edges[maxm];
ll cost,ans,d[maxn];
void init(int n) {
this->n=n;m=0;
memset(first,-1,sizeof(first));
}
void AddEdge(int u,int v,int w,int cost) {
edges[m]=(Edge){u,v,w,cost};next[m]=first[u];first[u]=m++;
edges[m]=(Edge){v,u,0,-cost};next[m]=first[v];first[v]=m++;
}
int vis[maxn];deque<int> Q;
int BFS() {
rep(i,1,n) d[i]=inf;d[t]=0;
Q.push_back(t);
while(!Q.empty()) {
Cnt++;
int x=Q.front();inq[x]=0;Q.pop_front();
ren {
Edge& e=edges[i^1];
if(e.flow&&d[e.from]>d[x]+e.cost) {
d[e.from]=d[x]+e.cost;
if(!inq[e.from]) {
inq[e.from]=1;
if(Q.size()&&d[e.from]<=d[Q.front()]) Q.push_front(e.from);
else Q.push_back(e.from);
}
}
}
}
rep(i,0,m-1) edges[i].cost+=d[edges[i].to]-d[edges[i].from];
cost+=d[s];return d[s]!=inf&&cost<0;
}
int DFS(int x,int a) {
if(x==t||!a) {ans+=a*cost;return a;}
int f,flow=0;vis[x]=1;
ren {
Edge& e=edges[i];
if(e.flow&&!e.cost&&!vis[e.to]&&(f=DFS(e.to,min(a,e.flow)))) {
e.flow-=f;edges[i^1].flow+=f;
flow+=f;a-=f;if(!a) break;
}
}
return flow;
}
ll solve(int s,int t) {
this->s=s;this->t=t;
cost=ans=0;int flow=0,tmp;
while(BFS()) do {
memset(vis,0,sizeof(vis));
flow+=(tmp=DFS(s,inf));
}while(tmp);
return ans;
}
}sol;
int main() {
int n=read(),a=read()+1,b=read()+1,f=read(),fa=read(),fb=read();
int s=n*2+1,t=n*2+2;sol.init(t);int tot=0;
sol.AddEdge(s,1,inf,f);
rep(i,1,n) {
int tmp=read();tot+=tmp;
sol.AddEdge(i,i+n,tmp,-1000000);
if(i!=n) sol.AddEdge(i,i+1,inf,0);
if(i+a<=n) sol.AddEdge(i+n,i+a,inf,fa);
if(i+b<=n) sol.AddEdge(i+n,i+b,inf,fb);
sol.AddEdge(i+n,t,inf,0);
}
printf("%lld\n",sol.solve(s,t)+(ll)tot*1000000);
return 0;
}


View Code
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: