Problem p01915 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

struct Primal_Dual {
  typedef pair<int, int> Pi;

  struct edge {
    int to, cap, cost, rev;
  };
  vector<vector<edge>> graph;
  vector<int> potential, min_cost, prevv, preve;

  Primal_Dual(int V) : graph(V) {}

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

  int min_cost_flow(int s, int t, int f) {
    int V = graph.size(), ret = 0;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while (f > 0) {
      min_cost.assign(V, INF);
      que.push(Pi(0, s));
      min_cost[s] = 0;

      while (!que.empty()) {
        Pi p = que.top();
        que.pop();
        if (min_cost[p.second] < p.first)
          continue;
        for (int i = 0; i < graph[p.second].size(); i++) {
          edge &e = graph[p.second][i];
          int nextCost = min_cost[p.second] + e.cost + potential[p.second] -
                         potential[e.to];
          if (e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.push(Pi(min_cost[e.to], e.to));
          }
        }
      }
      if (min_cost[t] == INF)
        return -1;
      for (int v = 0; v < V; v++)
        potential[v] += min_cost[v];
      int addflow = f;
      for (int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }
};

int main() {
  int N, M, K, B[101][100], T[100];

  cin >> N >> M >> K;
  ++M;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++)
      cin >> B[i][j];
  }
  for (int i = 0; i < M - 1; i++) {
    cin >> T[i];
  }

  Primal_Dual flow(N + M + 3);
  int s = N + M, t = N + M + 1, add = N + M + 2;
  for (int i = 0; i < N; i++)
    flow.add_edge(s, i, 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++)
      flow.add_edge(i, j + N, 1, 100 - B[j][i]);
  }
  for (int j = 1; j < M; j++)
    flow.add_edge(j + N, add, T[j - 1], 0);
  flow.add_edge(N, t, INF, 0);
  flow.add_edge(add, t, N - K, 0);

  cout << 100 * N - flow.min_cost_flow(s, t, N) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int INF = 1 << 30;

struct Primal_Dual {
  typedef pair<int, int> Pi;

  struct edge {
    int to, cap, cost, rev;
  };
  vector<vector<edge>> graph;
  vector<int> potential, min_cost, prevv, preve;

  Primal_Dual(int V) : graph(V) {}

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

  int min_cost_flow(int s, int t, int f) {
    int V = graph.size(), ret = 0;
    priority_queue<Pi, vector<Pi>, greater<Pi>> que;
    potential.assign(V, 0);
    preve.assign(V, -1);
    prevv.assign(V, -1);

    while (f > 0) {
      min_cost.assign(V, INF);
      que.push(Pi(0, s));
      min_cost[s] = 0;

      while (!que.empty()) {
        Pi p = que.top();
        que.pop();
        if (min_cost[p.second] < p.first)
          continue;
        for (int i = 0; i < graph[p.second].size(); i++) {
          edge &e = graph[p.second][i];
          int nextCost = min_cost[p.second] + e.cost + potential[p.second] -
                         potential[e.to];
          if (e.cap > 0 && min_cost[e.to] > nextCost) {
            min_cost[e.to] = nextCost;
            prevv[e.to] = p.second, preve[e.to] = i;
            que.push(Pi(min_cost[e.to], e.to));
          }
        }
      }
      if (min_cost[t] == INF)
        return -1;
      for (int v = 0; v < V; v++)
        potential[v] += min_cost[v];
      int addflow = f;
      for (int v = t; v != s; v = prevv[v]) {
        addflow = min(addflow, graph[prevv[v]][preve[v]].cap);
      }
      f -= addflow;
      ret += addflow * potential[t];
      for (int v = t; v != s; v = prevv[v]) {
        edge &e = graph[prevv[v]][preve[v]];
        e.cap -= addflow;
        graph[v][e.rev].cap += addflow;
      }
    }
    return ret;
  }
};

int main() {
  int N, M, K;
  int A[100], B[100][100], T[100];

  cin >> N >> M >> K;
  ++M;
  for (int i = 0; i < M; i++) {
    for (int j = 0; j < N; j++)
      cin >> B[i][j];
  }
  for (int i = 0; i < M - 1; i++) {
    cin >> T[i];
  }

  Primal_Dual flow(N + M + 3);
  int s = N + M, t = N + M + 1, add = N + M + 2;
  for (int i = 0; i < N; i++)
    flow.add_edge(s, i, 1, 0);
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < M; j++)
      flow.add_edge(i, j + N, 1, 100 - B[j][i]);
  }
  for (int j = 1; j < M; j++)
    flow.add_edge(j + N, add, T[j - 1], 0);
  flow.add_edge(N, t, INF, 0);
  flow.add_edge(add, t, N - K, 0);

  cout << 100 * N - flow.min_cost_flow(s, t, N) << endl;
}