题意:
有n个小朋友投票,1表示赞成0表示不赞成。有m对朋友关系。
现在给定n个小朋友的本来意愿。
定义冲突数为和自己本来意愿不同的投票数量,加上投不同票的朋友对数。
求最小的冲突数。
做法:
这道题一眼看就是网络流最小割。
然后想了很长时间没想出怎么建图qaq。。看来是要加强练习了呜呜
对于每一个小朋友x,本来是1的连S->x,本来是0的连x->T。
每一对朋友关系连x->y和y->x双向边。
流量都是1.
可以这么理解:
对于S->x或x->T的边,如果存在最小割里,说明它改变本来的意愿;
对于朋友x->y或y->x,如果存在最小割里,说明朋友之间到最后还有冲突。
代码:
/*************************************************************
Problem: bzoj 1934 [Shoi2007]Vote 善意的投票
User: fengyuan
Language: C++
Result: Accepted
Time: 36 ms
Memory: 2472 kb
Submit_Time: 2018-01-17 15:24:32
*************************************************************/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#include<cctype>
#include<queue>
using namespace std;
const int N = 305, M = 100010;
const int inf = 1e9;
int n, m, cnt, ans, S, T;
int head[N], vis[N];
struct edge{
int to, nxt, c;
edge() {}
edge(int x, int y, int z) { to = x, nxt = y, c = z; }
}e[M];
inline void addedge(int x, int y, int z) { e[cnt] = edge(y, head[x], z); head[x] = cnt ++; }
inline bool bfs()
{
queue<int> q; q.push(S); memset(vis, -1, sizeof vis); vis[S] = 1;
while(!q.empty()) {
int u = q.front(); q.pop();
for(int i = head[u]; i != -1; i = e[i].nxt) {
int v = e[i].to;
if(vis[v] == -1 && e[i].c) {
vis[v] = vis[u] + 1;
q.push(v);
}
}
}
return vis[T] != -1;
}
inline int dfs(int u, int flow)
{
if(u == T) return flow;
int used = 0, w;
for(int i = head[u]; i != -1; i = e[i].nxt) {
int v = e[i].to;
if(vis[v] == vis[u]+1 && e[i].c) {
w = dfs(v, min(flow-used, e[i].c));
e[i].c -= w; e[i^1].c += w; used += w;
if(used == flow) return used;
}
}
if(!used) vis[u] = -1;
return used;
}
inline void dinic() { while(bfs()) ans += dfs(S, inf); }
int main()
{
scanf("%d%d", &n, &m); S = 0, T = n+1;
memset(head, -1, sizeof head);
for(int i = 1; i <= n; i ++) {
int x; scanf("%d", &x);
if(x) addedge(S, i, 1), addedge(i, S, 0);
else addedge(i, T, 1), addedge(T, i, 0);
}
for(int i = 1; i <= m; i ++) {
int x, y; scanf("%d%d", &x, &y);
addedge(x, y, 1); addedge(y, x, 1);
}
dinic();
printf("%d\n", ans);
return 0;
}