bzoj 1934: [Shoi2007]Vote 善意的投票

题目链接

bzoj 1934: [Shoi2007]Vote 善意的投票

代码


#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10007;
#define INF 0x7fffffff
using std::queue;
inline int read() {
int x=0;
char c=getchar();
while(c<'0'||c>'9') c=getchar();
while(c<='9'&&c>='0')x=x*10+c-'0',c=getchar();
return x;
}
int n,m,num=0,S,T;
int bel[maxn];
struct node{
int v,next,flow;
}edge[maxn<<4];
void add_edge(int u,int v,int flow) {
}
int cur[maxn],lev[maxn];
bool bfs() {
memset(lev,-1,sizeof lev);
queue<int>que;
que.push(S);lev[S]=0;
while(!que.empty()) {
int u=que.front();que.pop();
int v=edge[i].v;
if(edge[i].flow>0&&lev[v]<0) {
lev[v]=lev[u]+1;que.push(v);
}
}
}
if(lev[T]!=-1)return true;
else return false;
}
int dfs(int now,int flow) {
if(now==T)return flow;
int rest=0,delta;
for(int &i=cur[now];i;i=edge[i].next) {
int v=edge[i].v;
if(lev[v]==lev[now]+1&&edge[i].flow>0) {
delta=dfs(v,min(flow-rest,edge[i].flow));
if(delta) {
edge[i].flow-=delta;
edge[i^1].flow+=delta;
rest+=delta;if(rest==flow)break;
}
}
}
if(rest==flow)lev[now]=-1;
return rest;
}
int dinic() {
int ans=0;
while(bfs())
ans+=dfs(S,INF);
return ans;
}
int main() {
T=n+1;
for(int p,i=1;i<=n;++i) {