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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cap, rev;
  Edge(){};
  Edge(int to0, int cap0) {
    to = to0;
    cap = cap0;
  }
  Edge(int to0, int cap0, int rev0) {
    to = to0;
    cap = cap0;
    rev = rev0;
  }
};

int maxFlow(const vector<vector<Edge>> &edges0, int source, int sink) {
  static vector<vector<Edge>> edges;
  static vector<bool> used;

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

  int n = edges0.size();
  edges.assign(n, vector<Edge>());
  for (int i = 0; i < n; ++i) {
    for (unsigned j = 0; j < edges0[i].size(); ++j) {
      const Edge &e = edges0[i][j];
      edges[i].push_back(Edge(e.to, e.cap, edges[e.to].size()));
      edges[e.to].push_back(Edge(i, 0, edges[i].size() - 1));
    }
  }

  int ret = 0;
  for (;;) {
    used.assign(n, false);
    int f = Func::dfs(source, sink, INT_MAX);
    if (f == 0)
      return ret;
    ret += f;
  }
}

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    vector<int> x1(n), y1(n), v(n);
    for (int i = 0; i < n; ++i)
      cin >> x1[i] >> y1[i] >> v[i];

    vector<int> x2(m), y2(m);
    for (int i = 0; i < m; ++i)
      cin >> x2[i] >> y2[i];

    vector<pair<double, pair<int, int>>> a;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        double d =
            sqrt(pow(x1[i] - x2[j], 2.0) + pow(y1[i] - y2[j], 2.0)) / v[i];
        a.push_back(make_pair(d, make_pair(i, j)));
      }
    }
    sort(a.begin(), a.end());

    int left = 0;
    int right = n * m - 1;
    while (left < right) {
      int mid = (left + right) / 2;

      vector<vector<Edge>> edges(n + m + 2);
      for (int i = 0; i < n; ++i)
        edges[0].push_back(Edge(i + 1, 1));
      for (int i = 0; i < m; ++i)
        edges[n + 1 + i].push_back(Edge(n + m + 1, 1));
      for (int i = 0; i <= mid; ++i)
        edges[a[i].second.first + 1].push_back(
            Edge(a[i].second.second + n + 1, 1));

      if (maxFlow(edges, 0, n + m + 1) == m)
        right = mid;
      else
        left = mid + 1;
    }

    printf("%.10f\\n", a[left].first);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cap, rev;
  Edge(){};
  Edge(int to0, int cap0) {
    to = to0;
    cap = cap0;
  }
  Edge(int to0, int cap0, int rev0) {
    to = to0;
    cap = cap0;
    rev = rev0;
  }
};

int maxFlow(const vector<vector<Edge>> &edges0, int source, int sink) {
  static vector<vector<Edge>> edges;
  static vector<bool> used;

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

  int n = edges0.size();
  edges.assign(n, vector<Edge>());
  for (int i = 0; i < n; ++i) {
    for (unsigned j = 0; j < edges0[i].size(); ++j) {
      const Edge &e = edges0[i][j];
      edges[i].push_back(Edge(e.to, e.cap, edges[e.to].size()));
      edges[e.to].push_back(Edge(i, 0, edges[i].size() - 1));
    }
  }

  int ret = 0;
  for (;;) {
    used.assign(n, false);
    int f = Func::dfs(source, sink, INT_MAX);
    if (f == 0)
      return ret;
    ret += f;
  }
}

int main() {
  for (;;) {
    int n, m;
    cin >> n >> m;
    if (n == 0)
      return 0;

    vector<int> x1(n), y1(n), v(n);
    for (int i = 0; i < n; ++i)
      cin >> x1[i] >> y1[i] >> v[i];

    vector<int> x2(m), y2(m);
    for (int i = 0; i < m; ++i)
      cin >> x2[i] >> y2[i];

    vector<pair<double, pair<int, int>>> a;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < m; ++j) {
        double d =
            sqrt(pow(x1[i] - x2[j], 2.0) + pow(y1[i] - y2[j], 2.0)) / v[i];
        a.push_back(make_pair(d, make_pair(i, j)));
      }
    }
    sort(a.begin(), a.end());

    int left = -1;
    int right = n * m - 1;
    while (left < right) {
      int mid = (left + right) / 2;

      vector<vector<Edge>> edges(n + m + 2);
      for (int i = 0; i < n; ++i)
        edges[0].push_back(Edge(i + 1, 1));
      for (int i = 0; i < m; ++i)
        edges[n + 1 + i].push_back(Edge(n + m + 1, 1));
      for (int i = 0; i <= mid; ++i)
        edges[a[i].second.first + 1].push_back(
            Edge(a[i].second.second + n + 1, 1));

      if (maxFlow(edges, 0, n + m + 1) == m)
        right = mid;
      else
        left = mid + 1;
    }

    printf("%.10f\\n", a[left].first);
  }
}