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

#include <cstdio>
#include <vector>
struct Edge {
  Edge(int from_, int to_, int capacity_, int rev_)
      : from(from_), to(to_), capacity(capacity_), rev(rev_) {}
  int from;
  int to;
  int capacity;
  int rev;
};

std::vector<Edge> edges[128];

void addEdge(int from, int to, int capacity) {
  int e1 = edges[from].size();
  int e2 = edges[to].size();
  edges[from].push_back(Edge(from, to, capacity, e2));
  edges[to].push_back(Edge(to, from, 0, e1));
}

bool checked[128];
void init_flow() {
  for (int i = 0; i < 128; ++i)
    checked[i] = false;
}
int flow(int from, int to, int v) {
  if (v <= 0)
    return 0;
  if (from == to)
    return v;
  int res = 0;
  checked[from] = true;
  for (Edge &edge : edges[from]) {
    if (checked[edge.to])
      continue;
    int nv = flow(edge.to, to, std::min(v, edge.capacity));
    if (nv == 0)
      continue;
    edge.capacity -= nv;
    edges[edge.to][edge.rev].capacity += nv;
    res = nv;
    break;
  }
  // checked[from] = false;
  return res;
}

int v;
int maximum_flow(int from, int to) {
  int res = 0;
  for (;;) {
    init_flow();
    // for(int i = 0; i < v; ++i) {
    //   printf("from : %d\\n", i);
    //   for(Edge edge : edges[i]) {
    //     printf("%d -> %d : %d $ %d\\n", edge.from, edge.to, edge.capacity,
    //     edges[edge.to][edge.rev].capacity);
    //   }
    // }
    int v = flow(from, to, (1 << 29));
    if (v == 0)
      break;
    res += v;
    // printf("+%d\\n", v);
  }
  return res;
}

int main() {
  int e;
  scanf("%d %d", &v, &e);
  for (int i = 0; i < e; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    addEdge(a, b, c);
  }

  printf("%d\\n", maximum_flow(0, v - 1));

  // for(int i = 0; i < v; ++i) {
  //   printf("from : %d\\n", i);
  //   for(Edge edge : edges[i]) {
  //     printf("%d -> %d : %d $ %d\\n", edge.from, edge.to, edge.capacity,
  //     edges[edge.to][edge.rev].capacity);
  //   }
  // }

  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <vector>
struct Edge {
  Edge(int from_, int to_, int capacity_, int rev_)
      : from(from_), to(to_), capacity(capacity_), rev(rev_) {}
  int from;
  int to;
  int capacity;
  int rev;
};

std::vector<Edge> edges[128];

void addEdge(int from, int to, int capacity) {
  int e1 = edges[from].size();
  int e2 = edges[to].size();
  edges[from].push_back(Edge(from, to, capacity, e2));
  edges[to].push_back(Edge(to, from, 0, e1));
}

bool checked[128];
void init_flow() {
  for (int i = 0; i < 128; ++i)
    checked[i] = false;
}
int flow(int from, int to, int v) {
  if (v <= 0)
    return 0;
  if (from == to)
    return v;
  int res = 0;
  checked[from] = true;
  for (Edge &edge : edges[from]) {
    if (checked[edge.to])
      continue;
    int nv = flow(edge.to, to, std::min(v, edge.capacity));
    if (nv == 0)
      continue;
    edge.capacity -= nv;
    edges[edge.to][edge.rev].capacity += nv;
    res = nv;
    break;
  }
  checked[from] = false;
  return res;
}

int v;
int maximum_flow(int from, int to) {
  int res = 0;
  for (;;) {
    init_flow();
    // for(int i = 0; i < v; ++i) {
    //   printf("from : %d\\n", i);
    //   for(Edge edge : edges[i]) {
    //     printf("%d -> %d : %d $ %d\\n", edge.from, edge.to, edge.capacity,
    //     edges[edge.to][edge.rev].capacity);
    //   }
    // }
    int v = flow(from, to, (1 << 29));
    if (v == 0)
      break;
    res += v;
    // printf("+%d\\n", v);
  }
  return res;
}

int main() {
  int e;
  scanf("%d %d", &v, &e);
  for (int i = 0; i < e; ++i) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    addEdge(a, b, c);
  }

  printf("%d\\n", maximum_flow(0, v - 1));

  // for(int i = 0; i < v; ++i) {
  //   printf("from : %d\\n", i);
  //   for(Edge edge : edges[i]) {
  //     printf("%d -> %d : %d $ %d\\n", edge.from, edge.to, edge.capacity,
  //     edges[edge.to][edge.rev].capacity);
  //   }
  // }

  return 0;
}