本文共 1926 字,大约阅读时间需要 6 分钟。
Description
幼儿园里有n个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的人数。
我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?
Input
第一行只有两个整数n,m,保证有2≤n≤300,1≤m≤n(n-1)/2。其中n代表总人数,m代表好朋友的对数。文件第二行有n个整数,第i个整数代表第i个小朋友的意愿,当它为1时表示同意睡觉,当它为0时表示反对睡觉。接下来文件还有m行,每行有两个整数i,j。表示i,j是一对好朋友,我们保证任何两对i,j不会重复。
Output
只需要输出一个整数,即可能的最小冲突数。
Sample Input
3 3
1 0 0 1 2 1 3 3 2
Sample Output
1
HINT
在第一个例子中,所有小朋友都投赞成票就能得到最优解
题解
好显然的最小割啊。。
选1的我们就st->点连一条边 流量为1 选0的我们就点->ed连一条边 流量为1 然后两个朋友之间双向边 流量也都为1 这样的话如果两个朋友在一个集合里,就一定要割掉中间的相当于多出一个冲突。。 不然的话如果要割掉原有的边(相当于违背意愿),答案 也要多一个 真的水啊,今天三道网络流了
#include#include #include #include #include using namespace std;struct node{ int x,y,c,next,other;}a[311000];int len,last[11000];void ins(int x,int y,int c){ int k1,k2; k1=++len; a[len].x=x;a[len].y=y;a[len].c=c; a[len].next=last[x];last[x]=len; k2=++len; a[len].x=y;a[len].y=x;a[len].c=0; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1;}void insx(int x,int y,int c){ int k1,k2; k1=++len; a[len].x=x;a[len].y=y;a[len].c=c; a[len].next=last[x];last[x]=len; k2=++len; a[len].x=y;a[len].y=x;a[len].c=c; a[len].next=last[y];last[y]=len; a[k1].other=k2; a[k2].other=k1;}int list[111000],h[800];int head,tail,st,ed;bool bt_h(){ list[1]=st;head=1;tail=2; memset(h,0,sizeof(h)); h[st]=1; while(head!=tail) { int x=list[head]; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(a[k].c>0 && h[y]==0) { h[y]=h[x]+1; list[tail++]=y; } } head++; } if(h[ed]==0)return false; return true;}int findflow(int x,int f){ if(x==ed)return f; int s=0,t; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(h[y]==h[x]+1 && a[k].c>0 && s
转载地址:http://vfmu.baihongyu.com/