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

#include <bits/stdc++.h>
using namespace std;

const int INF = 1 << 28;

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);
    prevv.assign(V, -1);
    preve.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 (ret);
      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, A[1000], B[1000];
  cin >> N;
  vector<int> nums;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    nums.push_back(A[i]);
    nums.push_back(B[i]);
  }
  sort(nums.begin(), nums.end());
  nums.erase(unique(nums.begin(), nums.end()), nums.end());

  int s = N + nums.size() + 1, g = s + 1;

  Primal_Dual MCF(g + 1);
  int ret = 0;
  for (int i = 0; i < N; i++) {
    A[i] = lower_bound(nums.begin(), nums.end(), A[i]) - nums.begin();
    B[i] = lower_bound(nums.begin(), nums.end(), B[i]) - nums.begin();
    MCF.add_edge(s, i, 1, 0);
    MCF.add_edge(i, N + A[i], 1, nums[A[i]]);
    MCF.add_edge(i, N + B[i], 1, nums[B[i]]);
    MCF.add_edge(i, N + nums.size(), 1, nums[A[i]] + nums[B[i]]);
    ret += nums[A[i]] + nums[B[i]];
  }

  for (int i = 0; i < nums.size(); i++) {
    MCF.add_edge(N + i, g, 1, 0);
  }

  MCF.add_edge(N + nums.size(), g, INF, 0);
  cout << ret - MCF.min_cost_flow(s, g, N) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int INF = 1 << 28;

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);
    prevv.assign(V, -1);
    preve.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 (ret);
      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, A[1000], B[1000];
  cin >> N;
  vector<int> nums;
  for (int i = 0; i < N; i++) {
    cin >> A[i] >> B[i];
    nums.push_back(A[i]);
    nums.push_back(B[i]);
  }
  sort(nums.begin(), nums.end());
  nums.erase(unique(nums.begin(), nums.end()), nums.end());

  int s = nums.size(), g = nums.size() + 1, b = nums.size() + 2;

  Primal_Dual MCF(b + 1 + nums.size() * 10);
  int ret = 0;
  for (int i = 0; i < N; i++) {
    A[i] = lower_bound(nums.begin(), nums.end(), A[i]) - nums.begin();
    B[i] = lower_bound(nums.begin(), nums.end(), B[i]) - nums.begin();
    MCF.add_edge(s, i, 1, 0);
    MCF.add_edge(i, g + A[i] + 2, 1, nums[A[i]]);
    MCF.add_edge(i, g + B[i] + 2, 1, nums[B[i]]);
    MCF.add_edge(i, b, 1, nums[A[i]] + nums[B[i]]);
    ret += nums[A[i]] + nums[B[i]];
  }
  for (int i = 0; i < nums.size(); i++) {
    MCF.add_edge(g + i + 2, g, 1, 0);
  }

  MCF.add_edge(b, g, INF, 0);
  cout << ret - MCF.min_cost_flow(s, g, N) << endl;
}