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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define V 10000
#define INF 100000000

typedef pair<int, int> P;

struct edge {
  int to, cap, cost, rev;
  edge(int a, int b, int c, int d) {
    to = a;
    cap = b, cost = c;
    rev = d;
  }
};

int n, m[110], g, a;
int x[110][30], y[110][30], c[110][30];
vector<int> ar[110];
P ans;

vector<edge> G[V];
int h[V];
int dist[V];
int prevv[V], preve[V];

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

P min_cost_flow(int s, int t, int f) {
  int res = 0;
  fill(h, h + V, 0);
  while (f > 0) {
    priority_queue<P, vector<P>, greater<P>> q;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    q.push(P(0, s));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (int i = 0; i < 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;
          q.push(P(dist[e.to], e.to));
        }
      }
    }
    if (dist[t] == INF)
      return P(f, res);

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

    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 P(f, res);
}

int main() {
  while (scanf("%d", &n) != EOF, n) {
    m[0] = 1;
    ar[0].clear();
    ar[0].push_back(0);
    x[0][0] = 0;
    y[0][0] = 0;
    c[0][0] = 0;
    for (int i = 1; i < n; i++) {
      scanf("%d", &m[i]);
      ar[i].clear();
      for (int j = 0; j < m[i]; j++) {
        scanf("%d %d %d", &x[i][j], &y[i][j], &c[i][j]);
        ar[i].push_back(y[i][j]);
      }
      sort(ar[i].begin(), ar[i].end());
      ar[i].erase(unique(ar[i].begin(), ar[i].end()), ar[i].end());
    }
    m[n] = 1;
    ar[n].clear();
    ar[n].push_back(INF);
    x[n][0] = INF;
    y[n][0] = INF;
    c[n][0] = 0;
    scanf("%d", &g);

    for (int i = 0; i <= V; i++)
      G[i].clear();
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < ar[i].size(); j++) {
        for (int k = 0; k < m[i + 1]; k++) {
          if (ar[i][j] <= x[i + 1][k]) {
            for (int l = 0; l < ar[i + 1].size(); l++) {
              if (y[i + 1][k] == ar[i + 1][l]) {
                a = l;
                break;
              }
            }
            add_edge(i * 50 + j * 2 + 1, (i + 1) * 50 + a * 2, 1, c[i + 1][k]);
          }
        }
      }
      for (int j = 0; j < ar[i + 1].size(); j++) {
        add_edge((i + 1) * 50 + j * 2, (i + 1) * 50 + j * 2 + 1, 1, 0);
      }
    }

    ans = min_cost_flow(1, n * 50, g);
    cout << g - ans.first << " " << ans.second << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define V 10000
#define INF 100000000

typedef pair<int, int> P;
struct edge {
  int to, cap, cost, rev;
  edge(int a, int b, int c, int d) {
    to = a;
    cap = b, cost = c;
    rev = d;
  }
};

int n, m[110], g, a, b;
int x[110][30], y[110][30], c[110][30];
vector<int> ar[30];

vector<edge> G[V];
int h[V];
int dist[V];
int prevv[V], preve[V];

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

int min_cost_flow(int s, int t, int f) {
  int res = 0;
  fill(h, h + V, 0);
  while (f > 0) {
    priority_queue<P, vector<P>, greater<P>> q;
    fill(dist, dist + V, INF);
    dist[s] = 0;
    q.push(P(0, s));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (int i = 0; i < 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;
          q.push(P(dist[e.to], e.to));
        }
      }
    }
    if (dist[t] == INF)
      return -1;

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

    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 min_cost_flow(int s,int t,int f){
  int res = 0;
  while(f>0){
    fill(dist,dist+V,INF);
    dist[s] = 0;
    bool up = true;
    while(up){
      up = false;
      for(int v=0;v<V;v++){
        if(dist[v] == INF)continue;
        for(int i=0;i<G[v].size();i++){
          edge &e = G[v][i];
          if(e.cap > 0 && dist[e.to] > dist[v] + e.cost){
            dist[e.to] = dist[v] + e.cost;
            prevv[e.to] = v;
            preve[e.to] = i;
            up = true;
          }
        }
      }
    }

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

    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*dist[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() {
  while (scanf("%d", &n) != EOF, n) {
    m[0] = 1;
    ar[0].clear();
    ar[0].push_back(0);
    x[0][0] = 0;
    y[0][0] = 0;
    c[0][0] = 0;
    for (int i = 1; i < n; i++) {
      scanf("%d", &m[i]);
      ar[i].clear();
      for (int j = 0; j < m[i]; j++) {
        scanf("%d %d %d", &x[i][j], &y[i][j], &c[i][j]);
        ar[i].push_back(y[i][j]);
      }
      sort(ar[i].begin(), ar[i].end());
      ar[i].erase(unique(ar[i].begin(), ar[i].end()), ar[i].end());
    }
    m[n] = 1;
    ar[n].clear();
    ar[n].push_back(INF);
    x[n][0] = INF;
    y[n][0] = INF;
    c[n][0] = 0;
    scanf("%d", &g);

    for (int ans = g; ans >= 0; ans--) {
      for (int i = 0; i <= V; i++)
        G[i].clear();
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < ar[i].size(); j++) {
          for (int k = 0; k < m[i + 1]; k++) {
            if (ar[i][j] <= x[i + 1][k]) {
              for (int l = 0; l < ar[i + 1].size(); l++) {
                if (y[i + 1][k] == ar[i + 1][l]) {
                  a = l;
                  break;
                }
              }
              add_edge(i * 50 + j * 2 + 1, (i + 1) * 50 + a * 2, g,
                       c[i + 1][k]);
            }
          }
        }
        for (int j = 0; j < ar[i + 1].size(); j++)
          add_edge((i + 1) * 50 + j * 2, (i + 1) * 50 + j * 2 + 1, 1, 0);
      }
      /*
      if(ans == g){
        for(int i=0;i<V;i++){
          for(int j=0;j<G[i].size();j++){
            edge e = G[i][j];
            cout << i << " " << e.to << " " << e.cap << " " << e.cost << endl;
          }
        }
      }
      */

      int res = min_cost_flow(1, n * 50, ans);
      if (0 <= res) {
        cout << ans << " " << res << endl;
        break;
      }
    }
  }
}