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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define SZ(v) ((int)(v).size())
const int maxint = -1u >> 1;
const int maxn = 100 + 30;
typedef long long ll;

int n, m;
ll adj[maxn][maxn];
int f[maxn], bestf[maxn];
int find(int v) {
  if (v != bestf[v]) {
    return bestf[v] = find(bestf[v]);
  }
  return v;
}
int bestff;
int v[maxn];
ll w[maxn];
bool a[maxn];
vector<pair<int, int>> res;
ll mincut() {
  ll ans = (-1ull) >> 1;
  // for (int i = 0; i < n; ++i) {
  // for (int j = 0; j < n; ++j) {
  // adj[i][j] = -adj[i][j];
  //}
  //}
  for (int i = 0; i < n; ++i) {
    f[i] = v[i] = i;
  }
  for (int t = n; t > 1; --t) {
    a[0] = true;
    for (int i = 1; i < t; ++i) {
      a[i] = false;
      w[i] = adj[v[0]][v[i]];
    }
    int prev = v[0];
    for (int i = 1; i < t; ++i) {
      int zj = -1;
      for (int j = 1; j < t; ++j) {
        if (a[j] == false && (zj == -1 || w[j] > w[zj])) {
          zj = j;
        }
      }
      a[zj] = true;
      if (i == t - 1) {
        if (w[zj] < ans) {
          ans = w[zj];
          copy(f, f + n, bestf);
          bestff = v[zj];
        }
        for (int k = 0; k < t; ++k) {
          adj[v[k]][prev] = adj[prev][v[k]] += adj[v[zj]][v[k]];
        }
        f[v[zj]] = prev;
        v[zj] = v[t - 1];
        break;
      }
      prev = v[zj];
      for (int j = 1; j < t; ++j) {
        if (a[j] == false) {
          w[j] += adj[v[zj]][v[j]];
        }
      }
    }
  }
  // bool flag[maxn] = {};
  // for (int i = 0; i < n; ++i) {
  // if (find(i) == bestff) {
  // flag[i] = true;
  //}
  //}
  // res.clear();
  // for (int i = 0; i < n; ++i) {
  // for (int j = i + 1; j < n; ++j) {
  // if (flag[i] == flag[j] && save[i][j]) {
  // res.push_back(make_pair(i, j));
  //}
  //}
  //}
  return ans;
}

void gao() {
  ll ans = 0;
  memset(adj, 0, sizeof(adj));
  for (int i = 0, a, b; i < m; ++i) {
    ll c;
    scanf("%d%d%lld", &a, &b, &c);
    if (c < 0) {
      ans += c;
    } else {
      adj[a][b] = adj[b][a] = adj[a][b] + c;
    }
  }
  printf("%lld\\n", ans + mincut());
}

int main() {
  while (scanf("%d%d", &n, &m) == 2 && (n || m)) {
    gao();
  }
  return 0;
}

Error: TLE

Fixed submission:

// poj 3469 2987  å¿«äº
//////////èç¹å·ã1ï¼nã,source,sink
///ä¸ºå¶ä¸­ä»»æç¹
#include <cstdio>
#include <cstring>
#define maxn 132
int inf;
using namespace std;
struct edge {
  int u, v, next, f, pre;
} e[100000];
int num, rnum;
int n, m, N;
int source, sink;
int head[maxn], rhead[maxn], start[maxn];
int d[maxn], numb[maxn];
int p[maxn];
void Init() {
  memset(head, -1, sizeof(head)); /// åé¾è¡¨å¿é¡»ç½® -1
  memset(rhead, -1, sizeof(rhead));
  memset(p, -1, sizeof(p));
  num = 0; /// å¿é¡»ä¸º0
}
void BFS() {
  int i, j;
  for (i = 1; i <= N; i++) //////////èç¹å·ã1ï¼nã
  {
    d[i] = N; /// åå§åæ¯ä¸ªèç¹çè·ç¦»ä¸ºæ ç©·å¤§
              /// nå³å¯
    numb[i] = 0;
  }
  int Q[maxn], head(0), tail(0);
  d[sink] = 0; // æsinkæ¾å¥éå
  numb[0] = 1;
  Q[++tail] = sink;
  while (head < tail) {
    i = Q[++head];
    for (j = rhead[i]; j != -1; j = e[j].pre) {
      if (e[j].f == 0 || d[e[j].u] < N)
        continue; /// ä»¥åå°±å°è¾¾è¿æèæ²¡æå¯æµéäº
      d[e[j].u] = d[i] + 1;
      numb[d[e[j].u]]++;
      Q[++tail] = e[j].u;
    }
  }
}
int Augment() {
  int i;
  int tmp = inf;
  for (i = p[sink]; i != -1; i = p[e[i].u]) {
    if (tmp > e[i].f)
      tmp = e[i].f;
  }
  for (i = p[sink]; i != -1; i = p[e[i].u]) {
    e[i].f -= tmp; ////æ­£è¾¹ï¼åè¾¹ç¸ç»§çï¼ä¸­é´å·®
                   ///1
    e[i ^ 1].f += tmp;
  }
  return tmp;
}
int Retreat(int &i) {
  int tmp, j, mind(N - 1);
  for (j = head[i]; j != -1;
       j = e[j].next) ////æ¾æå°çè·¯å¾æ å·
  {
    if (e[j].f > 0 && d[e[j].v] < mind)
      mind = d[e[j].v];
  }
  tmp = d[i]; /// ä¿®æ¹ç¸åº
              /// è·¯å¾æ å·çä¸ªæ°
  d[i] = mind + 1;
  numb[tmp]--;
  numb[d[i]]++;
  if (i != source) ////ååèç¹
    i = e[p[i]].u;
  return numb[tmp]; ////è¿åæåièç¹çè·¯å¾æ å·dãiã
                    ///çä¸ªæ°,æ­å±åç»æ
}
int maxflow() {
  int flow(0);
  int i, j;
  BFS();
  for (i = 1; i <= N; i++)
    start[i] = head[i];
  // memcpy(start,head,sizeof(start));
  i = source;
  while (d[source] < N) {
    for (j = start[i]; j != -1; j = e[j].next)
      if (e[j].f > 0 && d[i] == d[e[j].v] + 1)
        break;
    if (j != -1) ////æ¾å°å¢å¹¿çè·¯å¾
    {
      start[i] = j;
      p[e[j].v] = j;
      i = e[j].v;
      if (i == sink) /// å¢å¹¿å°äºsink
      {
        flow += Augment();
        i = source;
      }
    } else ////æ²¡æå¢å¹¿è·¯ï¼
    {
      start[i] = head[i];
      if (Retreat(i) ==
          0) ////éè¦éæ°æ è®°ï¼åãiï¼jãä¸­jæå°çè·¯å¾æ å·
        break;
    }
  }
  return flow;
}

void addedge(int a, int b, int c) {
  e[num].f = c;
  e[num].u = a;
  e[num].v = b;
  e[num].next = head[a];
  head[a] = num;
  e[num].pre = rhead[b];
  rhead[b] = num;
  num++;

  e[num].u = b;
  e[num].v = a;
  e[num].f = 0;
  e[num].next = head[b];
  head[b] = num;
  e[num].pre = rhead[a];
  rhead[a] = num;
  num++;
}
int g[maxn][maxn];
inline int min(int a, int b) { return a < b ? a : b; }
int main() {
  int i;
  int a, b, c;
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    N = n;
    memset(g, 0, sizeof(g));
    int ans = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d %d %d", &a, &b, &c);
      a++;
      b++;
      g[a][b] += c;
      ans += c;
    }
    inf = ans;
    for (int i = 2; i <= n; i++) {
      source = 1;
      sink = i;
      Init();
      int tmp = 0;
      for (int j = 1; j <= n; j++)
        for (int k = 1; k <= n; k++) {
          if (j == k)
            continue;
          if (g[j][k] < 0)
            tmp += g[j][k];
          if (g[j][k] > 0)
            addedge(j, k, g[j][k]);
        }

      // printf("%d\\n", maxflow());
      ans = min(ans, maxflow() + tmp);
    }
    printf("%d\\n", ans);
  }
  return 0;
}