1934: [Shoi2007]Vote 善意的投票

1934: [Shoi2007]Vote 善意的投票

Time Limit: 1 Sec   Memory Limit: 64 MB
Submit: 1920   Solved: 1189
[ Submit][ Status][ Discuss]

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

在第一个例子中,所有小朋友都投赞成票就能得到最优解

Source

[ Submit][ Status][ Discuss]

等价于将所有小朋友划分到两个集合,使得冲突数最少,联想到最小割
如果小朋友i投0,那么连边(s,i,1),代表如果违背i的意愿要1代价
如果小朋友i投1,那么连边(i,t,1),代表如果违背i的意愿要1代价...
如果i,j是好朋友,连边(i,j,1)(j,i,1)代表如果拆开好朋友要支付代价
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;

const int maxn = 333;
const int maxm = 2*maxn*maxn;
const int INF = ~0U>>1;

struct E{
	int to,cap,flow; E(){}
	E(int to,int cap,int flow): to(to),cap(cap),flow(flow){}
}edgs[maxm];

int n,m,cnt,tot,s,t,cur[maxn],L[maxn];
bool vis[maxn];

vector <int> v[maxn];
queue <int> Q;

void Add(int x,int y,int cap)
{
	v[x].push_back(cnt); edgs[cnt++] = E(y,cap,0);
	v[y].push_back(cnt); edgs[cnt++] = E(x,0,0);
}

bool BFS()
{
	for (int i = 1; i <= t; i++) L[i] = 0,vis[i] = 0;
	Q.push(s); vis[s] = 1; L[s] = 1;
	while (!Q.empty())
	{
		int k = Q.front(); Q.pop();
		for (int i = 0; i < v[k].size(); i++)
		{
			E e = edgs[v[k][i]];
			if (e.cap == e.flow) continue;
			if (vis[e.to]) continue;
			vis[e.to] = 1; L[e.to] = L[k] + 1; Q.push(e.to);
		}
	}
	return vis[t] == 1;
}

int Dinic(int x,int a)
{
	if (x == t) return a;
	int flow = 0;
	for (int &i = cur[x]; i < v[x].size(); i++)
	{
		E &e = edgs[v[x][i]];
		if (e.cap == e.flow) continue;
		if (L[e.to] != L[x] + 1) continue;
		int f = Dinic(e.to,min(a,e.cap - e.flow));
		if (!f) continue;
		e.flow += f;
		edgs[v[x][i]^1].flow -= f;
		flow += f;
		a -= f;
		if (!a) return flow;
	}
	if (!flow) L[x] = 0;
	return flow;
}

int main()
{
	#ifdef DMC
		freopen("DMC.txt","r",stdin);
	#endif
	
	cin >> n >> m; s = n + 1; t = n + 2;
	for (int i = 1; i <= n; i++)
	{
		int x; scanf("%d",&x);
		if (x == 1) Add(i,t,1);
		else Add(s,i,1);
	}
	while (m--)
	{
		int x,y; scanf("%d%d",&x,&y);
		Add(x,y,1); Add(y,x,1);
	}
	
	int MaxFlow = 0;
	while (BFS())
	{
		for (int i = 1; i <= t; i++) cur[i] = 0;
		MaxFlow += Dinic(s,INF);
	}
	cout << MaxFlow;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值