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

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 200
#define MAXM 3000
#define wint int
#define cint int
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
  n = _n;
  m = 0;
  memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
  next[m] = ptr[u];
  ptr[u] = m;
  zu[m] = v;
  capa[m] = w;
  cost[m] = +c;
  ++m;
  next[m] = ptr[v];
  ptr[v] = m;
  zu[m] = u;
  capa[m] = 0;
  cost[m] = -c;
  ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
  int i, u, v;
  wint f;
  cint c, cc;
  memset(pot, 0, n * sizeof(cint));
  //*
  for (bool cont = 1; cont;) {
    cont = 0;
    for (u = 0; u < n; ++u)
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
            pot[zu[i]] = pot[u] + cost[i];
            cont = 1;
          }
        }
  }
  //*/
  for (toc = 0, tof = 0; tof + wEPS < flo;) {
    typedef pair<cint, int> node;
    priority_queue<node, vector<node>, greater<node>> q;
    for (u = 0; u < n; ++u) {
      d[u] = cINF;
      vis[u] = 0;
    }
    for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
      c = q.top().first;
      u = q.top().second;
      q.pop();
      if (vis[u]++)
        continue;
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          cc = c + cost[i] + pot[u] - pot[v = zu[i]];
          if (d[v] > cc + cEPS) {
            q.push(make_pair(d[v] = cc, v));
            pree[v] = i;
          }
        }
    }
    if (!vis[ink])
      return 0;
    f = flo - tof;
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      f = min(f, capa[i]);
    }
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      capa[i] -= f;
      capa[i ^ 1] += f;
    }
    tof += f;
    toc += f * (d[ink] - pot[src] + pot[ink]);
    for (u = 0; u < n; ++u)
      pot[u] += d[u];
  }
  return 1;
}
} // namespace MCF
int x[110];
int y[110];
int p[10];
int f[10];
int X[10];
int Y[10];
int ABS(int i) { return max(i, -i); }
int a, b, c;
int calc(int d, int i) {
  int ret = 999999999;
  // for(int i=0;i<(1<<b);i++){
  int hu = 0;
  int tmp = 0;
  for (int j = 0; j < b; j++)
    if (i & (1 << j)) {
      hu += p[j];
      tmp += f[j] + d * c;
    }
  if (hu < a)
    return ret;
  MCF::init(a + b + 2);
  int s = a + b;
  int t = a + b + 1;
  for (int j = 0; j < a; j++) {
    MCF::ae(s, j, 1, 0);
    for (int k = 0; k < b; k++) {
      if (i & (1 << k))
        MCF::ae(j, a + k, 1, max(0, ABS(x[j] - X[k]) + ABS(y[j] - Y[k]) - d));
    }
  }
  for (int j = 0; j < b; j++)
    if (i & (1 << j))
      MCF::ae(a + j, t, p[j], 0);
  MCF::solve(s, t, a);
  tmp += MCF::toc;
  ret = min(ret, tmp);
  //}
  return ret;
}
int main() {
  while (scanf("%d%d%d", &a, &b, &c), a) {
    for (int i = 0; i < a; i++) {
      scanf("%d%d", x + i, y + i);
    }
    for (int i = 0; i < b; i++) {
      scanf("%d%d%d%d", X + i, Y + i, p + i, f + i);
    }
    int ret = 999999999;
    for (int k = 0; k < (1 << b); k++) {
      int L = -1;
      int R = 4010;
      while (L + 1 < R) {
        int M = (L + R) / 2;
        int v1 = calc(M, k);
        int v2 = calc(M + 1, k);
        ret = min(ret, min(v1, v2));
        if (v1 > v2) {
          L = M;
        } else {
          R = M;
        }
      }
      /*	for(int i=L-1;i<=L-1;i++){
                      if(i<0)continue;
                      ret=min(ret,calc(i,k));
              }*/
      /*for(int i=0;;i++){
              int tmp=calc(i);
              if(ret<=tmp)break;
              ret=tmp;
      }*/
    }
    printf("%d\\n", ret);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 200
#define MAXM 3000
#define wint int
#define cint int
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
  n = _n;
  m = 0;
  memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
  next[m] = ptr[u];
  ptr[u] = m;
  zu[m] = v;
  capa[m] = w;
  cost[m] = +c;
  ++m;
  next[m] = ptr[v];
  ptr[v] = m;
  zu[m] = u;
  capa[m] = 0;
  cost[m] = -c;
  ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
  int i, u, v;
  wint f;
  cint c, cc;
  memset(pot, 0, n * sizeof(cint));
  //*
  for (bool cont = 1; cont;) {
    cont = 0;
    for (u = 0; u < n; ++u)
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
            pot[zu[i]] = pot[u] + cost[i];
            cont = 1;
          }
        }
  }
  //*/
  for (toc = 0, tof = 0; tof + wEPS < flo;) {
    typedef pair<cint, int> node;
    priority_queue<node, vector<node>, greater<node>> q;
    for (u = 0; u < n; ++u) {
      d[u] = cINF;
      vis[u] = 0;
    }
    for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
      c = q.top().first;
      u = q.top().second;
      q.pop();
      if (vis[u]++)
        continue;
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          cc = c + cost[i] + pot[u] - pot[v = zu[i]];
          if (d[v] > cc + cEPS) {
            q.push(make_pair(d[v] = cc, v));
            pree[v] = i;
          }
        }
    }
    if (!vis[ink])
      return 0;
    f = flo - tof;
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      f = min(f, capa[i]);
    }
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      capa[i] -= f;
      capa[i ^ 1] += f;
    }
    tof += f;
    toc += f * (d[ink] - pot[src] + pot[ink]);
    for (u = 0; u < n; ++u)
      pot[u] += d[u];
  }
  return 1;
}
} // namespace MCF
int x[110];
int y[110];
int p[10];
int f[10];
int X[10];
int Y[10];
int ABS(int i) { return max(i, -i); }
int a, b, c;
int calc(int d, int i) {
  int ret = 999999999;
  // for(int i=0;i<(1<<b);i++){
  int hu = 0;
  int tmp = 0;
  for (int j = 0; j < b; j++)
    if (i & (1 << j)) {
      hu += p[j];
      tmp += f[j] + d * c;
    }
  if (hu < a)
    return ret;
  MCF::init(a + b + 2);
  int s = a + b;
  int t = a + b + 1;
  for (int j = 0; j < a; j++) {
    MCF::ae(s, j, 1, 0);
    for (int k = 0; k < b; k++) {
      if (i & (1 << k))
        MCF::ae(j, a + k, 1, max(0, ABS(x[j] - X[k]) + ABS(y[j] - Y[k]) - d));
    }
  }
  for (int j = 0; j < b; j++)
    if (i & (1 << j))
      MCF::ae(a + j, t, p[j], 0);
  MCF::solve(s, t, a);
  tmp += MCF::toc;
  ret = min(ret, tmp);
  //}
  return ret;
}
int main() {
  while (scanf("%d%d%d", &a, &b, &c), a) {
    for (int i = 0; i < a; i++) {
      scanf("%d%d", x + i, y + i);
    }
    for (int i = 0; i < b; i++) {
      scanf("%d%d%d%d", X + i, Y + i, p + i, f + i);
    }
    int ret = 999999999;
    for (int k = 0; k < (1 << b); k++) {
      int L = 0;
      int R = 4010;
      while (L + 1 < R) {
        int M = (L + R) / 2;
        int v1 = calc(M, k);
        int v2 = calc(M + 1, k);
        ret = min(ret, min(v1, v2));
        if (v1 > v2) {
          L = M;
        } else {
          R = M;
        }
      }
      for (int i = L - 1; i <= L + 1; i++) {
        if (i < 0)
          continue;
        ret = min(ret, calc(i, k));
      }
      /*for(int i=0;;i++){
              int tmp=calc(i);
              if(ret<=tmp)break;
              ret=tmp;
      }*/
    }
    printf("%d\\n", ret);
  }
}