# BZOJ_1934_[Shoi2007]Vote 善意的投票

BZOJ_1934_[Shoi2007]Vote 善意的投票

3 3
1 0 0
1 2
1 3
3 2

## Sample Output

1

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 100000000
#define S (n+1)
#define T (n+2)
#define N 350
#define M 500050
int Q[N],l,r;
inline void add(int u,int v,int f) {
}
bool bfs() {
int i;
memset(dep,0,sizeof(dep));
dep[S]=1;
l=r=0;
Q[r++]=S;
while(l<r) {
int x=Q[l++];
if(!dep[to[i]]&&flow[i]) {
dep[to[i]]=dep[x]+1;
if(to[i]==T) return 1;
Q[r++]=to[i];
}
}
}
return 0;
}
int dfs(int x,int mf) {
if(x==T) return mf;
int nf=0,i;
if(dep[to[i]]==dep[x]+1&&flow[i]) {
int tmp=dfs(to[i],min(mf-nf,flow[i]));
nf+=tmp;
flow[i]-=tmp;
flow[i^1]+=tmp;
if(nf==mf) break;
}
}
dep[x]=0;
return nf;
}
void dinic() {
int ans=0,f;
while(bfs())while(f=dfs(S,inf))ans+=f;
printf("%d\n",ans);
}
int main() {
scanf("%d%d",&n,&m);
int i,x,y;
for(i=1;i<=n;i++) {
scanf("%d",&x);
}
for(i=1;i<=m;i++) {
scanf("%d%d",&x,&y);
}
dinic();
}


posted @ 2018-04-08 08:00  fcwww  阅读(101)  评论(0编辑  收藏