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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
typedef int Weight;
struct Edge {
  int src, dest;
  int cap, rev;
  Weight weight;
  bool operator<(const Edge &rhs) const { return weight > rhs.weight; }
};

const int V = 4000;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
Weight h[V];            //??????????????£???
Weight dist[V];         //???????????¢
int prevv[V], preve[V]; //??´???????????¨??????

void add_edge(Graph &g, int src, int dest, int cap, int weight) {
  g[src].push_back(Edge{src, dest, cap, (int)g[dest].size(), weight});
  g[dest].push_back(Edge{dest, src, 0, (int)g[src].size() - 1, -weight});
}
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 2147483647;
const long long int L_INF = 9223372036854775807;
Weight min_cost_flow(Graph &g, int s, int t, int f) {
  Weight res = 0;
  memset(h, 0, sizeof(h));
  typedef pair<Weight, int> P;
  while (f > 0) {
    priority_queue<P, vector<P>, greater<P>> que;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      REP(i, g[v].size()) {
        Edge &e = g[v][i];
        if (e.cap > 0 && dist[e.dest] > dist[v] + e.weight + h[v] - h[e.dest]) {
          dist[e.dest] = dist[v] + e.weight + h[v] - h[e.dest];
          prevv[e.dest] = v;
          preve[e.dest] = i;
          que.push(P(dist[e.dest], e.dest));
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    REP(v, V) h[v] += dist[v];

    int d = f;
    for (int v = t; v != s; v = prevv[v])
      d = min(d, g[prevv[v]][preve[v]].cap);
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = prevv[v]) {
      Edge &e = g[prevv[v]][preve[v]];
      e.cap -= d;
      g[v][e.rev].cap += d;
    }
  }
  return res;
}
int N, M, B;
int getneedmoney(const vector<vector<int>> &diss, const int bus,
                 const vector<int> &caps, bitset<5> &bs) {
  const static int start = 0;
  const static int person = 1;
  const int hole = person + N;
  const int goal = hole + M;
  Graph g(goal + 1);
  for (int i = 0; i < N; ++i) {
    add_edge(g, start, person + i, 1, 0);
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      int cost = max(0, diss[i][j] - bus);
      add_edge(g, person + i, hole + j, 1, cost);
    }
  }
  for (int j = 0; j < M; ++j) {
    if (bs[j]) {
      add_edge(g, hole + j, goal, caps[j], 0);
    }
  }
  int ans = min_cost_flow(g, start, goal, N);
  return ans + B * bus * bs.count();
}

int main() {
  while (1) {
    cin >> N >> M >> B;
    if (!N)
      break;
    vector<vector<int>> diss(N, vector<int>(M));
    vector<int> capas(M);
    vector<int> prices(M);
    {
      vector<pair<int, int>> people;
      for (int i = 0; i < N; ++i) {
        int x, y;
        cin >> x >> y;
        people.emplace_back(x, y);
      }
      for (int i = 0; i < M; ++i) {
        int x, y, c, f;
        cin >> x >> y >> c >> f;
        for (int j = 0; j < N; ++j) {
          const int dis =
              (abs(people[j].first - x) + abs(people[j].second - y));
          diss[j][i] = dis;
        }
        capas[i] = c;
        prices[i] = f;
      }
    }
    int finans = 1e9;
    for (int i = 0; i < (1 << M); ++i) {
      bitset<5> bs(i);
      int cap_sum = 0;
      ;
      int hole_money = 0;
      for (int j = 0; j < M; ++j) {
        if (bs[j]) {
          cap_sum += capas[j];
          hole_money += prices[j];
        }
      }
      if (cap_sum < N)
        continue;
      else {
        int amin = 0;
        int amax = 4001;
        const static int a = 13;
        ld aa = (1 + sqrt(5)) / 2;
        int premid = -1;
        int precost = -1;
        bool preminwin;
        while (amin + 5 <= amax) {

          int amid1;
          int amid2;
          int cost1;
          int cost2;
          if (premid == -1) {
            amid1 = (amin * aa + amax * 1) / (aa + 1);
            amid2 = (amin * 1 + amax * aa) / (aa + 1);
            cost1 = getneedmoney(diss, amid1, capas, bs);
            cost2 = getneedmoney(diss, amid2, capas, bs);
          } else if (preminwin) {
            amid1 = premid;
            cost1 = precost;
            amid2 = (amin * 1 + amax * aa) / (aa + 1);
            cost2 = getneedmoney(diss, amid2, capas, bs);
          } else {
            amid2 = premid;
            cost2 = precost;
            amid1 = (amin * aa + amax * 1) / (aa + 1);
            cost1 = getneedmoney(diss, amid1, capas, bs);
          }
          if (cost1 < cost2) {
            amax = amid2;
            premid = amid1;
            precost = cost1;
            preminwin = false;
          } else {
            amin = amid1;
            premid = amid2;
            precost = cost2;
            preminwin = true;
          }
        }
        int ans = 1e9;
        for (int bus = amin; bus < amax; ++bus) {
          const int cost = getneedmoney(diss, bus, capas, bs);
          if (ans < cost)
            break;
          else {
            ans = min(ans, cost);
          }
        }
        finans = min(finans, ans + hole_money);
      }
    }
    cout << finans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt"
typedef int Weight;
struct Edge {
  int src, dest;
  int cap, rev;
  Weight weight;
  bool operator<(const Edge &rhs) const { return weight > rhs.weight; }
};

const int V = 4000;
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;
Weight h[V];            //??????????????£???
Weight dist[V];         //???????????¢
int prevv[V], preve[V]; //??´???????????¨??????

void add_edge(Graph &g, int src, int dest, int cap, int weight) {
  g[src].push_back(Edge{src, dest, cap, (int)g[dest].size(), weight});
  g[dest].push_back(Edge{dest, src, 0, (int)g[src].size() - 1, -weight});
}
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c) for (auto i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 2147483647;
const long long int L_INF = 9223372036854775807;
Weight min_cost_flow(Graph &g, int s, int t, int f) {
  Weight res = 0;
  memset(h, 0, sizeof(h));
  typedef pair<Weight, int> P;
  while (f > 0) {
    priority_queue<P, vector<P>, greater<P>> que;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      REP(i, g[v].size()) {
        Edge &e = g[v][i];
        if (e.cap > 0 && dist[e.dest] > dist[v] + e.weight + h[v] - h[e.dest]) {
          dist[e.dest] = dist[v] + e.weight + h[v] - h[e.dest];
          prevv[e.dest] = v;
          preve[e.dest] = i;
          que.push(P(dist[e.dest], e.dest));
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    REP(v, V) h[v] += dist[v];

    int d = f;
    for (int v = t; v != s; v = prevv[v])
      d = min(d, g[prevv[v]][preve[v]].cap);
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = prevv[v]) {
      Edge &e = g[prevv[v]][preve[v]];
      e.cap -= d;
      g[v][e.rev].cap += d;
    }
  }
  return res;
}
int N, M, B;
int getneedmoney(const vector<vector<int>> &diss, const int bus,
                 const vector<int> &caps, bitset<5> &bs) {
  const static int start = 0;
  const static int person = 1;
  const int hole = person + N;
  const int goal = hole + M;
  Graph g(goal + 1);
  for (int i = 0; i < N; ++i) {
    add_edge(g, start, person + i, 1, 0);
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < M; ++j) {
      int cost = max(0, diss[i][j] - bus);
      add_edge(g, person + i, hole + j, 1, cost);
    }
  }
  for (int j = 0; j < M; ++j) {
    if (bs[j]) {
      add_edge(g, hole + j, goal, caps[j], 0);
    }
  }
  int ans = min_cost_flow(g, start, goal, N);
  return ans + B * bus * bs.count();
}

int main() {
  while (1) {
    cin >> N >> M >> B;
    if (!N)
      break;
    vector<vector<int>> diss(N, vector<int>(M));
    vector<int> capas(M);
    vector<int> prices(M);
    {
      vector<pair<int, int>> people;
      for (int i = 0; i < N; ++i) {
        int x, y;
        cin >> x >> y;
        people.emplace_back(x, y);
      }
      for (int i = 0; i < M; ++i) {
        int x, y, c, f;
        cin >> x >> y >> c >> f;
        for (int j = 0; j < N; ++j) {
          const int dis =
              (abs(people[j].first - x) + abs(people[j].second - y));
          diss[j][i] = dis;
        }
        capas[i] = c;
        prices[i] = f;
      }
    }
    int finans = 1e9;
    for (int i = 0; i < (1 << M); ++i) {
      bitset<5> bs(i);
      int cap_sum = 0;
      ;
      int hole_money = 0;
      for (int j = 0; j < M; ++j) {
        if (bs[j]) {
          cap_sum += capas[j];
          hole_money += prices[j];
        }
      }
      if (cap_sum < N)
        continue;
      else {
        int amin = 0;
        int amax = 4001;
        const static int a = 13;
        while (amin + a <= amax) {
          int amid1 = (amin * (a / 2 + 1) + amax * (a / 2)) / a;
          int amid2 = (amin * (a / 2) + amax * (a / 2 + 1)) / a;
          const int cost1 = getneedmoney(diss, amid1, capas, bs);
          const int cost2 = getneedmoney(diss, amid2, capas, bs);
          if (cost1 < cost2) {
            amax = amid2;
          } else {
            amin = amid1;
          }
        }
        int ans = 1e9;
        for (int bus = amin; bus < amax; ++bus) {
          const int cost = getneedmoney(diss, bus, capas, bs);
          if (ans < cost)
            break;
          else {
            ans = min(ans, cost);
          }
        }
        finans = min(finans, ans + hole_money);
      }
    }
    cout << finans << endl;
  }
  return 0;
}