Problem p00825 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
#define inf 100000000

using namespace std;

struct edge {
  int to, cap, cost, rev;
  edge(int a, int b, int c, int d) { to = a, cap = b, cost = c, rev = d; }
};

int n;
vector<edge> G[370];
const int S = 0, T = 366;
int prevv[370], preve[350];

void add_edge(int s, int t, int cap, int cost) {
  G[s].push_back(edge(t, cap, cost, G[t].size()));
  G[t].push_back(edge(s, 0, -cost, G[s].size() - 1));
}

void BellmanFord() {
  int dist[370];
  for (int i = S; i <= T; i++)
    dist[i] = inf;
  dist[S] = 0;
  prevv[S] = -1;

  bool update = true;
  while (update) {
    update = false;
    for (int i = S; i <= T; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        if (G[i][j].cap == 0)
          continue;
        if (dist[G[i][j].to] > dist[i] + G[i][j].cost) {
          update = true;
          dist[G[i][j].to] = dist[i] + G[i][j].cost;
          prevv[G[i][j].to] = i;
          preve[G[i][j].to] = j;
        }
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = S; i <= T; i++)
      G[i].clear();
    for (int i = S; i < T; i++)
      add_edge(i, i + 1, inf, 0);

    int ii, jj, ww;
    for (int i = 0; i < n; i++) {
      cin >> ii >> jj >> ww;
      add_edge(ii, jj + 1, 1, -ww);
    }

    int F = 2, ans = 0;
    while (F > 0) {
      BellmanFord();

      int flow = F;
      int p = T;
      while (prevv[p] != -1) {
        flow = min(flow, G[prevv[p]][preve[p]].cap);
        p = prevv[p];
      }

      p = T;
      while (prevv[p] != -1) {
        ans += G[prevv[p]][preve[p]].cost * flow;
        G[prevv[p]][preve[p]].cap -= flow;
        G[p][G[prevv[p]][preve[p]].rev].cap += flow;
        p = prevv[p];
      }
      F -= flow;
    }
    cout << -ans << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <vector>
#define inf 100000000

using namespace std;

struct edge {
  int to, cap, cost, rev;
  edge(int a, int b, int c, int d) { to = a, cap = b, cost = c, rev = d; }
};

int n;
vector<edge> G[370];
const int S = 0, T = 366;
int prevv[370], preve[370];

void add_edge(int s, int t, int cap, int cost) {
  G[s].push_back(edge(t, cap, cost, G[t].size()));
  G[t].push_back(edge(s, 0, -cost, G[s].size() - 1));
}

void BellmanFord() {
  int dist[370];
  for (int i = S; i <= T; i++)
    dist[i] = inf;
  dist[S] = 0;
  prevv[S] = -1;

  bool update = true;
  while (update) {
    update = false;
    for (int i = S; i <= T; i++) {
      for (int j = 0; j < G[i].size(); j++) {
        if (G[i][j].cap == 0)
          continue;
        if (dist[G[i][j].to] > dist[i] + G[i][j].cost) {
          update = true;
          dist[G[i][j].to] = dist[i] + G[i][j].cost;
          prevv[G[i][j].to] = i;
          preve[G[i][j].to] = j;
        }
      }
    }
  }
}

int main(void) {
  while (1) {
    cin >> n;
    if (n == 0)
      break;

    for (int i = S; i <= T; i++)
      G[i].clear();
    for (int i = S; i < T; i++)
      add_edge(i, i + 1, inf, 0);

    int ii, jj, ww;
    for (int i = 0; i < n; i++) {
      cin >> ii >> jj >> ww;
      add_edge(ii, jj + 1, 1, -ww);
    }

    int F = 2, ans = 0;
    while (F > 0) {
      BellmanFord();

      int flow = F;
      int p = T;
      while (prevv[p] != -1) {
        flow = min(flow, G[prevv[p]][preve[p]].cap);
        p = prevv[p];
      }

      p = T;
      while (prevv[p] != -1) {
        ans += G[prevv[p]][preve[p]].cost * flow;
        G[prevv[p]][preve[p]].cap -= flow;
        G[p][G[prevv[p]][preve[p]].rev].cap += flow;
        p = prevv[p];
      }
      F -= flow;
    }
    cout << -ans << endl;
  }

  return 0;
}