• [BZOJ1934][Shoi2007]Vote 善意的投票[最小割]


    建图方式:
    S->同意 ,反对->T

    对于每一对好友连容量为1的边

    #include <bits/stdc++.h>
    using namespace std;
    const int inf = 1e9;
    const int MAXN = 3e2+7;
    const int MAXM = 2e5+7;
    int n, m, s, t, dep[MAXN], maxflow;
    struct Edge {
        int v, w, next;
    } G[MAXM];
    int tot = 1, head[MAXN], cur[MAXN];
    inline void add(int u, int v, int w) {
        G[++tot].v=v, G[tot].w=w, G[tot].next=head[u];
        head[u]=tot;
    }
     
    bool bfs(int s, int t) {
        memset(dep, 0x7f, sizeof dep);
        memcpy(cur+1, head+1, n*4+8);
        queue<int>q;
        while(!q.empty()) q.pop();
        dep[s] = 0;
        q.push(s);
        while(!q.empty()) {
            int u = q.front();
            q.pop();
            for(int i = head[u]; i; i = G[i].next) {
                int v = G[i].v, w = G[i].w;
                if (dep[v] > inf && w) {
                    dep[v] = dep[u] + 1;
                    if (v == t) return 1;
                    q.push(v);
                }
            }
        }
        return dep[t] < inf;
    }
     
    int dfs(int u, int t, int limit) {
        if (u == t || !limit) return limit;
        int flow = 0, f;
        for(int i = cur[u]; i; i = G[i].next) {
            cur[u] = i;
            int v = G[i].v, w = G[i].w;
            if (dep[v] == dep[u] + 1 && (f = dfs(v, t, min(w, limit)))) {
                flow += f;
                limit -= f;
                G[i].w -= f;
                G[i^1].w += f;
                if (!limit) break;
            }
        }
        return flow;
    }
     
    void dinic(int s, int t) {
        while(bfs(s, t)) maxflow += dfs(s, t, inf);
    }
     
    int main(void) {
        // memset(head, -1, sizeof head);
        scanf("%d%d", &n, &m);
        for(int i = 1; i <= n; ++i) {
            scanf("%d", &s);
            if (s) add(n+1, i, 1), add(i, n+1, 0);
            else add(i, n+2, 1), add(n+2, i, 0);
        }
        for(int i = 1; i <= m; ++i) {
            int u, v;
            scanf("%d%d", &u, &v);
            add(u, v, 1), add(v, u, 1);
    //      add(u, v, 0), add(v, u, 0);//加不加都行,因为不会退流
        }
        dinic(n+1, n+2);
        printf("%d", maxflow);
        return 0;
    }
    
  • 相关阅读:
    你现在的技能,是不是只剩下给别人点赞了?
    web编码(转)
    Android 官方文档:(二)应用清单 —— 2.26 &lt;uses-permission&gt;标签
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
    开源 java CMS
    EM算法原理
    OpenCV2马拉松第14圈——边缘检測(Sobel,prewitt,roberts)
    winzip15.0注冊码
    什么是积分墙?
    Bitmap工具类
  • 原文地址:https://www.cnblogs.com/storz/p/10191524.html
Copyright © 2020-2023  润新知