Problem p01279 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#define INF 1e5
using namespace std;

int n, m;
int x[101], y[101], v[101];
int xd[101], yd[101];
double cost[101][101];
struct edge {
  int to, cap, rev;
  edge() {}
  edge(int tt, int cc, int rr) {
    to = tt;
    cap = cc;
    rev = rr;
  }
};
vector<edge> G[205];

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));
}

bool used[205];

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      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 flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, 1);
    if (f == 0)
      return flow;
    flow += f;
  }
}

bool C(double x) {
  for (int i = 0; i <= n + m + 1; i++) {
    G[i].clear();
  }
  for (int i = 0; i < n; i++) {
    add_edge(0, i + 1, 1);
    for (int j = 0; j < m; j++) {
      if (cost[i][j] <= x)
        add_edge(i + 1, n + j + 1, 1);
    }
  }
  for (int i = 0; i < m; i++) {
    add_edge(n + i + 1, n + m + 1, 1);
  }
  return max_flow(0, n + m + 1) == m;
}

int main(void) {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n + m == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d", &x[i], &y[i], &v[i]);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &xd[i], &yd[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] =
            sqrt(pow(x[i] - xd[j], 2) + pow(y[i] - yd[j], 2)) / (double)v[i];
      }
    }
    double l = 0.0, r = INF;
    for (int i = 0; i < 100; i++) {
      double mid = (l + r) / 2.0;
      if (C(mid))
        r = mid;
      else
        l = mid;
    }
    printf("%.8f\\n", r);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <vector>
#define INF 1e5
using namespace std;

int n, m;
int x[101], y[101], v[101];
int xd[101], yd[101];
double cost[101][101];
struct edge {
  int to, cap, rev;
  edge() {}
  edge(int tt, int cc, int rr) {
    to = tt;
    cap = cc;
    rev = rr;
  }
};
vector<edge> G[201];

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));
}

bool used[201];

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      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 flow = 0;
  while (1) {
    memset(used, 0, sizeof(used));
    int f = dfs(s, t, 1);
    if (f == 0)
      return flow;
    flow += f;
  }
}

bool C(double x) {
  for (int i = 0; i <= n + m + 1; i++) {
    G[i].clear();
  }
  for (int i = 0; i < n; i++) {
    add_edge(0, i + 1, 1);
    for (int j = 0; j < m; j++) {
      if (cost[i][j] <= x)
        add_edge(i + 1, n + j + 1, 1);
    }
  }
  for (int i = 0; i < m; i++) {
    add_edge(n + i + 1, n + m + 1, 1);
  }
  return max_flow(0, n + m + 1) == m;
}

int main(void) {
  while (1) {
    scanf("%d %d", &n, &m);
    if (n + m == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d %d", &x[i], &y[i], &v[i]);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d", &xd[i], &yd[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        cost[i][j] = sqrt(pow(x[i] - xd[j], 2) + pow(y[i] - yd[j], 2));
      }
    }
    double l = 0.0, r = INF;
    for (int i = 0; i < 100; i++) {
      double mid = (l + r) / 2.0;
      if (C(mid))
        r = mid;
      else
        l = mid;
    }
    printf("%.10f\\n", r);
  }
  return 0;
}