[BZOJ1221][HNOI2001] 软件开发(费用流)

题目描述

传送门

题解

和餐巾费用问题一样。
你自己判断一下a天之后到底是i+a+1还是i+a吧= =

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;

const int max_n=1005;
const int max_N=max_n*2+2;
const int max_m=max_n*10;
const int max_e=max_m*2;
const int INF=1e9;

int n,a,b,f,fa,fb,N,mincost;
int r[max_n];
int tot,point[max_N],next[max_e],v[max_e],c[max_e],remain[max_e];
int dis[max_N],last[max_N];
bool vis[max_N];
queue <int> q;

inline void addedge(int x,int y,int z,int cap){
    ++tot; next[tot]=point[x]; point[x]=tot; v[tot]=y; c[tot]=z; remain[tot]=cap;
    ++tot; next[tot]=point[y]; point[y]=tot; v[tot]=x; c[tot]=-z; remain[tot]=0;
}
inline int addflow(int s,int t){
    int now=t,ans=INF;
    while (now!=s){
        ans=min(ans,remain[last[now]]);
        now=v[last[now]^1];
    }

    now=t;
    while (now!=s){
        remain[last[now]]-=ans;
        remain[last[now]^1]+=ans;
        now=v[last[now]^1];
    }
    return ans;
}
inline bool bfs(int s,int t){
    memset(dis,0x7f,sizeof(dis));
    dis[s]=0;
    memset(vis,0,sizeof(vis));
    vis[s]=true;
    while (!q.empty()) q.pop();
    q.push(s);

    while (!q.empty()){
        int now=q.front(); q.pop();
        vis[now]=false;
        for (int i=point[now];i!=-1;i=next[i])
          if (dis[v[i]]>dis[now]+c[i]&&remain[i]){
            dis[v[i]]=dis[now]+c[i];
            last[v[i]]=i;
            if (!vis[v[i]]){
                vis[v[i]]=true;
                q.push(v[i]);
            }
          }
    }
    if (dis[t]>INF) return false;
    int flow=addflow(s,t);
    mincost+=dis[t]*flow;
    return true;
}
inline void major(int s,int t){
    mincost=0;
    while (bfs(s,t));
}

int main(){
    tot=-1;
    memset(point,-1,sizeof(point));
    memset(next,-1,sizeof(next));

    scanf("%d%d%d%d%d%d",&n,&a,&b,&f,&fa,&fb);
    N=2*n+2;
    for (int i=1;i<=n;++i) scanf("%d",&r[i]);
    for (int i=1;i<=n;++i){
        addedge(1,1+i,0,r[i]);
        addedge(1+n+i,N,0,r[i]);
    }
    for (int i=1;i<=n;++i)
      addedge(1,1+n+i,f,INF);
    for (int i=1;i<=n;++i){
        if (i+a+1<=n) addedge(1+i,1+n+i+a+1,fa,INF);
        if (i+b+1<=n) addedge(1+i,1+n+i+b+1,fb,INF);
    }
    for (int i=1;i<n;++i)
      addedge(1+n+i,1+n+i+1,0,INF);
    major(1,N);
    printf("%d\n",mincost);
}

你可能感兴趣的:(网络流,hnoi,bzoj)