bzoj1934[Shoi2007]Vote 善意的投票

bzoj1934[Shoi2007]Vote 善意的投票

题意:

n个小朋友通过投票来决定睡不睡午觉。每个人都有自己的主见,但也可以投和自己本来意愿相反的票。冲突总数为好朋友之间发生冲突的总数加上和自己本来意愿发生冲突的人数。求最小冲突数。

题解:

最小割,s向每个选1的人连边流量为1,每个选0的人连边流量为1。好朋友之间连流量为1的双向边。反思:理解错题意……以为冲突总数是每个人投票的冲突数之和,每个人投票冲突数是与朋友之间发生冲突的总数加上和这个人本来意愿发生冲突的人数。脑洞好大QAQ

代码:

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 #include <queue>
 5 #define maxn 500
 6 #define inc(i,j,k) for(int i=j;i<=k;i++)
 7 #define INF 0x3fffffff
 8 using namespace std;
 9 
10 struct e{int t,c,n;}; e es[maxn*2000]; int g[maxn],ess;
11 inline void pe(int f,int t,int c){
12     es[++ess]=(e){t,c,g[f]}; g[f]=ess; es[++ess]=(e){f,0,g[t]}; g[t]=ess;
13 }
14 inline void pe2(int f,int t,int c){
15     es[++ess]=(e){t,c,g[f]}; g[f]=ess; es[++ess]=(e){f,c,g[t]}; g[t]=ess;
16 }
17 inline void init(){
18     ess=-1; memset(g,-1,sizeof(g));
19 }
20 queue <int> q; int h[maxn];
21 bool bfs(int s,int t){
22     memset(h,-1,sizeof(h)); while(!q.empty())q.pop(); h[s]=0; q.push(s);
23     while(! q.empty()){
24         int x=q.front(); q.pop();
25         for(int i=g[x];i!=-1;i=es[i].n)if(es[i].c&&h[es[i].t]==-1)h[es[i].t]=h[x]+1,q.push(es[i].t);
26     }
27     return h[t]!=-1;
28 }
29 int dfs(int x,int t,int f){
30     if(x==t)return f; int u=0;
31     for(int i=g[x];i!=-1;i=es[i].n)if(es[i].c&&h[es[i].t]==h[x]+1){
32         int w=dfs(es[i].t,t,min(f,es[i].c)); f-=w; u+=w; es[i].c-=w; es[i^1].c+=w; if(f==0)return u;
33     }
34     if(u==0)h[x]=-1; return u;
35 }
36 int dinic(int s,int t){
37     int f=0; while(bfs(s,t))f+=dfs(s,t,INF); return f;
38 }
39 inline int read(){
40     char ch=getchar(); int f=1,x=0;
41     while(ch<'0'||ch>'9'){if(ch=='-')f=-1; ch=getchar();} while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
42     return x*f;
43 }
44 int n,m,s,t;
45 int main(){
46     n=read(); m=read(); s=0; t=n+1; init(); inc(i,1,n){int x=read(); if(x)pe(s,i,1);else pe(i,t,1);}
47     inc(i,1,m){int x=read(),y=read(); pe2(x,y,1);} printf("%d",dinic(s,t)); return 0;
48 }

 

20160603

posted @ 2016-07-20 19:53  YuanZiming  阅读(402)  评论(0编辑  收藏  举报