## bzoj 1934 : [Shoi2007]Vote 善意的投票 最小割

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 2e5+5;
int q[maxn*2], head[maxn*2], dis[maxn/10], s, t, num;
struct node
{
int to, nextt, c;
node(){}
node(int to, int nextt, int c):to(to), nextt(nextt), c(c){}
}e[maxn*2];
void init() {
num = 0;
}
void add(int u, int v, int c) {
}
int bfs() {
mem(dis);
dis[s] = 1;
int st = 0, ed = 0;
q[ed++] = s;
while(st<ed) {
int u = q[st++];
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(!dis[v]&&e[i].c) {
dis[v] = dis[u]+1;
if(v == t)
return 1;
q[ed++] = v;
}
}
}
return 0;
}
int dfs(int u, int limit) {
if(u == t) {
return limit;
}
int cost = 0;
for(int i = head[u]; ~i; i = e[i].nextt) {
int v = e[i].to;
if(e[i].c&&dis[v] == dis[u]+1) {
int tmp = dfs(v, min(limit-cost, e[i].c));
if(tmp>0) {
e[i].c -= tmp;
e[i^1].c += tmp;
cost += tmp;
if(cost == limit)
break;
} else {
dis[v] = -1;
}
}
}
return cost;
}
int dinic() {
int ans = 0;
while(bfs()) {
ans += dfs(s, inf);
}
return ans;
}
int main()
{
int n, m, a, b, x;
cin>>n>>m;
init();
s = 0, t = n+1;
for(int i = 1; i<=n; i++) {
scanf("%d", &x);
if(x) {
} else {
}
}
while(m--) {
scanf("%d%d", &a, &b);
}
cout<<dinic()<<endl;
return 0;
}

posted on 2016-02-22 15:27  yohaha  阅读(85)  评论(0编辑  收藏

• 随笔 - 305
• 文章 - 0
• 评论 - 6
• 引用 - 0