Problem p01903 (C++) — buggy submission, verdict: Runtime Error

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

const int MAX_V = 200;
const int INF = (1 << 29);
struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V];
vector<edge> tmp[MAX_V];
bool used[MAX_V];

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

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (edge &e : G[v]) {
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      // cout << v << " -> " << e.to << " " << d << " : " << e.cap << endl;
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (!f)
      return flow;
    flow += f;
  }
}

int N, V, S, T;
/*
int remove_flow( edge &u, edge &v ){
  u.cap = v.cap = 0;
  int fw = max_flow( v.to, u.to );
  if( fw == 0 ){
    max_flow( T, u.to );
    max_flow( v.to, S );
    if( max_flow( S, T ) ) return 0;
    return -1;
  }
  return 0;
}
*/

int add_flow(edge &u, edge &v, int cap, int map) {
  u.cap = cap;
  v.cap = map;
  int ret = max_flow(S, T);
  return ret;
}

int main() {

  int K, N, M;
  cin >> K >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    add_edge(a, b, c);
  }
  T = 0;
  S = K + N + 1;
  for (int i = 1; i <= K; i++)
    add_edge(S, i, INF);

  int flow = max_flow(S, T);

  for (int i = 0; i <= S; i++)
    tmp[i] = G[i];

  int res = flow;
  for (int i = 0; i <= K + N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      edge &e = G[i][j];
      int map = G[e.to][e.rev].cap;
      int ad = add_flow(e, G[e.to][e.rev], INF, map);
      //      cout << i << " -> " << e.to << " "<< cap << " ad : " << ad <<
      //      endl;
      res = max(res, flow + ad);
      for (int i = 0; i <= S; i++)
        G[i] = tmp[i];
    }
  }
  if (res >= INF)
    cout << "overfuro" << endl;
  else
    cout << res << endl;
}

Error: 0

Fixed submission:

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

const int MAX_V = 200;
const int INF = (1 << 29);
struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  G[from].push_back((edge){to, cap, (int)G[to].size()});
  G[to].push_back((edge){to, cap, (int)G[from].size()});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (edge &e : G[v]) {
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  for (;;) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, INF);
    if (!f)
      return flow;
    flow += f;
  }
}

int once_flow(int s, int t) {
  memset(used, 0, sizeof(used));
  return dfs(s, t, INF);
}

int N, V, S, T;

int remove_flow(edge &u, edge &v) {
  bool f = false;
  if (u.cap == 0)
    f = true;
  u.cap = v.cap = 0;
  if (f) {
    int fw = once_flow(v.to, u.to);
    if (fw == 0) {
      once_flow(T, u.to);
      once_flow(v.to, S);
      if (once_flow(S, T))
        return 0;
      return -1;
    }
    return 0;
  }
  return 0;
}

int add_flow(edge &u, edge &v, int cap) {
  u.cap = cap;
  v.cap = 0;
  int ret = once_flow(S, T);
  return ret;
}

int main() {

  int K, N, M;
  cin >> K >> N >> M;
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    add_edge(a, b, c);
    add_edge(b, a, c);
  }
  T = 0;
  S = K + N + 1;
  for (int i = 1; i <= K; i++)
    add_edge(S, i, INF);

  int flow = max_flow(S, T);
  int res = flow;
  for (int i = 0; i <= K + N; i++) {
    for (edge &e : G[i]) {
      int cap = e.cap;
      int ad = add_flow(e, G[e.to][e.rev], INF);
      // cout << i << " -> " << e.to << " ad : " << ad << endl;
      res = max(res, flow + ad);
      remove_flow(e, G[e.to][e.rev]);
      add_flow(e, G[e.to][e.rev], cap);
    }
  }
  if (res >= INF)
    cout << "overfuro" << endl;
  else
    cout << res << endl;
}