Bootstrap

HDU - 6126 Give out candies

Give out candies

题解:

第一次遇见这样处理的网络流模型。

将问题转换成最小割问题。

具体的题解参考自:传送门

先将每个人的拆成m个人。

然后s向第1人连边流量为inf。第i个人向第i+1个人连边,流量为 3000 - w。 将t视为每组的第m+1个人。

接来下是约束关系的建边, x, y ,z。

如果x小朋友拿了j个糖果,则y小朋友拿的糖果至少为y-z。

则在x的拆点和y对应的拆点之间建立一条流量为inf的边。

最后跑完最大流的时候。

如果ans >= inf 则说明没有最小割, 无解。

否则 ans = n * 3000 - ans.

 

代码:

#include<bits/stdc++.h>
using namespace std;
#define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout);
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define pb push_back
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define lch(x) tr[x].son[0]
#define rch(x) tr[x].son[1]
#define max3(a,b,c) max(a,max(b,c))
#define min3(a,b,c) min(a,min(b,c))
typedef pair<int,int> pll;
const int inf = 0x3f3f3f3f;
const int _inf = 0xc0c0c0c0;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const LL _INF = 0xc0c0c0c0c0c0c0c0;
const LL mod =  (int)1e9+7;
const int N = 3000;
const int M = 2e5;
int head[N], deep[N], cur[N];
int w[M], to[M], nx[M];
int tot;
void add(int u, int v, int val){
    w[tot]  = val; to[tot] = v;
    nx[tot] = head[u]; head[u] = tot++;

    w[tot] = 0; to[tot] = u;
    nx[tot] = head[v]; head[v] = tot++;
}
int bfs(int s, int t){
    queue<int> q;
    memset(deep, 0, sizeof(deep));
//    for(int i = s; i <= t; ++i) deep[i]=0;
    q.push(s);
    deep[s] = 1;
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u]; ~i; i = nx[i]){
            if(w[i] > 0 && deep[to[i]] == 0){
                deep[to[i]] = deep[u] + 1;
                q.push(to[i]);
            }
        }
    }
    return deep[t] > 0;
}
LL Dfs(int u, int t, LL flow){
    if(u == t) return flow;
    for(int &i = cur[u]; ~i; i = nx[i]){
        if(deep[u]+1 == deep[to[i]] && w[i] > 0){
            LL di = Dfs(to[i], t, min(1ll*w[i], flow));
            if(di > 0){
                w[i] -= di, w[i^1] += di;
                return di;
            }
        }
    }
    return 0;
}

LL Dinic(int s, int t){
    LL ans = 0, tmp;
    while(bfs(s, t)){
        for(int i = 0; i <= t; i++) cur[i] = head[i];
        while(tmp = Dfs(s, t, inf)) ans += tmp;
    }
    return ans;
}
void init(){
    memset(head, -1, sizeof(head));
    tot = 0;
}
int main(){
    int T;
    scanf("%d", &T);
    for(int _cas = 1; _cas <= T; ++_cas){
        init();
        int n, m, k, s, t;
        scanf("%d%d%d", &n, &m, &k);
        s = 0, t = n * m + 1;
//        assert(t > 300);
        for(int i = 0, v; i < n; ++i){
            add(s, i*m+1, inf);
            for(int j = 1; j <= m; ++j){
                scanf("%d", &v);
                if(j == m) add(i*m+j, t, 3000-v);
                else add(i*m+j, i*m+j+1, 3000-v);
            }
        }
        for(int i = 1, x,y,z; i <= k; ++i){
            scanf("%d%d%d", &x, &y, &z);
            for(int j = 1; j <= m; ++j){
                if(j-z < 1) add(x*m-m+j, s, inf);
                else if(j-z > m) add(x*m-m+j, t, inf);
                else add((x-1)*m+j, (y-1)*m+j-z, inf);
            }
        }
        LL ans = Dinic(s, t);
        if(ans >= inf) puts("-1");
        else printf("%lld\n", 3000*n-ans);
    }
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/MingSD/p/11130696.html

;