【SHOI 2007】善意的投票

Problem

Description

幼儿园里有 \(n\) 个小朋友打算通过投票来决定睡不睡午觉。对他们来说,这个问题并不是很重要,于是他们决定发扬谦让精神。虽然每个人都有自己的主见,但是为了照顾一下自己朋友的想法,他们也可以投和自己本来意愿相反的票。我们定义一次投票的冲突数为好朋友之间发生冲突的总数加上和所有和自己本来意愿发生冲突的人数。

我们的问题就是,每位小朋友应该怎样投票,才能使冲突数最小?

Input Format

文件的第一行只有两个整数 \(n,m\),保证有 \(2\le n\le 300,1\le m\le \frac{n(n-1)}{2}\)。其中 \(n\) 代表总人数,m代表好朋友的对数。文件第二行有 \(n\) 个整数,第 \(i\) 个整数代表第 \(i\) 个小朋友的意愿,当它为 \(1\) 时表示同意睡觉,当它为 \(0\) 时表示反对睡觉。接下来文件还有 \(m\) 行,每行有两个整数 \(i\)\(j\) 。表示 \(i\)\(j\) 是一对好朋友,我们保证任何两对 \(i\)\(j\) 不会重复。

Output Format

只需要输出一个整数,即可能的最小冲突数。

Sample

Input 1

3 3
1 0 0
1 2
1 3
3 2

Output 1

1

Range

\(2\le n\le 300, 1\le m\le n(n - 1) / 2\)

Algorithm

网络流

Mentality

先建立一个源点 \(S\) 和汇点 \(T\) ,然后将所有同意睡觉的人和 \(S\) 连边,否则和 \(T\) 连边,容量设为 \(1\) ,再将每对朋友之间连上一条容量为 \(1\) 的边。

若割去一条边的代价为 \(1\) ,不难发现题目所求其实际上就是使 \(S\)\(T\) 之间不连通的最小代价,因为若 \(S\)\(T\) 仍联通,说明它们之间仍有朋友的冲突没有被解决或是被计算。

若断去一个人与其朋友之间的边,就代表他没有违背自己的意愿,而若他断掉与自己所连的源、汇点之间的边,则代表他向朋友妥协了。

跟据最大流等于最小割,直接在建出的图上跑最大流即可。

Code

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
#define LL long long
#define go(x, i, v) for (int i = hd[x], v = to[i]; i; v = to[i = nx[i]])
#define inline __inline__ __attribute__((always_inline))
inline LL read() {
  LL x = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = (x << 3) + (x << 1) + ch - '0';
    ch = getchar();
  }
  return x * w;
}
const int Max_n = 305, Max_m = Max_n * Max_n << 1, inf = 1e9;
int n, m, S, T, ans;
int cntr = 1, hd[Max_n], cur[Max_n], nx[Max_m], to[Max_m], w[Max_m];
int dep[Max_n], fnow[Max_n], flow[Max_n];
void addr(int u, int v, int W) {
  cntr++;
  nx[cntr] = hd[u], to[cntr] = v, w[cntr] = W;
  hd[u] = cntr;
}
queue<int> q;
bool build() {
  for (int i = 1; i <= n + 2; i++) cur[i] = hd[i], dep[i] = -1, fnow[i] = 0;
  q.push(S), dep[S] = 0, fnow[S] = 1e9;
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    go(x, i, v) if (dep[v] == -1 && w[i]) dep[v] = dep[x] + 1, q.push(v);
  }
  return dep[T] != -1;
}
void dfs(int x) {
  if (x == T) {
    flow[x] = fnow[x], ans += flow[x];
    return;
  }
  for (int i = cur[x], v = to[i]; i; v = to[i = nx[i]])
    if (dep[v] == dep[x] + 1 && w[i]) {
      cur[x] = i, fnow[v] = min(fnow[x], w[i]), dfs(v);
      w[i] -= flow[v], fnow[x] -= flow[v];
      w[i ^ 1] += flow[v], flow[x] += flow[v];
      flow[v] = 0;
    }
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("2057.in", "r", stdin);
  freopen("2057.out", "w", stdout);
#endif
  n = read(), m = read(), S = n + 1, T = n + 2;
  for (int i = 1; i <= n; i++)
    if (!read())
      addr(S, i, 1), addr(i, S, 0);
    else
      addr(i, T, 1), addr(T, i, 0);
  int u, v;
  while (m--) {
    u = read(), v = read();
    addr(u, v, 1), addr(v, u, 0);
    addr(u, v, 0), addr(v, u, 1);
  }
  while (build()) dfs(S);
  cout << ans;
}
posted @ 2019-10-06 20:17  洛水·锦依卫  阅读(135)  评论(0编辑  收藏  举报