[HNOI 2001]软件开发

时间:2023-03-09 06:51:41
[HNOI 2001]软件开发

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

题解

经典构图题。

将每一天拆成两个点$i$,$i’$,

加如下$6$条边:

$(sta, i, ni, f)$——在第$i$天可以买至多$ni$个餐巾,每块$f$分;

$(i, fin, ni, 0)$——第$i$天要用$ni$块餐巾;

$(sta, i’, ni, 0)$——第$i$天用剩的$ni$块旧餐巾;

$(i’, i+a+1, ∞, fa)$——第$i$天的旧餐巾送到快洗部,每块$fa$分;

$(i’, i+b+1, ∞, fb)$——第$i$天的旧餐巾送到慢洗部,每块$fb$分;

$(i’, i’+1, ∞, 0)$——第$i$天的旧餐巾可以留到第$i+1$天再处理;

求一次最小费用流即为结果。

 #include <set>
#include <map>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#define LL long long
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
using namespace std;
const int N = 2e3;
const int INF = ~0u>>; int n,a,b,f,fa,fb,ni;
struct tt{
int to, cost, next, cap;
}edge[N*+];
int path[N+], top = -;
void Add(int u, int v, int cap, int cost);
int sta, fin;
int min_cost_flow();
int SPFA(); int main(){
memset(path, -, sizeof(path));
scanf("%d%d%d%d%d%d", &n, &a, &b, &f, &fa, &fb);
sta = , fin = *n+;
for (int i = ; i <= n; i++){
  scanf("%d", &ni);
  Add(sta, i, ni, f);
   Add(i, fin, ni, );
   Add(sta, i+n, ni, );
  if (i+a+ <= n) Add(i+n, i+a+, INF, fa);
  if (i+b+ <= n) Add(i+n, i+b+, INF, fb);
  if (i+ <= n) Add(i+n, i++n, INF, );
}
printf("%d\n", min_cost_flow());
return ;
} void Add(int u, int v, int cap, int cost){
edge[++top].to = v;
edge[top].cost = cost;
edge[top].cap = cap;
edge[top].next = path[u];
path[u] = top;
edge[++top].to = u;
edge[top].cost = -cost;
edge[top].cap = ;
edge[top].next = path[v];
path[v] = top;
}
int min_cost_flow(){
int tolcost = ;
int tmp;
while (tmp = SPFA()) tolcost += tmp;
return tolcost;
}
int SPFA(){
int dist[N+];
memset(dist, /, sizeof(dist)); dist[sta] = ; dist[fin] = INF;
bool vis[N+] = {}; vis[sta] = ;
queue<int>Q;
while (!Q.empty()) Q.pop();
Q.push(sta);
int pre[N+] = {};
while (!Q.empty()){
  int u = Q.front(); Q.pop(); vis[u]=;
  for (int i = path[u]; i != -; i = edge[i].next){
  int v = edge[i].to;
   if (dist[v] > dist[u]+edge[i].cost && edge[i].cap > ){
     dist[v] = dist[u]+edge[i].cost;
     pre[v] = i;
     if (!vis[v]){
     vis[v] = ;
     Q.push(v);
     }
   }
  }
}
if (dist[fin] == INF) return ;
int minflow = INF;
for (int i = fin; i != sta; i = edge[pre[i]^].to)
   minflow = Min(minflow, edge[pre[i]].cap);
for (int i = fin; i != sta; i = edge[pre[i]^].to)
  edge[pre[i]].cap -= minflow,
  edge[pre[i]^].cap += minflow;
return dist[fin]*minflow;
}