标签:submit problems 双向 ons 直接 之间 范围 font memory
在第一个例子中,所有小朋友都投赞成票就能得到最优解
1 #include "bits/stdc++.h" 2 using namespace std; 3 typedef long long LL; 4 const int MAX=305; 5 const int M=MAX*MAX; 6 int n,m,s,t; 7 int tot,head[MAX],adj[M],wei[M],next[M]; 8 int deep[MAX],cur[MAX]; 9 inline int read(){ 10 int an=0,x=1;char c=getchar(); 11 while (c<‘0‘ || c>‘9‘) {if (c==‘-‘) x=-1;c=getchar();} 12 while (c>=‘0‘ && c<=‘9‘) {an=(an<<3)+(an<<1)+c-‘0‘;c=getchar();} 13 return an*x; 14 } 15 void addedge(int u,int v,int w){ 16 tot++;adj[tot]=v,wei[tot]=w,next[tot]=head[u],head[u]=tot; 17 } 18 bool bfs(){ 19 int i,j,u; 20 memset(deep,127,sizeof(deep)); 21 deep[s]=0; 22 queue <int> q;q.push(s); 23 while (!q.empty()){ 24 u=q.front();q.pop(); 25 for (i=head[u];i;i=next[i]){ 26 if (deep[adj[i]]>1e9 && wei[i]>0) 27 deep[adj[i]]=deep[u]+1,q.push(adj[i]); 28 } 29 } 30 return deep[t]<1e9; 31 } 32 int dfs(int x,int flo){ 33 if (x==t || flo==0) return flo; 34 int j; 35 for (int &i=cur[x];i;i=next[i]){ 36 if (deep[adj[i]]==deep[x]+1 && wei[i]>0){ 37 j=dfs(adj[i],min(flo,wei[i])); 38 if (j){ 39 wei[i]-=j,wei[i^1]+=j; 40 return j; 41 } 42 } 43 } 44 return 0; 45 } 46 int main(){ 47 freopen ("vote.in","r",stdin);freopen ("vote.out","w",stdout); 48 int i,j,u,v; 49 n=read(),m=read();tot=1;s=n+1,t=n+2; 50 for (i=1;i<=n;i++){ 51 u=read(); 52 if (u==1) addedge(s,i,1),addedge(i,s,0); 53 else addedge(i 54 ,t,1),addedge(t,i,0); 55 } 56 for (i=1;i<=m;i++){ 57 u=read();v=read(); 58 addedge(u,v,1),addedge(v,u,1); 59 } 60 int flow=0,dd; 61 while (bfs()){ 62 for (i=1;i<=n+2;i++) cur[i]=head[i]; 63 while (dd=dfs(s,1e9)) flow+=dd; 64 } 65 printf("%d",flow); 66 return 0; 67 }
BZOJ-1934: [Shoi2007]Vote 善意的投票 (网络流最小割)
标签:submit problems 双向 ons 直接 之间 范围 font memory
原文地址:http://www.cnblogs.com/keximeiruguo/p/7749953.html