# BZOJ1934 [Shoi2007]Vote 善意的投票 【最小割】

3 3

1 0 0

1 2

1 3

3 2

1

## 题解

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u]; k != -1; k = ed[k].nxt)
using namespace std;
const int maxn = 305,maxm = 300005,INF = 1000000000;
inline int RD(){
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57) {if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57) {out = (out << 1) + (out << 3) + c - '0'; c = getchar();}
return out * flag;
}
int h[maxn],ne = 0,N,M,d[maxn],vis[maxn],cur[maxn],S,T;
struct EDGE{int to,nxt,f;}ed[maxm];
inline void build(int u,int v,int w){
ed[ne] = (EDGE){v,h[u],w}; h[u] = ne++;
ed[ne] = (EDGE){u,h[v],0}; h[v] = ne++;
}
bool bfs(){
queue<int> q;
for (int i = S; i <= T; i++) vis[i] = false,d[i] = INF;
d[S] = 0; q.push(S); int to,u;
while (!q.empty()){
u = q.front(); q.pop();
Redge(u) if (ed[k].f && !vis[to = ed[k].to]){
d[to] = d[u] + 1; vis[to] = true;
q.push(to);
}
}
return vis[T];
}
int dfs(int u,int minf){
if (u == T || !minf) return minf;
int flow = 0,f,to;
if (cur[u] == -2) cur[u] = h[u];
for (int& k = cur[u]; k != -1; k = ed[k].nxt)
if (d[to = ed[k].to] == d[u] + 1 && (f = dfs(to,min(minf,ed[k].f)))){
ed[k].f -= f; ed[k ^ 1].f += f;
flow += f; minf -= f;
if (!minf) break;
}
return flow;
}
int maxflow(){
int flow = 0;
while (bfs()){
fill(cur,cur + maxn,-2);
flow += dfs(S,INF);
}
return flow;
}
int main(){
memset(h,-1,sizeof(h));
N = RD(); M = RD(); S = 0; T = N + 1; int a,b;
REP(i,N) if (RD()) build(S,i,1); else build(i,T,1);
while (M--){
a = RD(); b = RD();
build(a,b,1); build(b,a,1);
}
printf("%d",maxflow());
return 0;
}

posted @ 2017-12-24 14:26  Mychael  阅读(105)  评论(0编辑  收藏