【洛谷】P3980 [NOI2008]志愿者招募

【洛谷】P3980 [NOI2008]志愿者招募

我居然现在才会用费用流解线性规划……

当然这里解决的一类问题比较特殊

以式子作为点,变量作为边,然后要求就是变量在不同的式子里出现了两次,系数一次为+1,一次为-1

这样的话就作为了一个出度和一个入度,和边正好对应了

我们设每种志愿者选择人数是\(x_{i}\)

我们的限制是

\[\left\{\begin{matrix} x_{i} \geq 0\\ x_{1} + x_{2} \geq A_{1}\\ x_{1} + x_{3} \geq A_{2}\\ x_{3} + x_{4} \geq A_{3} \end{matrix}\right. \]

假如数据是这样的吧~

但是我们需要流量平衡,于是我们新加几个变量

\[\left\{\begin{matrix} x_{i} \geq 0\\ y_{i} \geq 0\\ x_{1} + x_{2} - y_{1} - A_{1} = 0\\ x_{1} + x_{3} - y_{2} - A_{2} = 0\\ x_{3} + x_{4} - y_{3} - A_{3} = 0 \end{matrix}\right. \]

然后呢我们在前面和后面补上0 = 0

差分后就是

\[\left\{\begin{matrix} x_{i} \geq 0\\ y_{i} \geq 0\\ x_{1} + x_{2} - y_{1} - A_{1} = 0\\ x_{1} + x_{3} - y_{2} - A_{2} = 0\\ x_{3} + x_{4} - y_{3} - A_{3} = 0 \end{matrix}\right. \]

\[\left\{\begin{matrix} x_{i} \geq 0\\ y_{i} \geq 0\\ x_{1} + x_{2} - y_{1} - A_{1} = 0\\ -x_{2} + x_{3} - y_{2} + y_{1} + A_{1} - A_{2} = 0\\ -x_{1} + x_{4} - y_{3} + y_{2} + A_{2} - A_{3} = 0 \\ -x_{3} - x_{4} + y_{3} + A_{3} = 0 \end{matrix}\right. \]

这样的话就满足我们的限制了

我们对于每个式子建一个点,我们把\(-x_{1}\)的式子向\(x_{1}\)连一条边,容量为正无穷,费用为志愿者的费用

\(y\)同理,不过费用是0

然后再看常数项,如果常数项是负的,则向汇点流一条容量为常数项绝对值的边,如果是正的,则源点向这个式子流一条容量为常数项的边

#include <bits/stdc++.h>
#define fi first
#define se second
#define pii pair<int,int>
#define mp make_pair
#define pb push_back
#define space putchar(' ')
#define enter putchar('\n')
#define eps 1e-10
#define ba 47
#define MAXN 200005
//#define ivorysi
using namespace std;
typedef long long int64;
typedef unsigned int u32;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
    if(c == '-') f = -1;
    c = getchar();
    }
    while(c >= '0' && c <= '9') {
    res = res * 10 +c - '0';
    c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) {
    out(x / 10);
    }
    putchar('0' + x % 10);
}
struct node {
    int to,next,cap;int64 val;
}E[1000005];
int head[1005],sumE = 1,Ncnt,S,T,cur[1005];
int64 ans;
int N,M;
int st[10005],ed[10005],cs[10005],a[1005];
bool vis[1005];
void add(int u,int v,int c,int a) {
    E[++sumE].to = v;
    E[sumE].next = head[u];
    E[sumE].cap = c;
    E[sumE].val = a;
    head[u] = sumE;
}
void addtwo(int u,int v,int c,int a) {
    add(u,v,c,a);add(v,u,0,-a);
}
int64 dis[1005];
bool inq[1005];
queue<int> Q;
bool SPFA() {
    for(int i = 1 ; i <= Ncnt ; ++i) dis[i] = 1e18;
    dis[S] = 0;Q.push(S);
    memset(inq,0,sizeof(inq));
    while(!Q.empty()) {
    int u = Q.front();Q.pop();
    inq[u] = 0;
    for(int i = head[u] ; i ; i = E[i].next) {
        int v = E[i].to;
        if(E[i].cap) {
        if(dis[u] + E[i].val < dis[v]) {
            dis[v] = dis[u] + E[i].val;
            if(!inq[v]) Q.push(v);
        }
        }
    }
    }
    return dis[T] < 1e18;
}
int dfs(int u,int aug) {
    if(u == T) {
    ans += aug * dis[T];
    return aug;
    }
    vis[u] = 1;
    for(int i = head[u] ; i ; i = E[i].next) {
    int v = E[i].to;
    if(!vis[v] && E[i].cap && dis[v] == dis[u] + E[i].val) {
        int t = dfs(v,min(E[i].cap,aug));
        if(t) {
        E[i].cap -= t;
        E[i ^ 1].cap += t;
        return t;
        }
    }
    }
    return 0;
}
void MCMF() {
    while(SPFA()) {
    do {
        memset(vis,0,sizeof(vis));
    }while(dfs(S,0x7fffffff));
    }
}
void Solve() {
    read(N);read(M);
    S = N + 2;T = N + 3;Ncnt = N + 3;
    for(int i = 1 ; i <= N ; ++i) {
    read(a[i]);
    if(a[i] > a[i - 1]) addtwo(i,T,a[i] - a[i - 1],0);
    else addtwo(S,i,a[i - 1] - a[i],0);
    addtwo(i,i + 1,0x7fffffff,0);
    }
    addtwo(S,N + 1,a[N],0);
   
    
    for(int i = 1 ; i <= M ; ++i) {
    read(st[i]);read(ed[i]);read(cs[i]);
    addtwo(ed[i] + 1,st[i],0x7fffffff,cs[i]);
    }
    MCMF();
    out(ans);enter;
}
int main(){
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}
posted @ 2019-06-21 08:43  sigongzi  阅读(379)  评论(0编辑  收藏  举报