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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

typedef long long ll;
const int MAX_V = 10001;

struct edge {
  int to, cap, cost, rev;
};

int V;
vector<edge> G[MAX_V];
ll dist[MAX_V];
ll h[MAX_V]; // |eV

// ¼OÌ¸_ÆÓ
int prevv[MAX_V];
int preve[MAX_V];

const ll INF = 1000000000000000LL;

// firstÍÅZ£AsecondÍ¸_ÌÔ
typedef pair<ll, int> P;

// from©çtoÖÞ©¤eÊcap,RXgcostÌÓðOtÉÇÁ·éB
void add_edge(int from, int to, int cap, int cost) {
  edge e;
  e.to = to, e.cap = cap, e.cost = cost, e.rev = G[to].size();
  G[from].push_back(e);
  e.to = from, e.cap = 0, e.cost = -cost, e.rev = G[from].size() - 1;
  G[to].push_back(e);
}

// s©çtÖÌ¬ÊfÌÅ¬ïp¬ðßé
// Èª¹È¢êÍ-1ðÔ·
ll min_cost_flow(int s, int t, int f) {
  ll res = 0;
  fill(h, h + V, 0);
  // ¬ÊªÀEÉB·éÜÅ¬·
  while (f > 0) {
    // _CNXg@ÅAÅZ£ðõ
    priority_queue<P, vector<P>, greater<P>> que;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (int i = 0; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevv[e.to] = v;
          preve[e.to] = i;
          que.push(P(dist[e.to], e.to));
        }
      }
    }

    if (dist[t] == INF)
      return -1;
    for (int i = 0; i < V; i++)
      h[i] += dist[i];

    int d = f;
    for (int v = t; v != s; v = prevv[v]) {
      d = min(d, G[prevv[v]][preve[v]].cap);
    }
    f -= d;
    res += 1LL * d * h[t];
    for (int v = t; v != s; v = prevv[v]) {
      edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }
  return res;
}

int main() {

  int n;
  const int s = 0;
  int t = 0;
  int a[1001];
  int b[1001];
  map<int, int> costIdxs;
  int idx = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    // Ü¾¶ÝµÄ¢È¯êÎAÇÁ
    if (costIdxs.find(a[i]) == costIdxs.end())
      costIdxs[a[i]] = idx++;
    if (costIdxs.find(b[i]) == costIdxs.end())
      costIdxs[b[i]] = idx++;
  }
  // m[hÍAn+RXgÌíÞ+èÄ³µm[h+X^[g+S[
  V = 3 + n + costIdxs.size();
  // m[h-1ªS[
  t = V - 1;
  int base = (--costIdxs.end())->first;
  // s©çem[hÖÚ±
  for (int i = 0; i < n; i++)
    add_edge(0, i + 1, 1, 0);
  // em[h©çeRXgÖÚ±
  for (int i = 1; i <= n; i++) {
    // Ç¿çðg¤©ÅñÊèÌèÄª¶Ý
    add_edge(i, costIdxs[a[i - 1]] + n + 1, 1, -b[i - 1] + base);
    add_edge(i, costIdxs[b[i - 1]] + n + 1, 1, -a[i - 1] + base);
    // èÄ³µ
    add_edge(i, V - 2, 1, base);
  }
  // eRXg©çS[ÖÚ±
  for (map<int, int>::iterator it = costIdxs.begin(); it != costIdxs.end();
       it++)
    add_edge(it->second + n + 1, V - 1, 1, 0);
  // èÄ³µ©çS[Ö
  add_edge(V - 2, t, 1000000, 0);
  ll res = -(min_cost_flow(s, t, n) - base * n);
  cout << res << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;

const int MAX_V = 1001;

struct edge {
  int to, cap, cost, rev;
};

int V;
vector<edge> G[MAX_V];
int dist[MAX_V];
int h[MAX_V]; // |eV

// ¼OÌ¸_ÆÓ
int prevv[MAX_V];
int preve[MAX_V];

const int INF = 1000000000;

// firstÍÅZ£AsecondÍ¸_ÌÔ
typedef pair<int, int> P;

// from©çtoÖÞ©¤eÊcap,RXgcostÌÓðOtÉÇÁ·éB
void add_edge(int from, int to, int cap, int cost) {
  edge e;
  e.to = to, e.cap = cap, e.cost = cost, e.rev = G[to].size();
  G[from].push_back(e);
  e.to = from, e.cap = 0, e.cost = -cost, e.rev = G[from].size() - 1;
  G[to].push_back(e);
}

// s©çtÖÌ¬ÊfÌÅ¬ïp¬ðßé
// Èª¹È¢êÍ-1ðÔ·
int min_cost_flow(int s, int t, int f) {

  int res = 0;
  fill(h, h + V, 0);
  // ¬ÊªÀEÉB·éÜÅ¬·
  while (f > 0) {
    // _CNXg@ÅAÅZ£ðõ
    priority_queue<P, vector<P>, greater<P>> que;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    que.push(P(0, s));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (int i = 0; i < (int)G[v].size(); i++) {
        edge &e = G[v][i];
        if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]) {
          dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];
          prevv[e.to] = v;
          preve[e.to] = i;
          que.push(P(dist[e.to], e.to));
        }
      }
    }

    if (dist[t] == INF)
      return -1;
    for (int i = 0; i < V; i++)
      h[i] += dist[i];

    int d = f;
    for (int v = t; v != s; v = prevv[v]) {
      d = min(d, G[prevv[v]][preve[v]].cap);
    }
    f -= d;
    res += d * h[t];
    for (int v = t; v != s; v = prevv[v]) {
      edge &e = G[prevv[v]][preve[v]];
      e.cap -= d;
      G[v][e.rev].cap += d;
    }
  }
  return res;
}

int main() {

  int n;
  const int s = 0;
  int t = 0;
  int a[1001];
  int b[1001];
  map<int, int> costIdxs;
  int idx = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i];
    // Ü¾¶ÝµÄ¢È¯êÎAÇÁ
    if (costIdxs.find(a[i]) == costIdxs.end())
      costIdxs[a[i]] = idx++;
    if (costIdxs.find(b[i]) == costIdxs.end())
      costIdxs[b[i]] = idx++;
  }
  // m[hÍAn+RXgÌíÞ+èÄ³µm[h+X^[g+S[
  V = 3 + n + costIdxs.size();
  // m[h-1ªS[
  t = V - 1;
  int base = (--costIdxs.end())->first;
  // s©çem[hÖÚ±
  for (int i = 0; i < n; i++)
    add_edge(0, i + 1, 1, 0);
  // em[h©çeRXgÖÚ±
  for (int i = 1; i <= n; i++) {
    // Ç¿çðg¤©ÅñÊèÌèÄª¶Ý
    add_edge(i, costIdxs[a[i - 1]] + n + 1, 1, -b[i - 1]);
    add_edge(i, costIdxs[b[i - 1]] + n + 1, 1, -a[i - 1]);
    // èÄ³µ
    add_edge(i, V - 2, 1, 0);
  }
  // eRXg©çS[ÖÚ±
  for (map<int, int>::iterator it = costIdxs.begin(); it != costIdxs.end();
       it++)
    add_edge(it->second + n + 1, V - 1, 1, 0);
  // èÄ³µ©çS[Ö
  add_edge(V - 2, t, INF, 0);
  int res = -(min_cost_flow(s, t, n));
  cout << res << endl;

  return 0;
}