Problem p00269 (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, cost, rev;
  Edge(){};
  Edge(int to0, int cap0, int cost0) {
    to = to0;
    cap = cap0, cost = cost0;
  }
  Edge(int to0, int cap0, int cost0, int rev0) {
    to = to0;
    cap = cap0;
    cost = cost0;
    rev = rev0;
  }
};

int minCostFlow(const vector<vector<Edge>> &edges0, int source, int sink,
                int flow) {
  int n = edges0.size();
  vector<vector<Edge>> edges(n);
  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, e.cost, edges[e.to].size()));
      edges[e.to].push_back(Edge(i, 0, -e.cost, edges[i].size() - 1));
    }
  }

  vector<int> h(n, 0);
  vector<int> prevV(n);
  vector<int> prevE(n);

  int ret = 0;
  while (flow > 0) {
    vector<int> dist(n, INT_MAX);
    dist[source] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        q;
    q.push(make_pair(0, source));

    while (!q.empty()) {
      pair<int, int> p = q.top();
      q.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (unsigned i = 0; i < edges[v].size(); ++i) {
        Edge e = edges[v][i];
        if (e.cap > 0 && dist[v] + e.cost + h[v] - h[e.to] < dist[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevV[e.to] = v;
          prevE[e.to] = i;
          q.push(make_pair(dist[e.to], e.to));
        }
      }
    }
    if (dist[sink] == INT_MAX) {
      return -1;
    }
    for (int i = 0; i < n; ++i)
      h[i] += dist[i];

    int g = flow;
    for (int i = sink; i != source; i = prevV[i])
      g = min(g, edges[prevV[i]][prevE[i]].cap);
    for (int i = sink; i != source; i = prevV[i]) {
      Edge &e = edges[prevV[i]][prevE[i]];
      e.cap -= g;
      edges[i][e.rev].cap += g;
    }
    flow -= g;
    ret += g * h[sink];
  }
  return ret;
}

int main() {
  for (;;) {
    int d, k, l;
    cin >> d >> k >> l;
    if (d == 0)
      return 0;

    vector<vector<int>> c(d, vector<int>(k));
    for (int i = 0; i < d; ++i) {
      for (int j = 0; j < k; ++j) {
        cin >> c[i][j];
      }
    }

    vector<vector<Edge>> edgesBuy(k + d + 2);
    for (int i = 0; i < k; ++i) {
      edgesBuy[0].push_back(Edge(i + 1, 0, 0));
      for (int j = 0; j < d; ++j)
        edgesBuy[i + 1].push_back(Edge(k + 1 + j, 2, c[j][i]));
    }
    for (int i = 0; i < d; ++i)
      edgesBuy[k + 1 + i].push_back(Edge(k + 1 + d, l, 0));

    int m, n, p;
    cin >> m >> n >> p;

    vector<vector<int>> r(m, vector<int>(k));
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < k; ++j) {
        cin >> r[i][j];
      }
    }
    vector<vector<int>> t(p + 1, vector<int>(k, 0));
    for (int i = 0; i < p; ++i) {
      for (int j = 0; j < k; ++j) {
        cin >> t[i][j];
      }
    }
    ++p;

    vector<vector<Edge>> edges(m + p + 2);
    for (int i = 0; i < m; ++i) {
      edges[0].push_back(Edge(i + 1, 1, 0));
      for (int j = 0; j < p; ++j) {
        int flow = 0;
        for (int a = 0; a < k; ++a) {
          if (r[i][a] < t[j][a]) {
            flow = -1;
            break;
          }
          flow += r[i][a] - t[j][a];
          edgesBuy[0][a].cap = r[i][a] - t[j][a];
        }

        if (flow != -1) {
          int cost = minCostFlow(edgesBuy, 0, k + d + 1, flow);
          if (cost != -1)
            edges[i + 1].push_back(Edge(m + 1 + j, 1, cost));
        }
      }
    }
    for (int i = 0; i < p - 1; ++i)
      edges[m + 1 + i].push_back(Edge(m + p + 1, 1, 0));
    edges[m + p].push_back(Edge(m + p + 1, n, 0));

    cout << minCostFlow(edges, 0, m + p + 1, n) << endl;
  }
}

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, cost, rev;
  Edge(){};
  Edge(int to0, int cap0, int cost0) {
    to = to0;
    cap = cap0, cost = cost0;
  }
  Edge(int to0, int cap0, int cost0, int rev0) {
    to = to0;
    cap = cap0;
    cost = cost0;
    rev = rev0;
  }
};

int minCostFlow(const vector<vector<Edge>> &edges0, int source, int sink,
                int flow) {
  int n = edges0.size();
  vector<vector<Edge>> edges(n);
  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, e.cost, edges[e.to].size()));
      edges[e.to].push_back(Edge(i, 0, -e.cost, edges[i].size() - 1));
    }
  }

  vector<int> h(n, 0);
  vector<int> prevV(n);
  vector<int> prevE(n);

  int ret = 0;
  while (flow > 0) {
    vector<int> dist(n, INT_MAX);
    dist[source] = 0;
    priority_queue<pair<int, int>, vector<pair<int, int>>,
                   greater<pair<int, int>>>
        q;
    q.push(make_pair(0, source));

    while (!q.empty()) {
      pair<int, int> p = q.top();
      q.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (unsigned i = 0; i < edges[v].size(); ++i) {
        Edge e = edges[v][i];
        if (e.cap > 0 && dist[v] + e.cost + h[v] - h[e.to] < dist[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevV[e.to] = v;
          prevE[e.to] = i;
          q.push(make_pair(dist[e.to], e.to));
        }
      }
    }
    if (dist[sink] == INT_MAX) {
      return -1;
    }
    for (int i = 0; i < n; ++i)
      h[i] += dist[i];

    int g = flow;
    for (int i = sink; i != source; i = prevV[i])
      g = min(g, edges[prevV[i]][prevE[i]].cap);
    for (int i = sink; i != source; i = prevV[i]) {
      Edge &e = edges[prevV[i]][prevE[i]];
      e.cap -= g;
      edges[i][e.rev].cap += g;
    }
    flow -= g;
    ret += g * h[sink];
  }
  return ret;
}

int main() {
  for (;;) {
    int d, k, l;
    cin >> d >> k >> l;
    if (d == 0)
      return 0;

    vector<vector<int>> c(d, vector<int>(k));
    for (int i = 0; i < d; ++i) {
      for (int j = 0; j < k; ++j) {
        cin >> c[i][j];
      }
    }

    vector<vector<Edge>> edgesBuy(k + d + 2);
    for (int i = 0; i < k; ++i) {
      edgesBuy[0].push_back(Edge(i + 1, 0, 0));
      for (int j = 0; j < d; ++j)
        edgesBuy[i + 1].push_back(Edge(k + 1 + j, 2, c[j][i]));
    }
    for (int i = 0; i < d; ++i)
      edgesBuy[k + 1 + i].push_back(Edge(k + 1 + d, l, 0));

    int m, n, p;
    cin >> m >> n >> p;

    vector<vector<int>> r(m, vector<int>(k));
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < k; ++j) {
        cin >> r[i][j];
      }
    }
    vector<vector<int>> t(p + n, vector<int>(k, 0));
    for (int i = 0; i < p; ++i) {
      for (int j = 0; j < k; ++j) {
        cin >> t[i][j];
      }
    }
    p += n;

    vector<vector<Edge>> edges(m + p + 2);
    for (int i = 0; i < m; ++i) {
      edges[0].push_back(Edge(i + 1, 1, 0));
      for (int j = 0; j < p; ++j) {
        int flow = 0;
        for (int a = 0; a < k; ++a) {
          if (r[i][a] < t[j][a]) {
            flow = -1;
            break;
          }
          flow += r[i][a] - t[j][a];
          edgesBuy[0][a].cap = r[i][a] - t[j][a];
        }

        if (flow != -1) {
          int cost = minCostFlow(edgesBuy, 0, k + d + 1, flow);
          if (cost != -1)
            edges[i + 1].push_back(Edge(m + 1 + j, 1, cost));
        }
      }
    }
    for (int i = 0; i < p; ++i)
      edges[m + 1 + i].push_back(Edge(m + p + 1, 1, 0));

    cout << minCostFlow(edges, 0, m + p + 1, n) << endl;
  }
}