• BZOJ1221 [HNOI2001] 软件开发


    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1221

    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

    最少费用

    建图:

    建立附加源点和汇点S和T

    1. 源点到第i天的入点连容量ni费用为0的边

    2. 第i天的出点到汇点连容量ni费用为0的边

    3. 源点到第i天的出点连容量为INF费用为f的边(保证每天的出点到汇点满流)

    4. 第i天的入点到第i+a+1(i+b+1)天的出点连容量为INF费用为fa(fb)的边(代表两种消毒方式)

    5. 第i天的入点到第i+1天的入点连容量为INF费用为0的边(代表每天剩下的毛巾可以留到下一天)

    然后跑费用流

    昨天从TLE那里学到了费用流多路增广的姿势,一试吓一跳:单路增广3244ms,多路增广48ms

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstring>
     5 #include <queue>
     6 #define rep(i,l,r) for(int i=l; i<=r; i++)
     7 #define clr(x,y) memset(x,y,sizeof(x))
     8 #define travel(x) for(Edge *p=last[x]; p; p=p->pre)
     9 using namespace std;
    10 const int INF = 0x3f3f3f3f;
    11 const int maxn = 1010;
    12 inline int read(){
    13     int ans = 0, f = 1;
    14     char c = getchar();
    15     for(; !isdigit(c); c = getchar())
    16     if (c == '-') f = -1;
    17     for(; isdigit(c); c = getchar())
    18     ans = ans * 10 + c - '0';
    19     return ans * f;
    20 }
    21 struct Edge{
    22     Edge *pre,*rev; int to,cap,cost;
    23 }edge[maxn*12],*last[maxn<<1],*cur[maxn<<1],*pt;
    24 int n,a,b,f,fa,fb,x,S,T,cost,d[maxn<<1];
    25 bool isin[maxn<<1],vis[maxn<<1];
    26 queue <int> q;
    27 inline void add(int x,int y,int z,int w){
    28     pt->pre = last[x]; pt->to = y; pt->cap = z; pt->cost = w; last[x] = pt++;
    29     pt->pre = last[y]; pt->to = x; pt->cap = 0; pt->cost = -w; last[y] = pt++;
    30     last[x]->rev = last[y]; last[y]->rev = last[x];
    31 }
    32 bool spfa(){
    33     clr(isin,0); isin[S] = 1; q.push(S);
    34     clr(d,INF); d[S] = 0;
    35     while (!q.empty()){
    36         int now = q.front(); q.pop(); isin[now] = 0;
    37         travel(now){
    38             if (d[p->to] > d[now] + p->cost && p->cap > 0){
    39                 d[p->to] = d[now] + p->cost;
    40                 if (!isin[p->to]) isin[p->to] = 1, q.push(p->to);
    41             }
    42         }
    43     }
    44     return d[T] != INF;
    45 }
    46 int dfs(int x,int flow){
    47     if (x == T || (!flow)) return flow; vis[x] = 1; int w = 0;
    48     for(Edge *p = cur[x]; p && w < flow; p = p->pre){
    49         if (p->cap > 0 && (!vis[p->to]) && d[p->to] == d[x] + p->cost){
    50             int delta = dfs(p->to,min(p->cap,flow-w));
    51             p->cap -= delta; p->rev->cap += delta; w += delta;
    52             cost += p->cost * delta;
    53             if (p->cap) cur[x] = p;
    54         }
    55     }
    56     return w;
    57 }
    58 void mincost(){
    59     while (spfa()){
    60         clr(vis,0);
    61         rep(i,S,T) cur[i] = last[i];
    62         dfs(S,INF);
    63     }
    64 }
    65 int main(){
    66     n = read(); a = read(); b = read(); f = read(); fa = read(); fb = read();
    67     S = 0; T = n << 1 | 1;
    68     clr(last,0); pt = edge;
    69     rep(i,1,n) x = read(), add(S,i,x,0), add(i+n,T,x,0);
    70     rep(i,1,n) add(S,i+n,INF,f);
    71     rep(i,1,n-1) add(i,i+1,INF,0);
    72     rep(i,1,n){
    73         if (i + a + 1 <= n) add(i,i+n+a+1,INF,fa);
    74         if (i + b + 1 <= n) add(i,i+n+b+1,INF,fb);
    75     }
    76     mincost();
    77     printf("%d
    ",cost);
    78     return 0;
    79 }
    View Code
  • 相关阅读:
    Kivy Event 在哪里?
    kivy file import
    spark windows环境下spark安装和运行(2)
    spark windows环境下spark安装和运行(1)
    Spring Boot配置多个DataSource
    java jdbc 元数据使用
    asp.net core 3.x 授权默认流程
    asp.net core 3.x 授权中的概念
    IdentityServer4 源码分析
    rabbitmq 安装问题
  • 原文地址:https://www.cnblogs.com/jimzeng/p/bzoj1221.html
Copyright © 2020-2023  润新知