其他分享
首页 > 其他分享> > [NOI2008]志愿者招募 (费用流)

[NOI2008]志愿者招募 (费用流)

作者:互联网

大意: $n$天, 第$i$天要$a_i$个志愿者. $m$种志愿者, 每种无限多, 第$i$种工作时间$[s_i,t_i]$花费$c_i$, 求最少花费.

 

源点$S$连第一天, 容量$INF$

第$n+1$天连汇点$T$, 容量$INF$

第$i$天往后连$INF-a_i$

每个志愿者连$s_i$到$t_i+1$, 容量$INF$, 费用$c_i$

求出$S$到$T$的最小费用最大流即可

 

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <cstring>
#include <bitset>
#include <functional>
#include <random>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<',';hr;})
using namespace std;
typedef long long ll;
const int N = 1e6+10, INF = 0x3f3f3f3f, S = N-2, T = N-1;
int n, m, flow;
struct edge {
    int to,w,v,next;
    edge(int to=0,int w=0,int v=0,int next=0):to(to),w(w),v(v),next(next){}
} e[N];
int head[N], dep[N], vis[N], cur[N], f[N], cnt=1;
int pre[N],pre2[N];
queue<int> Q;
ll cost;
int spfa() {
    REP(i,1,n+1) f[i]=dep[i]=INF,vis[i]=0;
    f[S]=dep[S]=f[T]=dep[T]=INF;
    dep[S]=0,Q.push(S);
    while (Q.size()) {
        int u = Q.front(); Q.pop();
        vis[u] = 0;
        for (int i=head[u]; i; i=e[i].next) {
            if (dep[e[i].to]>dep[u]+e[i].v&&e[i].w) {
                dep[e[i].to]=dep[u]+e[i].v;
                pre[e[i].to]=u,pre2[e[i].to]=i;
                f[e[i].to]=min(f[u],e[i].w);
                if (!vis[e[i].to]) {
                    vis[e[i].to]=1;
                    Q.push(e[i].to);
                }
            }
        }
    }
    return dep[T]!=INF;
}
void EK(){
    while(spfa()) {
        int w = f[T];
        for (int u=T; u!=S; u=pre[u]) {
            e[pre2[u]].w-=w;
            e[pre2[u]^1].w+=w;
        }
        flow += w, cost += (ll)w*dep[T];
    }
}
void add(int u, int v, int w, int k) {
    e[++cnt] = edge(v,w,k,head[u]);
    head[u] = cnt;
    e[++cnt] = edge(u,0,-k,head[v]);
    head[v] = cnt;
}



int main() {
    scanf("%d%d",&n,&m);
    REP(i,1,n) {
        int t;
        scanf("%d",&t);
        add(i,i+1,INF-t,0);
    }
    add(S,1,INF,0);
    add(n+1,T,INF,0);
    REP(i,1,m) {
        int s,t,c;
        scanf("%d%d%d",&s,&t,&c);
        add(s,t+1,INF,c);
    }
    EK();
    printf("%lld\n",cost);
}
View Code

 

标签:dep,志愿者,REP,招募,int,NOI2008,INF,include,define
来源: https://www.cnblogs.com/uid001/p/11718910.html