QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#535194#9224. Express EvictionwsyearRE 1ms7684kbC++172.6kb2024-08-27 21:34:262024-08-27 21:34:26

Judging History

你现在查看的是最新测评结果

  • [2024-08-27 21:34:26]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:7684kb
  • [2024-08-27 21:34:26]
  • 提交

answer

#include <bits/stdc++.h>

#define rep(i, j, k) for (int i = (j); i <= (k); ++i)
#define per(i, j, k) for (int i = (j); i >= (k); --i)
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(),(v).end()
#define fi first
#define se second
using ll = long long;
using pii = std::pair<int, int>;
using pll = std::pair<ll, ll>;

template<class T>inline void chkmn(T &x, T y) { if (y < x) x = y; }
template<class T>inline void chkmx(T &x, T y) { if (y > x) x = y; }

using namespace std;

struct MF {
  static const int N = 510;
  static const int M = 1000010;
  int s, t, head[N], to[M << 1], nxt[M << 1], flow[M << 1], cur[N], d[N], tot = 1;
  void init() {
    memset(head, 0, sizeof(head));
    tot = 1;
  }
  void addedge(int u, int v, int w) {
    to[++tot] = v, flow[tot] = w, nxt[tot] = head[u], head[u] = tot;
  }
  void add(int u, int v, int w) {
    addedge(u, v, w);
    addedge(v, u, 0);
  }
  bool bfs() {
    queue<int> q;
    memset(d, 0, sizeof(d));
    d[s] = 1, q.push(s);
    while (!q.empty()) {
      int u = q.front();
      q.pop(), cur[u] = head[u];
      for (int i = head[u]; i; i = nxt[i]) {
        int v = to[i], w = flow[i];
        if (w > 0 && !d[v])
          d[v] = d[u] + 1, q.push(v);
      }
    }
    return d[t];
  }
  int dfs(int u, int now) {
    if (u == t || !now) return now;
    int rest = now;
    for (int i = cur[u]; i && rest > 0; i = nxt[i]) {
      cur[u] = i;
      int v = to[i], w = flow[i];
      if (d[v] == d[u] + 1 && w > 0) {
        int cap = dfs(v, min(rest, w));
        if (cap) flow[i] -= cap, flow[i ^ 1] += cap, rest -= cap;
      }
    }
    return now - rest;
  }
  int mf() {
    int res = 0, cap;
    while (bfs()) while (cap = dfs(s, 1e9)) res += cap;
    return res;
  }
} g;

const int maxn = 55;

int n, m, a[maxn][maxn];
string s;

int id(int x, int y) {
  return (x - 1) * m + y;
}

int main() {
  cin.tie(nullptr) -> ios::sync_with_stdio(false);
  cin >> n >> m;
  rep (i, 1, n) {
    cin >> s;
    rep (j, 1, m) a[i][j] = (s[j - 1] == '#');
  }
  int S = 2 * n * m + 1, T = 2 * n * m + 2;
  g.init(), g.s = S, g.t = T;
  rep (i, 1, n) rep (j, 1, m) {
    g.add(id(i, j), n * m + id(i, j), a[i][j]);
    if (i == n || j == 1) g.add(S, id(i, j), 1e9);
    if (i == 1 || j == m) g.add(n * m + id(i, j), T, 1e9);
  }
  rep (i, 1, n) rep (j, 1, m) rep (dx, -2, 2) rep (dy, -2, 2) {
    if (!dx && !dy) continue;
    int tx = i + dx, ty = j + dy;
    if (tx < 1 || tx > n || ty < 1 || ty > m) continue;
    g.add(n * m + id(i, j), id(tx, ty), 1e9);
  }
  cout << g.mf() << '\n';
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7684kb

input:

4 6
.##...
.#....
##....
....#.

output:

1

result:

ok 1 number(s): "1"

Test #2:

score: -100
Runtime Error

input:

20 30
...###########################
#..###########################
...###########################
...###########################
.#############################
...###########################
...###########################
#..###########################
...###########################
..#...............

output:


result: