【BZOJ4435】【Cerc2015】Juice Junctions

【题目链接】

【思路要点】

  • 建出GomoryHu-Tree ,剩余部分用DFS即可解决。
  • 时间复杂度 O(NDinic(N,M)+N2) O ( N ∗ D i n i c ( N , M ) + N 2 ) ,由于最大流至多为3,因此 Dinic(N,M)=O(N+M) D i n i c ( N , M ) = O ( N + M )
  • 时限较紧,需要一定的常数优化。

【代码】


#include<bits/stdc++.h>

using namespace std;

#define MAXN  3005


#define MAXM  9005


#define INF   1e9

template <typename T> void read(T &x) {
  x = 0; int f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
  for (; isdigit(c); c = getchar()) x = x * 10 + c - '0';
  x *= f;
}
struct edge {int dest, flow, nxt, home; };
struct treeedge {int dest, index, nxt; };
int n, m, s, t, ans;
int dist[MAXN], p[MAXN], q[MAXN], curr[MAXN];
int heada[MAXN], tota, headb[MAXN], totb;
edge a[MAXM]; treeedge b[MAXM];
bool bfs() {
  memset(dist, 0, sizeof(dist));
  static int q[MAXN];
  int l = 0, r = 0;
  dist[s] = 1, q[0] = s;
  while (l <= r) {
      int tmp = q[l++];
      for (unsigned i = heada[tmp]; i; i = a[i].nxt)
          if (a[i].flow != 0 && dist[a[i].dest] == 0) {
              dist[a[i].dest] = dist[tmp] + 1;
              q[++r] = a[i].dest;
          }
  }
  return dist[t] != 0;
}
int dinic(int pos, int limit) {
  if (pos == t) return limit;
  int used = 0, tmp;
  for (int &i = curr[pos]; i; i = a[i].nxt)
      if (a[i].flow != 0 && dist[pos] + 1 == dist[a[i].dest] && (tmp = dinic(a[i].dest, min(limit - used, a[i].flow)))) {
          used += tmp;
          a[i].flow -= tmp;
          a[a[i].home].flow += tmp;
          if (used == limit) return limit;
      }
  return used;
}
void addedge(int s, int t, int flow) {
  a[++tota] = (edge) {t, flow, heada[s], tota + 1};
  heada[s] = tota;
  a[++tota] = (edge) {s, flow, heada[t], tota - 1};
  heada[t] = tota;
}
void ClearGraph() {
  for (int i = 1; i <= tota; i++)
      a[i].flow = a[a[i].home].flow = (a[i].flow + a[a[i].home].flow) / 2;
}
void solve(int l, int r) {
  if (l == r) return;
  s = p[l], t = p[r];
  int flow = 0;
  ClearGraph();
  while (bfs()) {
      for (int i = 1; i <= n; i++)
          curr[i] = heada[i];
      flow += dinic(s, INF);
  }
  b[++totb] = (treeedge) {t, flow, headb[s]};
  headb[s] = totb;
  b[++totb] = (treeedge) {s, flow, headb[t]};
  headb[t] = totb;
  int tl = l - 1, tr = r + 1;
  for (int i = l; i <= r; i++)
      if (dist[p[i]]) q[++tl] = p[i];
      else q[--tr] = p[i];
  for (int i = l; i <= r; i++)
      p[i] = q[i];
  solve(l, tl); solve(tr, r);
}
void work(int pos, int fa, int value) {
  if (value != INF) ans += value;
  for (unsigned i = headb[pos]; i; i = b[i].nxt)
      if (b[i].dest != fa) work(b[i].dest, pos, min(value, b[i].index));
}
int main() {
  read(n), read(m);
  for (int i = 1; i <= m; i++) {
      int x, y;
      read(x), read(y);
      if (x != y) addedge(x, y, 1);
  }
  for (int i = 1; i <= n; i++)
      p[i] = i;
  solve(1, n);
  ans = 0;
  for (int i = 1; i <= n; i++)
      work(i, 0, INF);
  cout << ans / 2 << endl;
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值