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

#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;

template <class T> struct AdjMatrix : public vector<vector<T>> {
  AdjMatrix(int n, const vector<T> &v = vector<T>())
      : vector<vector<T>>(n, v) {}
};

template <class T>
T augment(const AdjMatrix<T> &capa, int src, int snk, AdjMatrix<T> &flow) {
  int n = capa.size();
  vector<int> parent(n, -1);
  parent[src] = -2;

  T water = 0;
  queue<pair<T, int>> qu;
  qu.push(make_pair(INF, src));
  while (!qu.empty()) {
    pair<T, int> a = qu.front();
    qu.pop();
    int u = a.second;
    T w = a.first;
    if (u == snk) {
      water = w;
      break;
    }
    rep(v, n) if (parent[v] == -1 && capa[u][v] - flow[u][v] > 0) {
      qu.push(make_pair(min(w, capa[u][v] - flow[u][v]), v));
      parent[v] = u;
    }
  }

  if (water == 0)
    return 0;

  for (int v = snk, u = parent[snk]; v != src; v = u, u = parent[u]) {
    flow[u][v] += water;
    flow[v][u] -= water;
  }

  return water;
}

template <class T> T FordFulkerson(const AdjMatrix<T> &capa, int src, int snk) {
  int n = capa.size();
  AdjMatrix<T> flow(n, vector<T>(n));

  T ans = 0;
  while (1) {
    T water = augment(capa, src, snk, flow);
    if (water == 0)
      break;
    ans += water;
  }
  return ans;
}

int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int ans = 0;
    AdjMatrix<int> adj(n, vector<int>(n));
    rep(i, m) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      if (w < 0)
        ans += w;
      adj[u][v] = adj[v][u] = max(w, 0);
    }

    int ans2 = INF;
    rep(v, n) if (v != 0) ans2 = min(ans2, FordFulkerson(adj, 0, v));
    printf("%d\\n", ans + ans2);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <queue>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 29;

template <class T> struct AdjMatrix : public vector<vector<T>> {
  AdjMatrix(int n, const vector<T> &v = vector<T>())
      : vector<vector<T>>(n, v) {}
};

template <class T>
T augment(const AdjMatrix<T> &capa, int src, int snk, AdjMatrix<T> &flow) {
  int n = capa.size();
  vector<int> parent(n, -1);
  parent[src] = -2;

  T water = 0;
  queue<pair<T, int>> qu;
  qu.push(make_pair(INF, src));
  while (!qu.empty()) {
    pair<T, int> a = qu.front();
    qu.pop();
    int u = a.second;
    T w = a.first;
    if (u == snk) {
      water = w;
      break;
    }
    rep(v, n) if (parent[v] == -1 && capa[u][v] - flow[u][v] > 0) {
      qu.push(make_pair(min(w, capa[u][v] - flow[u][v]), v));
      parent[v] = u;
    }
  }

  if (water == 0)
    return 0;

  for (int v = snk, u = parent[snk]; v != src; v = u, u = parent[u]) {
    flow[u][v] += water;
    flow[v][u] -= water;
  }

  return water;
}

template <class T> T FordFulkerson(const AdjMatrix<T> &capa, int src, int snk) {
  int n = capa.size();
  AdjMatrix<T> flow(n, vector<T>(n));

  T ans = 0;
  while (1) {
    T water = augment(capa, src, snk, flow);
    if (water == 0)
      break;
    ans += water;
  }
  return ans;
}

int main() {
  for (int n, m; scanf("%d%d", &n, &m), n;) {
    int ans = 0;
    AdjMatrix<int> adj(n, vector<int>(n));
    rep(i, m) {
      int u, v, w;
      scanf("%d%d%d", &u, &v, &w);
      if (w < 0)
        ans += w;
      adj[u][v] = adj[v][u] = max(w, 0);
    }

    int ans2 = INF;
    rep(v, n) rep(u, v) ans2 = min(ans2, FordFulkerson(adj, u, v));
    printf("%d\\n", ans + ans2);
  }

  return 0;
}