Problem p01199 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define SOUR (N + N)
#define SINK (N + N + 1)
#define INF (1 << 29)

class Node {
public:
  vector<int> to;
};

int flow[205][205], capa[205][205], N, M, L;

void Connect(vector<Node> &node, int u, int v, int w) {
  flow[v][u] += w;
  capa[v][u] += w;
  capa[u][v] += w;

  node[u].to.push_back(v);
  node[v].to.push_back(u);
}
int dfs(int p, int T, int mf, vector<Node> &graph, vector<int> &level,
        vector<bool> &finished) {
  if (p == T)
    return mf;
  if (finished[p])
    return 0;

  finished[p] = true;

  for (int i = 0; i < graph[p].to.size(); i++) {
    int next = graph[p].to[i], fw = capa[p][next] - flow[p][next];
    if (level[p] >= level[next])
      continue;
    if (fw <= 0)
      continue;

    int f = dfs(next, T, min(mf, fw), graph, level, finished);
    if (f > 0) {
      finished[p] = false;
      flow[p][next] += f;
      flow[next][p] -= f;
      return f;
    }
  }

  return 0;
}

int dinic(int S, int T, vector<Node> &graph) {
  bool end = false;
  int total = 0;
  while (!end) {
    end = true;
    vector<int> level(graph.size(), -1);
    level[S] = 0;

    queue<int> q;
    q.push(S);
    while (!q.empty()) {
      int n = q.front();
      q.pop();
      for (int i = 0; i < graph[n].to.size(); i++) {
        int next = graph[n].to[i];
        if (level[next] != -1)
          continue;
        if (capa[n][next] - flow[n][next] <= 0)
          continue;
        level[next] = level[n] + 1;
        q.push(next);
      }
    }

    if (level[T] == -1)
      break;

    vector<bool> finished(graph.size());
    while (1) {
      int fw = dfs(S, T, INF, graph, level, finished);
      if (fw <= 0)
        break;

      total += fw;
      end = false;
    }
  }

  return total;
}

int main() {
  while (cin >> N >> M >> L, (N || M || L)) {
    int d[100][100];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        d[i][j] = INF;
        d[i][i] = d[j][j] = 0;
      }

    for (int i = 0; i < M; i++) {
      int u, v, w;
      cin >> u >> v >> w;
      u--;
      v--;
      d[u][v] = w;
      d[v][u] = w;
    }

    for (int k = 0; k < N; k++)
      for (int y = 0; y < N; y++)
        for (int x = 0; x < N; x++)
          d[x][y] = min(d[x][y], d[x][k] + d[k][y]);

    memset(flow, 0, sizeof(flow));
    memset(capa, 0, sizeof(capa));

    vector<Node> node(SINK + 1);

    int in, out;
    for (int i = 0; i < N; i++) {
      cin >> in;
      Connect(node, SOUR, i, in);
    }

    for (int i = 0; i < N; i++) {
      cin >> out;
      Connect(node, N + i, SINK, out);
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        if (d[i][j] < L)
          Connect(node, i, N + j, INF);
      }

    cout << dinic(SOUR, SINK, node) << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define SOUR (N + N)
#define SINK (N + N + 1)
#define INF (1 << 29)

class Node {
public:
  vector<int> to;
};

int flow[205][205], capa[205][205], N, M, L;

void Connect(vector<Node> &node, int u, int v, int w) {
  flow[v][u] += w;
  capa[v][u] += w;
  capa[u][v] += w;

  node[u].to.push_back(v);
  node[v].to.push_back(u);
}
int dfs(int p, int T, int mf, vector<Node> &graph, vector<int> &level,
        vector<bool> &finished) {
  if (p == T)
    return mf;
  if (finished[p])
    return 0;

  finished[p] = true;

  for (int i = 0; i < graph[p].to.size(); i++) {
    int next = graph[p].to[i], fw = capa[p][next] - flow[p][next];
    if (level[p] >= level[next])
      continue;
    if (fw <= 0)
      continue;

    int f = dfs(next, T, min(mf, fw), graph, level, finished);
    if (f > 0) {
      finished[p] = false;
      flow[p][next] += f;
      flow[next][p] -= f;
      return f;
    }
  }

  return 0;
}

int dinic(int S, int T, vector<Node> &graph) {
  bool end = false;
  int total = 0;
  while (!end) {
    end = true;
    vector<int> level(graph.size(), -1);
    level[S] = 0;

    queue<int> q;
    q.push(S);
    while (!q.empty()) {
      int n = q.front();
      q.pop();
      for (int i = 0; i < graph[n].to.size(); i++) {
        int next = graph[n].to[i];
        if (level[next] != -1)
          continue;
        if (capa[n][next] - flow[n][next] <= 0)
          continue;
        level[next] = level[n] + 1;
        q.push(next);
      }
    }

    if (level[T] == -1)
      break;

    vector<bool> finished(graph.size());
    while (1) {
      int fw = dfs(S, T, INF, graph, level, finished);
      if (fw <= 0)
        break;

      total += fw;
      end = false;
    }
  }

  return total;
}

int main() {
  while (cin >> N >> M >> L) {
    int d[100][100];
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        d[i][j] = INF;
        d[i][i] = d[j][j] = 0;
      }

    for (int i = 0; i < M; i++) {
      int u, v, w;
      cin >> u >> v >> w;
      u--;
      v--;
      d[u][v] = w;
      d[v][u] = w;
    }

    for (int k = 0; k < N; k++)
      for (int y = 0; y < N; y++)
        for (int x = 0; x < N; x++)
          d[x][y] = min(d[x][y], d[x][k] + d[k][y]);

    memset(flow, 0, sizeof(flow));
    memset(capa, 0, sizeof(capa));

    vector<Node> node(SINK + 1);

    int in, out;
    for (int i = 0; i < N; i++) {
      cin >> in;
      Connect(node, SOUR, i, in);
    }

    for (int i = 0; i < N; i++) {
      cin >> out;
      Connect(node, N + i, SINK, out);
    }

    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++) {
        if (d[i][j] < L)
          Connect(node, i, N + j, INF);
      }

    cout << dinic(SOUR, SINK, node) << endl;
  }
}