Problem p01876 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const int INF = 1e9;

struct Edge {
  int to, cap, cost, rev;
  Edge(int to_ = 0, int cap_ = 0, int cost_ = 0, int rev_ = 0)
      : to(to_), cap(cap_), cost(cost_), rev(rev_) {}
};

const int MAX_V = 200 * 200 * 2;
int A, B, K;
int V;
vector<Edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V]; //??´??????????????¨???

void add_edge(int from, int to, int cap, int cost) {
  G[from].push_back(Edge(to, cap, cost, G[to].size()));
  G[to].push_back(Edge(from, 0, -cost, G[from].size() - 1));
}

// ?§????s, ??????t, ?????????f ???????°??????¨?????? ?????¨??????????????°-1
int min_cost_flow(int s, int t) {
  int res = 0, i;
  for (i = 0; i < min(A, B); ++i) {
    fill(dist, dist + V, INF);
    dist[s] = 0;
    bool update = true;
    while (update) {
      update = false;
      for (int v = 0; v < V; ++v) {
        if (dist[v] == INF)
          continue;
        for (int i = 0; i < G[v].size(); ++i) {
          Edge &e = G[v][i];
          if (e.cap > 0 && dist[v] + e.cost < dist[e.to]) {
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            update = true;
          }
        }
      }
    }

    if (dist[t] == INF)
      return 0;

    int d = 1;
    for (int v = t; v != s; v = prevv[v])
      d = min(d, G[prevv[v]][preve[v]].cap);

    res += d * dist[t];
    for (int v = t; v != s; v = prevv[v]) {
      Edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
    if (res > K)
      break;
  }
  return i;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  cin >> A >> B >> K;
  int S = 0, T = A + B + A * B + 1;
  V = A + B + A * B + 2;
  int ans = 0;
  VS as(A), bs(B);
  REP(i, A) cin >> as[i];
  REP(i, B) cin >> bs[i];

  REP(i, MAX_V) G[i].clear();
  FOR(i, 1, A + 1) add_edge(S, i, 1, 0);
  FOR(i, 1, B + 1) add_edge(A + i, T, 1, 0);
  REP(i, A) {
    REP(j, B) {
      int t = A + B + i * B + j + 1;
      add_edge(1 + i, t, 1, as[i][j] - '0');
    }
  }
  REP(j, B) {
    REP(i, A) {
      int t = A + B + i * B + j + 1;
      add_edge(t, A + 1 + j, 1, bs[j][i] - '0');
    }
  }
  int c = min_cost_flow(S, T);

  cout << c << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;
typedef pair<LL, LL> PLL;

#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const int INF = 1e9;

struct Edge {
  int to, cap, cost, rev;
  Edge(int to_ = 0, int cap_ = 0, int cost_ = 0, int rev_ = 0)
      : to(to_), cap(cap_), cost(cost_), rev(rev_) {}
};

const int MAX_V = 200 * 200 * 2;
int V;
vector<Edge> G[MAX_V];
int dist[MAX_V];
int prevv[MAX_V], preve[MAX_V]; //??´??????????????¨???

void add_edge(int from, int to, int cap, int cost) {
  G[from].push_back(Edge(to, cap, cost, G[to].size()));
  G[to].push_back(Edge(from, 0, -cost, G[from].size() - 1));
}

// ?§????s, ??????t, ?????????f ???????°??????¨?????? ?????¨??????????????°-1
int min_cost_flow(int s, int t, int f) {
  int res = 0;
  while (f > 0) {
    fill(dist, dist + V, INF);
    dist[s] = 0;
    bool update = true;
    while (update) {
      update = false;
      for (int v = 0; v < V; ++v) {
        if (dist[v] == INF)
          continue;
        for (int i = 0; i < G[v].size(); ++i) {
          Edge &e = G[v][i];
          if (e.cap > 0 && dist[v] + e.cost < dist[e.to]) {
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            update = true;
          }
        }
      }
    }

    if (dist[t] == INF)
      return -1;

    int d = f;
    for (int v = t; v != s; v = prevv[v])
      d = min(d, G[prevv[v]][preve[v]].cap);

    f -= d;
    res += d * dist[t];
    for (int v = t; v != s; v = prevv[v]) {
      Edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }

  return res;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int A, B, K;
  cin >> A >> B >> K;
  int S = 0, T = A + B + A * B + 1;
  V = A + B + A * B + 2;
  int ans = 0;
  VS as(A), bs(B);
  REP(i, A) cin >> as[i];
  REP(i, B) cin >> bs[i];

  FOR(f, 1, A + 1) {
    REP(i, MAX_V) G[i].clear();
    FOR(i, 1, A + 1) add_edge(S, i, 1, 0);
    FOR(i, 1, B + 1) add_edge(A + i, T, 1, 0);
    REP(i, A) {
      REP(j, B) {
        int t = A + B + i * B + j + 1;
        add_edge(1 + i, t, 1, as[i][j] - '0');
      }
    }
    REP(j, B) {
      string s;
      cin >> s;
      REP(i, A) {
        int t = A + B + i * B + j + 1;
        add_edge(t, A + 1 + j, 1, bs[j][i] - '0');
      }
    }
    int c = min_cost_flow(S, T, f);
    if (c < 0 || c > K)
      break;
    ans = f;
  }
  cout << ans << endl;
  return 0;
}