ZOJ 3229 Shoot the Bullet(有源汇有上下界的最大流)

ZOJ 3229 Shoot the Bullet

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3229

题意:一个屌丝给m个女神拍照,计划拍照n天,每一天屌丝最多给C个女神拍照,每天拍照数不能超过D张,而且给每个女神 i 拍照有数量限制[Li,Ri],对于每个女神n天的拍照总和不能超过Gi,如果有解求屌丝最多能拍多少张照,并求每天给对应女神拍多少张照;否则输出-1。

思路:
有源汇有上下界的最大流
1. 在原先的网络流基础上,增设一个超级源点,一个超级汇点。
2. 然后从原先的汇点向源点连接一条流量为INF的边。转化为成无源汇有上下界的可行流
3. 对超级源点和超级汇点做一次网络流,如果没有可行流(即从源点出发每条边都满流)退出, 否则继续。
4. 在有可行流的基础上,删除超级源点、超级汇点,对原先的源点和汇点再进行一次网络流,得到最大流。
5. 此时每条边上的可行流加上下界即为对应的流量。

代码:
/*
ID: [email protected]
PROG:
LANG: C++
*/
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdio>
#include<vector>
#include<string>
#include<fstream>
#include<cstring>
#include<ctype.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define INF (1 << 30)
#define LINF (1LL << 60)
#define PI acos(-1.0)
#define mem(a, b) memset(a, b, sizeof(a))
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define eps 1e-6
#define debug puts("===============")
#define pb push_back
#define mkp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define POSIN(x,y) (0 <= (x) && (x) < n && 0 <= (y) && (y) < m)
typedef long long ll;
typedef unsigned long long ULL;
const int maxn = 15000;
const int maxm = 555555;
struct node {
    int v;    // vertex
    int cap;    // capacity
    int flow;   // current flow in this arc
    int nxt;
} e[maxm * 2];
int g[maxn], cnt;
int st, ed, n, m, ST, ED, id_cnt;
int low[maxm], id[maxm], tot[maxn];
void add(int u, int v, int c, int k) {
    e[++cnt].v = v;
    e[cnt].cap = c;
    e[cnt].flow = 0;
    e[cnt].nxt = g[u];
    g[u] = cnt;
    id[k] = cnt;

    e[++cnt].v = u;
    e[cnt].cap = 0;
    e[cnt].flow = 0;
    e[cnt].nxt = g[v];
    g[v] = cnt;
    id[0] = 0;
}

void init() {
    memset(g, 0, sizeof(int) * (m + n + 10));
    memset(tot, 0, sizeof(int) * (m + n + 10));
    cnt = 1;
    id_cnt = 0;
    st = n + m + 2, ed = n + m + 3;
    ST = 0, ED = n + m + 1;
    int u, v, L, U, c;
    for (int i = 1; i <= m; i++) {
        scanf("%d", &c);
        tot[n + i] -= c;
        tot[ED] += c;
        add(n + i, ED, INF - c, 0);
    }
    int C, D;
    for (int i = 1; i <= n; i++) {
        scanf("%d%d", &C, &D);
        add(ST, i, D, 0);
        for (int j = 0; j < C; j++) {
            scanf("%d%d%d", &v, &L, &U);
            low[++id_cnt] = L;
            add(i, n + v + 1, U - L, id_cnt);
            tot[i] -= L;
            tot[n + v + 1] += L;
        }
    }
    add(ED, ST, INF, 0);
}

int dist[maxn], numbs[maxn], q[maxn];
void rev_bfs() {
    int font = 0, rear = 1;
    for (int i = 0; i <= n; i++) { //n为总点数
        dist[i] = maxn;
        numbs[i] = 0;
    }
    q[font] = ed;
    dist[ed] = 0;
    numbs[0] = 1;
    while(font != rear) {
        int u = q[font++];
        for (int i = g[u]; i; i = e[i].nxt) {
            if (e[i ^ 1].cap == 0 || dist[e[i].v] < maxn) continue;
            dist[e[i].v] = dist[u] + 1;
            ++numbs[dist[e[i].v]];
            q[rear++] = e[i].v;
        }
    }
}
int maxflow() {
    rev_bfs();
    int u, totalflow = 0;
    int curg[maxn], revpath[maxn];
    for(int i = 0; i <= n; ++i) curg[i] = g[i];
    u = st;
    while(dist[st] < n) {
        if(u == ed) {   // find an augmenting path
            int augflow = INF;
            for(int i = st; i != ed; i = e[curg[i]].v)
                augflow = min(augflow, e[curg[i]].cap);
            for(int i = st; i != ed; i = e[curg[i]].v) {
                e[curg[i]].cap -= augflow;
                e[curg[i] ^ 1].cap += augflow;
                e[curg[i]].flow += augflow;
                e[curg[i] ^ 1].flow -= augflow;
            }
            totalflow += augflow;
            u = st;
        }
        int i;
        for(i = curg[u]; i; i = e[i].nxt)
            if(e[i].cap > 0 && dist[u] == dist[e[i].v] + 1) break;
        if(i) {   // find an admissible arc, then Advance
            curg[u] = i;
            revpath[e[i].v] = i ^ 1;
            u = e[i].v;
        } else {    // no admissible arc, then relabel this vertex
            if(0 == (--numbs[dist[u]])) break;    // GAP cut, Important!
            curg[u] = g[u];
            int mindist = n;
            for(int j = g[u]; j; j = e[j].nxt)
                if(e[j].cap > 0) mindist = min(mindist, dist[e[j].v]);
            dist[u] = mindist + 1;
            ++numbs[dist[u]];
            if(u != st)
                u = e[revpath[u]].v;    // Backtrack
        }
    }
    return totalflow;
}
int sum;
bool lowbound_flow() {
    sum = 0;
    for (int i = 0; i <= n + m + 1; i++) {
        if (tot[i] > 0) add(st, i, tot[i], 0), sum += tot[i];
        if (tot[i] < 0) add(i, ed, -tot[i], 0);
    }
    n = n + m + 6;
    if (sum == maxflow()) return true;
    return false;
}
int lowbound_maxflow() {
    g[st] = g[ed] = 0;
    st = ST, ed = ED;
    return maxflow();
}
int main () {
    while(~scanf("%d%d", &n, &m)) {
        init();
        if (!lowbound_flow()) puts("-1\n");
        else {
            printf("%d\n", lowbound_maxflow());
            for (int i = 1; i <= id_cnt; i++) printf("%d\n", e[id[i]].flow + low[i]);
            puts("");
        }
    }
    return 0;
}





你可能感兴趣的:(图论,网络流,有上下界的网络流)