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

#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <cstdlib>
#include <iomanip>
#include <map>
#include <math.h>
#include <set>
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN (-2147483647 - 1)
#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e14
#define _INFLL (long long)-1e14
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
#define bitmanip(m, val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<int, int> P;

class Networkflow {
private:
  struct edgedata {
    int to, weight, capacity;
    edgedata *dual_p;
  };
  struct node {
    int id;
    bool done;
    list<edgedata> edge;
  };
  vector<node> nodes;
  int n;
  int source, sink;

public:
  int result;
  Networkflow(vector<vector<pair<int, P>>> data, int s, int t) {
    n = data.size();
    source = s;
    sink = t;
    nodes.resize(n);
    Loop(i, n) { nodes[i] = {i, false, {}}; }
    edgedata *dummy = new edgedata;
    Loop(i, n) {
      Loop(j, data[i].size()) {
        nodes[i].edge.push_back({data[i][j].first, data[i][j].second.first,
                                 data[i][j].second.second, dummy});
        nodes[data[i][j].first].edge.push_back(
            {i, data[i][j].second.first, 0, &(nodes[i].edge.back())});
        nodes[i].edge.back().dual_p = &(nodes[data[i][j].first].edge.back());
      }
    }
    delete dummy;
  }
  void maxflow() {
    result = 0;
    stack<pair<int, edgedata *>> stk;
    int a; // the node which is focused on
    while (1) {
      a = source;
      Loop(i, n) nodes[i].done = false;
      nodes[source].done = true;
      while (a != sink) {
        int b = -1;
        edgedata *p;
        for (auto itr = nodes[a].edge.begin(); itr != nodes[a].edge.end();
             ++itr) {
          if ((*itr).capacity > 0) {
            b = (*itr).to;
            if (nodes[b].done)
              b = -1;
            else {
              p = &(*itr);
              stk.push(make_pair(a, p));
              nodes[b].done = true;
              a = b;
              break;
            }
          }
        }
        if (b == -1) {
          if (stk.empty())
            break;
          a = stk.top().first;
          stk.pop();
        }
      }
      if (stk.empty())
        break;
      while (stk.size()) {
        (*(stk.top().second)).capacity--;
        (*((*(stk.top().second)).dual_p)).capacity++;
        stk.pop();
      }
      result++;
    }
    return;
  }
};

int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<pair<int, P>>> data(v);
  Loop(i, e) {
    int u, v, c;
    cin >> u >> v >> c;
    data[u].push_back(make_pair(v, make_pair(1, c)));
  }
  Networkflow networkflow(data, 0, v - 1);
  networkflow.maxflow();
  cout << networkflow.result << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#define _USE_MATH_DEFINES
#include <bitset>
#include <cstdlib>
#include <iomanip>
#include <map>
#include <math.h>
#include <set>
using namespace std;
#define INT_MAX 2147483647
#define INT_MIN (-2147483647 - 1)
#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e14
#define _INFLL (long long)-1e14
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
#define bitmanip(m, val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef pair<int, int> P;

class Networkflow {
private:
  struct edgedata {
    int to, weight, capacity;
    edgedata *dual_p;
  };
  struct node {
    int id;
    bool done;
    vector<edgedata> edge;
  };
  vector<node> nodes;
  int n;
  int source, sink;

public:
  int result;
  Networkflow(vector<vector<pair<int, P>>> data, int s, int t) {
    n = data.size();
    source = s;
    sink = t;
    nodes.resize(n);
    Loop(i, n) { nodes[i] = {i, false, {}}; }
    edgedata *dummy = new edgedata;
    Loop(i, n) {
      Loop(j, data[i].size()) {
        nodes[i].edge.push_back({data[i][j].first, data[i][j].second.first,
                                 data[i][j].second.second, dummy});
        nodes[data[i][j].first].edge.push_back(
            {i, data[i][j].second.first, 0, &(nodes[i].edge.back())});
        nodes[i].edge.back().dual_p = &(nodes[data[i][j].first].edge.back());
      }
    }
  }
  void maxflow() {
    result = -1;
    stack<pair<int, edgedata *>> stk;
    int a; // the node which is focused on
    edgedata *dummy = new edgedata;
    stk.push(make_pair(-1, dummy));
    while (stk.size()) {
      a = source;
      Loop(i, n) { nodes[i].done = false; }
      nodes[source].done = true;
      while (a > -1 && a != sink) {
        int b = -1;
        edgedata *p;
        Loop(i, nodes[a].edge.size()) {
          if (nodes[a].edge[i].capacity > 0) {
            b = nodes[a].edge[i].to;
            if (nodes[b].done)
              b = -1;
            else {
              p = &(nodes[a].edge[i]);
              break;
            }
          }
        }
        if (b == -1) {
          a = stk.top().first;
          stk.pop();
        } else {
          stk.push(make_pair(a, p));
          nodes[b].done = true;
          a = b;
        }
      }
      while (stk.size() > 1) {
        (*(stk.top().second)).capacity--;
        (*((*(stk.top().second)).dual_p)).capacity++;
        stk.pop();
      }
      result++;
    }
    return;
  }
};

int main() {
  int v, e;
  cin >> v >> e;
  vector<vector<pair<int, P>>> data(v);
  Loop(i, e) {
    int u, v, c;
    cin >> u >> v >> c;
    data[u].push_back(make_pair(v, make_pair(1, c)));
  }
  Networkflow networkflow(data, 0, v - 1);
  networkflow.maxflow();
  cout << networkflow.result << endl;
  return 0;
}