luogu P3980 [NOI2008]志愿者招募 |费用流

题目描述

申奥成功后,布布经过不懈努力,终于成为奥组委下属公司人力资源部门的主管。布布刚上任就遇到了一个难题:为即将启动的奥运新项目招募一批短期志愿者。经过估算,这个项目需要 \(n\) 天才能完成,其中第 \(i\) 天至少需要 \(a_i\)​ 个人。布布通过了解得知,一共有 \(m\) 类志愿者可以招募。其中第 \(i\) 类可以从第 \(s_is\) 天工作到第 \(t_i\) 天,招募费用是每人 \(c_i\) 元。新官上任三把火,为了出色地完成自己的工作,布布希望用尽量少的费用招募足够的志愿者,但这并不是他的特长!于是布布找到了你,希望你帮他设计一种最优的招募方案。
输入格式

第一行包含两个整数 \(n,m\),表示完成项目的天数和可以招募的志愿者的种类。接下来的一行中包含 \(n\) 个非负整数,表示每天至少需要的志愿者人数。 接下来的 \(m\) 行中每行包含三个整数 \(s_i, t_i, c_i\)​,含义如上文所述。为了方便起见,我们可以认为每类志愿者的数量都是无限多的
输出格式

仅包含一个整数,表示你所设计的最优方案的总费用。


#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10,M=5e5+10,inf=0x3f3f3f3f;
int n,m,s,t;
int nxt[M],head[N],go[M],edge[M],cost[M],tot=1;
inline void add(int u,int v,int o1,int o2){
    nxt[++tot]=head[u],head[u]=tot,go[tot]=v,edge[tot]=o1,cost[tot]=o2;
    nxt[++tot]=head[v],head[v]=tot,go[tot]=u,edge[tot]=0,cost[tot]=-o2;    
}
int dis[N],ret;
bool vis[N];
inline bool spfa(){
    memset(dis,0x3f,sizeof(dis)); dis[s]=0;
    queue<int>q; q.push(s);
    while(q.size()){
        int u=q.front(); q.pop(); vis[u]=0;
        for(int i=head[u];i;i=nxt[i]){
            int v=go[i];
            if(edge[i]&&dis[v]>dis[u]+cost[i]){
                dis[v]=dis[u]+cost[i];
                if(!vis[v])q.push(v),vis[v]=1;
            }
        }
    }
    return dis[t]!=inf;
}
int dinic(int u,int flow){
    if(u==t)return flow;
    int rest=flow,k;
    vis[u]=1;
    for(int i=head[u];i&&rest;i=nxt[i]){
        int v=go[i];
        if(edge[i]&&!vis[v]&&dis[v]==dis[u]+cost[i]){
            k=dinic(v,min(rest,edge[i]));
            if(!k)dis[v]=-1;
            ret+=k*cost[i];
            edge[i]-=k;
            edge[i^1]+=k;
            rest-=k;
        }
    }
    vis[u]=0;
    return flow-rest;
}

signed main(){
    cin>>n>>m; t=n+m+1;
    for(int i=1,x;i<=n;i++){
        scanf("%d",&x);
        add(i,i+1,inf-x,0);
    }
    add(s,1,inf,0);
    add(n+1,t,inf,0);
    for(int i=1,x,y,c;i<=m;i++){
        scanf("%d%d%d",&x,&y,&c);
        add(x,y+1,inf,c);
    }
    int maxflow=0,flow=0;
    while(spfa())
    while(flow=dinic(s,inf))maxflow+=flow;
    cout<<ret<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/naruto-mzx/p/12210041.html