BZOJ 1934

1934: [Shoi2007]Vote 善意的投票

再此用来验证dinic板子

BZOJ 1934


/**************************************************************
    Problem: 1934
    User: Dream_Tonight
    Language: C++
    Result: Accepted
    Time:44 ms
    Memory:3640 kb
****************************************************************/
 
#include <algorithm>
#include <iostream>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <iostream>
#include <cstdio>
 
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 6e6 + 7;
const int inf = 0x7f7f7f7f;
int deep[310], head[310], cnt = 2, cur[310];
int s, t;
struct node {
    int to, value, next;
} edge[200010];
 
inline void add(int u, int v, int w) {
    edge[cnt].to = v;
    edge[cnt].value = w;
    edge[cnt].next = head[u];
    head[u] = cnt++;
}
 
inline void addEdge(int u, int v, int w) {
    add(u, v, w);
    add(v, u, w);
}
 
//分层图
bool bfs() {
    queue<int> q;
    memset(deep, 0, sizeof(deep));
    memcpy(cur, head, sizeof(head));
    deep[s] = 1;
    q.push(s);
    while (!q.empty()) {
        int x = q.front();
        if (x == t) break; //优化
        q.pop();
        for (int i = head[x]; i != -1; i = edge[i].next) {
            if (edge[i].value && !deep[edge[i].to]) {
                deep[edge[i].to] = deep[x] + 1;
                q.push(edge[i].to);
            }
        }
    }
    return deep[t];
}
 
//增广路
int dfs(int x, int limit) {
    if (x == t) return limit;
    int flow = 0;
    for (int i = cur[x]; i != -1; i = edge[i].next) {
        cur[x] = i; //当前弧优化
        if (deep[x] == deep[edge[i].to] - 1 && edge[i].value) {
            if (int f = dfs(edge[i].to, min(edge[i].value, limit))) {
                edge[i].value -= f;
                edge[i ^ 1].value += f;
                flow += f;
                limit -= f;
                if (!limit) break;
            }
        }
    }
    return flow;
}
 
int dinic() {
    int result = 0;
    while (bfs()) {
        while (int deep = dfs(s, inf)) {
            result += deep;
        }
    }
    return result;
}
 
int main() {
    int n, m, x, y;
    scanf("%d%d", &n, &m);
    memset(head, -1, sizeof(head));
    s = n + 1, t = n + 2;
    for (int i = 1; i <= n; i++) {
        scanf("%d", &x);
        if (x)add(s, i, 1), add(i, s, 0);
        else add(i, t, 1), add(t, i, 0);
    }
    for (int i = 1; i <= m; i++) {
        scanf("%d%d", &x, &y);
        addEdge(x, y, 1);
    }
    printf("%d\n", dinic());
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值