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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <queue>
#include <vector>
#define INF 1e9
using namespace std;

typedef pair<int, int> P;
struct edge {
  int to, cap, cost, rev;
  edge(int t, int ca, int co, int r) {
    to = t;
    cap = ca;
    cost = co;
    rev = r;
  }
};
int V;
vector<edge> G[200];
int h[201];
int dist[201];
int prevv[201], preve[201];

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

int min_cost_flow(int s, int t, int f) {
  int res = 0;
  fill(h, h + V, 0);
  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;
      for (int i = 0; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevv[e.to] = v;
          preve[e.to] = i;
          que.push(P(dist[e.to], e.to));
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    for (int v = 0; v < 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 ax[101], ay[101];
int indist[101][6];
int bx[6], by[6], c[6], f[6];

int make_graph(int bit, int d) {
  V = 2 + n + m * 2;
  for (int i = 0; i < 2 + n + m * 2; i++)
    G[i].clear();
  for (int i = 0; i < n; i++) {
    add_edge(0, 2 + i, 1, 0);
    for (int j = 0; j < m; j++) {
      if (bit >> j & 1) {
        int adcost = 0;
        if (indist[i][j] > d)
          adcost = (indist[i][j] - d);
        add_edge(2 + i, 2 + n + j, 1, adcost);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    add_edge(2 + n + i, 2 + n + m + i, c[i], 0);
    add_edge(2 + n + m + i, 1, c[i], 0);
  }
  return min_cost_flow(0, 1, n);
}

int res;

int dicide_d(int bit) {
  int fcost = 0;
  int allcap = 0, cnt = 0;
  for (int i = 0; i < m; i++) {
    if (bit >> i & 1) {
      fcost += f[i];
      allcap += c[i];
      cnt++;
    }
  }
  if (allcap < n || res < fcost)
    return -1;
  int l = 0, r = 4001;
  /*
  for(int i=0;i<25;i++){
          int ddl=(l*2+r)/3,ddr=(l+r*2)/3;
          int dl=make_graph(bit,(l*2+r)/3)+ddl*b*cnt;
          if(l*b*cnt+fcost>res)return -1;
          int dr=make_graph(bit,(l+r*2)/3)+ddr*b*cnt;
          if(dl>dr)l=(l*2+r)/3;
          else r=(l+r*2)/3;
  }
  */
  for (int i = 0; i < 16; i++) {
    int m = (l + r) / 2;
    int dm = make_graph(bit, m);
    int dm1 = make_graph(bit, m + 1);
    if (dm >= b * cnt + dm1)
      l = m;
    else
      r = m + 1;
  }
  return fcost +
         min(make_graph(bit, (l * 2 + r) / 3) + ((l * 2 + r) / 3) * b * cnt,
             make_graph(bit, (l + r * 2) / 3) + ((l + r * 2) / 3) * b * cnt);
}

int main(void) {
  while (1) {
    scanf("%d %d %d", &n, &m, &b);
    if (n + m + b == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &ax[i], &ay[i]);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d %d %d", &bx[i], &by[i], &c[i], &f[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        indist[i][j] = abs(ax[i] - bx[j]) + abs(ay[i] - by[j]);
      }
    }
    res = INF;
    for (int i = 1; i < (1 << m); i++) {
      int icost = dicide_d(i);
      if (icost != -1)
        res = min(res, icost);
    }
    printf("%d\\n", res);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <queue>
#include <vector>
#define INF 1e9
using namespace std;

typedef pair<int, int> P;
struct edge {
  int to, cap, cost, rev;
  edge(int t, int ca, int co, int r) {
    to = t;
    cap = ca;
    cost = co;
    rev = r;
  }
};
int V;
vector<edge> G[200];
int h[201];
int dist[201];
int prevv[201], preve[201];

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

int min_cost_flow(int s, int t, int f) {
  int res = 0;
  fill(h, h + V, 0);
  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;
      for (int i = 0; i < G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevv[e.to] = v;
          preve[e.to] = i;
          que.push(P(dist[e.to], e.to));
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    for (int v = 0; v < 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 ax[101], ay[101];
int indist[101][6];
int bx[6], by[6], c[6], f[6];

int make_graph(int bit, int d) {
  V = 2 + n + m * 2;
  for (int i = 0; i < 2 + n + m * 2; i++)
    G[i].clear();
  for (int i = 0; i < n; i++) {
    add_edge(0, 2 + i, 1, 0);
    for (int j = 0; j < m; j++) {
      if (bit >> j & 1) {
        int adcost = 0;
        if (indist[i][j] > d)
          adcost = (indist[i][j] - d);
        add_edge(2 + i, 2 + n + j, 1, adcost);
      }
    }
  }
  for (int i = 0; i < m; i++) {
    add_edge(2 + n + i, 2 + n + m + i, c[i], 0);
    add_edge(2 + n + m + i, 1, c[i], 0);
  }
  return min_cost_flow(0, 1, n);
}

int res;

int dicide_d(int bit) {
  int fcost = 0;
  int allcap = 0, cnt = 0;
  for (int i = 0; i < m; i++) {
    if (bit >> i & 1) {
      fcost += f[i];
      allcap += c[i];
      cnt++;
    }
  }
  if (allcap < n || res < fcost)
    return -1;
  int l = 0, r = 4001;
  /*
  for(int i=0;i<25;i++){
          int ddl=(l*2+r)/3,ddr=(l+r*2)/3;
          int dl=make_graph(bit,(l*2+r)/3)+ddl*b*cnt;
          if(l*b*cnt+fcost>res)return -1;
          int dr=make_graph(bit,(l+r*2)/3)+ddr*b*cnt;
          if(dl>dr)l=(l*2+r)/3;
          else r=(l+r*2)/3;
  }
  */
  for (int i = 0; i < 18; i++) {
    int m = (l + r) / 2;
    int dm = make_graph(bit, m);
    int dm1 = make_graph(bit, m + 1);
    if (dm >= b * cnt + dm1)
      l = m;
    else
      r = m + 1;
  }
  return fcost +
         min(make_graph(bit, (l * 2 + r) / 3) + ((l * 2 + r) / 3) * b * cnt,
             make_graph(bit, (l + r * 2) / 3) + ((l + r * 2) / 3) * b * cnt);
}

int main(void) {
  while (1) {
    scanf("%d %d %d", &n, &m, &b);
    if (n + m + b == 0)
      break;
    for (int i = 0; i < n; i++) {
      scanf("%d %d", &ax[i], &ay[i]);
    }
    for (int i = 0; i < m; i++) {
      scanf("%d %d %d %d", &bx[i], &by[i], &c[i], &f[i]);
    }
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < m; j++) {
        indist[i][j] = abs(ax[i] - bx[j]) + abs(ay[i] - by[j]);
      }
    }
    res = INF;
    for (int i = 1; i < (1 << m); i++) {
      int icost = dicide_d(i);
      if (icost != -1)
        res = min(res, icost);
    }
    printf("%d\\n", res);
  }
  return 0;
}