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

#include <iostream>
#include <vector>
using namespace std;

#define min(a, b) (a > b ? b : a)

struct Edge {
  int to, cap, rev;
  Edge() {}
  Edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};
typedef vector<Edge> Graph;

#define N 400
#define INF 1000000007

Graph g[N];
bool used[N];

void add_edge(int from, int to, int cap) {
  g[from].push_back(Edge(to, cap, g[to].size()));
  g[to].push_back(Edge(from, 0, g[from].size() - 1));
}

int dfs(int s, int t, int f) {
  if (s == t || f == 0)
    return f;
  used[s] = true;
  for (int i = 0; i < g[s].size(); ++i) {
    Edge &e = g[s][i];
    if (e.cap > 0 && !used[e.to]) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int ret = 0;
  while (true) {
    int f = dfs(s, t, INF);
    for (int i = 0; i < N; ++i)
      used[i] = false;
    if (f == 0)
      break;
    ret += f;
  }
  return ret;
}

int main() {
  int n, m, l;
  while (cin >> n >> m >> l) {
    int mat[N][N];
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        mat[i][j] = INF;
    for (int i = 0; i < n; ++i)
      mat[i][i] = 0;
    for (int i = 0; i < m; ++i) {
      int a, b, d;
      cin >> a >> b >> d;
      --a;
      --b;
      mat[a][b] = mat[b][a] = d;
    }
    for (int i = 0; i <= 2 * n + 1; ++i)
      g[i].clear();
    for (int i = 0; i < n; ++i) {
      int p;
      cin >> p;
      add_edge(0, i + 1, p);
    }
    for (int i = 0; i < n; ++i) {
      int k;
      cin >> k;
      add_edge(n + i + 1, 2 * n + 1, k);
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (mat[i][j] < l) {
          add_edge(i + 1, n + j + 1, INF);
        }
      }
    }
    cout << max_flow(0, 2 * n + 1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

#define min(a, b) (a > b ? b : a)

struct Edge {
  int to, cap, rev;
  Edge() {}
  Edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};
typedef vector<Edge> Graph;

#define N 400
#define INF 1000000007

Graph g[N];
bool used[N];

void add_edge(int from, int to, int cap) {
  g[from].push_back(Edge(to, cap, g[to].size()));
  g[to].push_back(Edge(from, 0, g[from].size() - 1));
}

int dfs(int s, int t, int f) {
  if (s == t || f == 0)
    return f;
  used[s] = true;
  for (int i = 0; i < g[s].size(); ++i) {
    Edge &e = g[s][i];
    if (e.cap > 0 && !used[e.to]) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        g[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int ret = 0;
  while (true) {
    int f = dfs(s, t, INF);
    for (int i = 0; i < N; ++i)
      used[i] = false;
    if (f == 0)
      break;
    ret += f;
  }
  return ret;
}

int main() {
  int n, m, l;
  while (cin >> n >> m >> l) {
    int mat[N][N];
    for (int i = 0; i < n; ++i)
      for (int j = 0; j < n; ++j)
        mat[i][j] = INF;
    for (int i = 0; i < n; ++i)
      mat[i][i] = 0;
    for (int i = 0; i < m; ++i) {
      int a, b, d;
      cin >> a >> b >> d;
      --a;
      --b;
      mat[a][b] = mat[b][a] = d;
    }
    for (int i = 0; i < n; ++i) {
      int p;
      cin >> p;
      add_edge(0, i + 1, p);
    }
    for (int i = 0; i < n; ++i) {
      int k;
      cin >> k;
      add_edge(n + i + 1, 2 * n + 1, k);
    }
    for (int k = 0; k < n; ++k) {
      for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
          mat[i][j] = min(mat[i][j], mat[i][k] + mat[k][j]);
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (mat[i][j] < l) {
          add_edge(i + 1, n + j + 1, INF);
        }
      }
    }
    cout << max_flow(0, 2 * n + 1) << endl;
  }
  return 0;
}