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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_V = 2503;
const int INF = INT_MAX;

typedef pair<ll, int> P;
int V;

struct Edge {
  int to;
  ll cap;
  ll cost;
  int rev;

  Edge(int to = -1, ll cap = -1, ll cost = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->cost = cost;
    this->rev = rev;
  }
};

vector<Edge> G[MAX_V];
ll h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];

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

ll min_cost_flow(int s, int t, ll flow_limit) {
  ll f = 0;
  ll totalCost = 0;
  fill(h, h + MAX_V, 0);

  while (f < flow_limit) {
    // fprintf(stderr, "f: %d\\n", f);
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    pque.push(P(0, s));

    while (!pque.empty()) {
      P p = pque.top();
      pque.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;

      for (int i = 0; i < (int)G[v].size(); ++i) {
        Edge *edge = &G[v][i];
        if (edge->cap <= 0)
          continue;

        ll cost = edge->cost + h[v] - h[edge->to];
        if (dist[edge->to] - dist[v] > cost) {
          dist[edge->to] = dist[v] + cost;
          prevv[edge->to] = v;
          preve[edge->to] = i;
          pque.push(P(dist[edge->to], edge->to));
        }
      }
    }

    if (dist[t] == INF) {
      return -1;
    }

    for (int v = 0; v < V; ++v) {
      h[v] += dist[v];
    }

    ll c = flow_limit - f;
    for (int v = t; v != s; v = prevv[v]) {
      c = min(c, G[prevv[v]][preve[v]].cap);
    }

    f += c;
    totalCost += c * h[t];

    // fprintf(stderr, "h[s]: %d, h[t]: %d, cost: %d, prev_cost: %d\\n", h[s],
    // h[t], cost, prev_cost);

    for (int v = t; v != s; v = prevv[v]) {
      Edge *edge = &G[prevv[v]][preve[v]];
      edge->cap -= c;
      G[v][edge->rev].cap += c;
    }
  }

  return totalCost;
}

int main() {
  ll GETA = 1'000'000'000;
  int N, K;
  cin >> N >> K;
  V = 2 * N + 2;

  int s = 2 * N;
  int t = 2 * N + 1;

  add_edge(s, t, N * K, GETA);

  for (int i = 0; i < N; ++i) {
    add_edge(s, i, K, 0);
    add_edge(N + i, t, K, 0);
  }

  ll a;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> a;

      add_edge(i, N + j, 1, GETA - a);
    }
  }

  auto res = min_cost_flow(s, t, N * K);
  vector<string> grid(N, string(N, '.'));

  cout << N * K * GETA - res << endl;

  for (int i = 0; i < N; ++i) {
    // int j = N + i;

    for (Edge edge : G[i]) {
      // fprintf(stderr, "i: %d, to: %d, cap: %lld\\n", i, edge.to, edge.cap);
      if (edge.to == t)
        continue;
      if (edge.cap != 0)
        continue;

      grid[i][edge.to - N] = 'X';
    }
  }

  for (int i = 0; i < N; ++i) {
    cout << grid[i] << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <queue>
#include <set>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;

const int MAX_N = 50;
const int MAX_V = 2503;
const int INF = INT_MAX;

typedef pair<ll, int> P;
int V;

struct Edge {
  int to;
  ll cap;
  ll cost;
  int rev;

  Edge(int to = -1, ll cap = -1, ll cost = -1, int rev = -1) {
    this->to = to;
    this->cap = cap;
    this->cost = cost;
    this->rev = rev;
  }
};

vector<Edge> G[MAX_N];
ll h[MAX_V];
int dist[MAX_V];
int prevv[MAX_V];
int preve[MAX_V];

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

ll min_cost_flow(int s, int t, ll flow_limit) {
  ll f = 0;
  ll totalCost = 0;
  fill(h, h + MAX_V, 0);

  while (f < flow_limit) {
    // fprintf(stderr, "f: %d\\n", f);
    priority_queue<P, vector<P>, greater<P>> pque;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    pque.push(P(0, s));

    while (!pque.empty()) {
      P p = pque.top();
      pque.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;

      for (int i = 0; i < G[v].size(); ++i) {
        Edge *edge = &G[v][i];
        if (edge->cap <= 0)
          continue;

        ll cost = edge->cost + h[v] - h[edge->to];
        if (dist[edge->to] - dist[v] > cost) {
          dist[edge->to] = dist[v] + cost;
          prevv[edge->to] = v;
          preve[edge->to] = i;
          pque.push(P(dist[edge->to], edge->to));
        }
      }
    }

    if (dist[t] == INF) {
      return -1;
    }

    for (int v = 0; v < V; ++v) {
      h[v] += dist[v];
    }

    ll c = flow_limit - f;
    for (int v = t; v != s; v = prevv[v]) {
      c = min(c, G[prevv[v]][preve[v]].cap);
    }

    f += c;
    totalCost += c * h[t];

    // fprintf(stderr, "h[s]: %d, h[t]: %d, cost: %d, prev_cost: %d\\n", h[s],
    // h[t], cost, prev_cost);

    for (int v = t; v != s; v = prevv[v]) {
      Edge *edge = &G[prevv[v]][preve[v]];
      edge->cap -= c;
      G[v][edge->rev].cap += c;
    }
  }

  return totalCost;
}

int main() {
  ll GETA = 1'000'000'000;
  int N, K;
  cin >> N >> K;
  V = 2 * N + 2;

  int s = 2 * N;
  int t = 2 * N + 1;

  add_edge(s, t, N * K, GETA);

  for (int i = 0; i < N; ++i) {
    add_edge(s, i, K, 0);
    add_edge(N + i, t, K, 0);
  }

  ll a;
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < N; ++j) {
      cin >> a;

      add_edge(i, N + j, 1, GETA - a);
    }
  }

  auto res = min_cost_flow(s, t, N * K);
  vector<string> grid(N, string(N, '.'));

  cout << N * K * GETA - res << endl;

  for (int i = 0; i < N; ++i) {
    int j = N + i;

    for (Edge edge : G[i]) {
      if (edge.to == t)
        continue;
      if (edge.cap != 0)
        continue;

      grid[i][edge.to - N] = 'X';
    }
  }

  for (int i = 0; i < N; ++i) {
    cout << grid[i] << endl;
  }

  return 0;
}