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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define RESIDUE(s, t) (capacity[s][t] - flow[s][t])

#define INF (1ll << 60)

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

Weight minimumCut(const Graph &g) {
  int n = g.size();
  vector<vector<Weight>> h(n, vector<Weight>(n)); // make adj. matrix
  REP(u, n) FOR(e, g[u]) h[e->src][e->dst] += e->weight;
  vector<int> V(n);
  REP(u, n) V[u] = u;

  Weight cut = INF;
  for (int m = n; m > 1; m--) {
    vector<Weight> ws(m, 0);
    int u, v;
    Weight w;
    REP(k, m) {
      u = v;
      v = max_element(ws.begin(), ws.end()) - ws.begin();
      w = ws[v];
      ws[v] = -1;
      REP(i, m) if (ws[i] >= 0) ws[i] += h[V[v]][V[i]];
    }
    REP(i, m) {
      h[V[i]][V[u]] += h[V[i]][V[v]];
      h[V[u]][V[i]] += h[V[v]][V[i]];
    }
    V.erase(V.begin() + v);
    cut = min(cut, w);
  }
  return cut;
}

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    Graph g(n);
    long long ans = 0;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      if (c <= 0)
        ans += c;
      else
        g[a].push_back(Edge(a, b, c)), g[b].push_back(Edge(b, a, c));
    }

    cout << ans + minimumCut(g) << endl;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <complex>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define RESIDUE(s, t) (capacity[s][t] - flow[s][t])

#define INF (1ll << 60)

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()

typedef long long Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

typedef vector<Weight> Array;
typedef vector<Array> Matrix;

Weight maximumFlow(const Graph &g, int s, int t) {
  int n = g.size();
  Matrix flow(n, Array(n)), capacity(n, Array(n));
  REP(u, n) FOR(e, g[u]) capacity[e->src][e->dst] += e->weight;

  Weight total = 0;
  while (1) {
    queue<int> Q;
    Q.push(s);
    vector<int> prev(n, -1);
    prev[s] = s;
    while (!Q.empty() && prev[t] < 0) {
      int u = Q.front();
      Q.pop();
      FOR(e, g[u]) if (prev[e->dst] < 0 && RESIDUE(u, e->dst) > 0) {
        prev[e->dst] = u;
        Q.push(e->dst);
      }
    }
    if (prev[t] < 0)
      return total; // prev[x] == -1 <=> t-side
    Weight inc = INF;
    for (int j = t; prev[j] != j; j = prev[j])
      inc = min(inc, RESIDUE(prev[j], j));
    for (int j = t; prev[j] != j; j = prev[j])
      flow[prev[j]][j] += inc, flow[j][prev[j]] -= inc;
    total += inc;
  }
}

int main() {
  int n, m;
  while (cin >> n >> m && n) {
    Graph g(n);
    long long ans = 0;
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      if (c <= 0)
        ans += c;
      else
        g[a].push_back(Edge(a, b, c)), g[b].push_back(Edge(b, a, c));
    }

    long long ans2 = INF;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i != j) {
          ans2 = min(ans2, maximumFlow(g, i, j));
        }
      }
    }
    cout << ans + ans2 << endl;
  }
}