• ZOJ 3229 Shoot the Bullet


    ZOJ_3229

        有源汇有上下界的最大流问题,具体的思路可以参考这篇博客:http://blog.csdn.net/water_glass/article/details/6823741,感觉按套路来做就可以了。注意最后输出的时候不是要输出所有的MM。

    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    #define MAXN 375
    #define MAXD 1375
    #define MAXM 225740
    #define INF 0x3f3f3f3f
    int N, M, first[MAXD], e, next[MAXM], v[MAXM], flow[MAXM];
    int S, T, SS, TT, d[MAXD], q[MAXD], work[MAXD], SUM;
    struct List
    {
        int n, id[110];
    }list[MAXD];
    int low[MAXN][1010];
    void add(int x, int y, int z)
    {
        v[e] = y, flow[e] = z;
        next[e] = first[x], first[x] = e ++;
    }
    void init()
    {
        int i, j, x, G, D, C, high;
        S = 0, T = N + M + 1, SS = T + 1, TT = SS + 1;
        memset(first, -1, sizeof(first[0]) * (TT + 1)), e = 0;
        SUM = 0;
        for(i = 1; i <= M; i ++)
        {
            scanf("%d", &G);
            add(N + i, T, INF - G), add(T, N + i, 0);
            add(SS, T, G), add(T, SS, 0), add(N + i, TT, G), add(TT, N + i, 0);
            SUM += G;
        }
        for(i = 1; i <= N; i ++)
        {
            scanf("%d%d", &C, &D), list[i].n = C;
            add(S, i, D), add(i, S, 0);
            for(j = 0; j < C; j ++)
            {
                scanf("%d", &x), ++ x;
                list[i].id[j] = x;
                scanf("%d%d", &low[i][x], &high);
                add(i, N + x, high - low[i][x]), add(N + x, i, 0);
                add(SS, N + x, low[i][x]), add(N + x, SS, 0), add(i, TT, low[i][x]), add(TT, i, 0);
                SUM += low[i][x];
            }
        }
        add(T, S, INF), add(S, T, 0);
    }
    int bfs(int S, int T)
    {
        int i, j, rear = 0;
        memset(d, -1, sizeof(d[0]) * (TT + 1));
        d[S] = 0, q[rear ++] = S;
        for(i = 0; i < rear; i ++)
            for(j = first[q[i]]; j != -1; j = next[j])
                if(flow[j] && d[v[j]] == -1)
                {
                    d[v[j]] = d[q[i]] + 1, q[rear ++] = v[j];
                    if(v[j] == T) return 1;
                }
        return 0;
    }
    int dfs(int cur, int a, int T)
    {
        if(cur == T) return a;
        for(int &i = work[cur]; i != -1; i = next[i])
            if(flow[i] && d[v[i]] == d[cur] + 1)
                if(int t = dfs(v[i], std::min(a, flow[i]), T))
                {
                    flow[i] -= t, flow[i ^ 1] += t;
                    return t;
                }
        return 0;
    }
    int dinic(int S, int T)
    {
        int ans = 0, t;
        while(bfs(S, T))
        {
            memcpy(work, first, sizeof(first[0]) * (TT + 1));
            while(t = dfs(S, INF, T))
                ans += t;
        }
        return ans;
    }
    void print()
    {
        int i, j, k, ans = 0;
        for(i = first[S]; i != -1; i = next[i]) if(v[i] != T) ans += flow[i ^ 1];
        printf("%d\n", ans);
        for(i = 1; i <= N; i ++)
        {
            for(j = first[i]; j != -1; j = next[j])
                if(v[j] >= N + 1 && v[j] <= N + M)
                    low[i][v[j] - N] += flow[j ^ 1];
            for(j = 0; j < list[i].n; j ++)
                printf("%d\n", low[i][list[i].id[j]]);
        }
    }
    void solve()
    {
        if(SUM != dinic(SS, TT))
            printf("-1\n");
        else
        {
            dinic(S, T);
            print();
        }
    }
    int main()
    {
        while(scanf("%d%d", &N, &M) == 2)
        {
            init();
            solve();
            printf("\n");
        }
        return 0;
    }
  • 相关阅读:
    1
    最大子串
    线段树
    mybatis分页插件
    springmvc下载文件
    获“领跑衫”感言
    finnal 评论 II
    用户使用报告
    事后诸葛亮会议 (尸体解剖)
    final阶段成员贡献分
  • 原文地址:https://www.cnblogs.com/staginner/p/2638898.html
Copyright © 2020-2023  润新知