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

#include <bits/stdc++.h>
#define int long long
#define INF 1e4
#define MAX_V 505
using namespace std;
/*?????§?????¢?????´?????????(Ford_Fulerson????????¨)???O(F|E|)*/

//????????¨????§???????(???????????????????????????)
struct edge {
  int to;
  vector<int> cap;
  int rev;
};

vector<edge> G[MAX_V]; //??°???????????£??\\?????????
bool used[MAX_V];      // DFS??§?????§??????????????????????????°

// from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, vector<int> &cap) {
  G[from].push_back((edge){to, cap, (int)G[to].size()});
  G[to].push_back(
      (edge){from, vector<int>((int)cap.size(), 0), (int)G[from].size() - 1});
}

bool check(vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    if (A[i] != 0)
      return A[i] > 0;
  return false;
}

bool check2(vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    if (A[i] == INF)
      return false;
  return true;
}

void Minus(vector<int> &res, vector<int> &v) {
  assert(res.size() == v.size());
  for (int i = 0; i < res.size(); i++)
    res[i] -= v[i];
}

void Plus(vector<int> &res, vector<int> &v) {
  assert(res.size() == v.size());
  for (int i = 0; i < res.size(); i++)
    res[i] += v[i];
}

vector<int> Min(vector<int> &A, vector<int> &B) {
  assert(A.size() == B.size());
  for (int i = 0; i < A.size(); i++) {
    if (A[i] < B[i])
      return A;
    if (A[i] > B[i])
      return B;
  }
  return A;
}

//?¢?????????????DFS??§??¢???
vector<int> dfs(int v, int t, vector<int> f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && check(e.cap)) {
      vector<int> d = dfs(e.to, t, Min(f, e.cap));

      /*      for(int j=0;j<d.size();j++){
        cout<<j<<endl;
        assert(d[j]!=INF);
        }*/

      if (check(d) && check2(d)) {
        Minus(e.cap, d);
        Plus(G[e.to][e.rev].cap, d);
        return d;
      }
    }
  }
  return vector<int>((int)f.size(), 0);
}

bool isall0(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] != 0)
      return false;
  return true;
}

bool isallnotINF(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] >= INF)
      return false;
  return true;
}

int V = 55;

// s??????t???????????§???????±???????
vector<int> max_flow(int s, int t) {
  vector<int> flow = vector<int>(V, 0);
  for (;;) {
    memset(used, 0, sizeof(used));
    vector<int> r = vector<int>(V, 0);
    r[0] = INF;
    vector<int> f = dfs(s, t, r);
    if (isall0(f))
      return flow;
    /*if(isallnotINF(f)) */ Plus(flow, f);
  }
}

vector<int> tov(string s) {

  vector<int> res = vector<int>(V, 0);

  int num1, num2, p = 0;

  if ('0' <= s[p] && s[p] <= '9') {
    num1 = 0;
    while ('0' <= s[p] && s[p] <= '9')
      num1 = num1 * 10 + s[p++] - '0';
  } else
    num1 = 1;

  if (p == s.size()) {
    num2 = 0;
  } else {
    if (p + 1 == s.size())
      num2 = 1;
    else {
      // assert(s[p+1]=='^'&&('0'<=s[p+2]&&s[p+2]<='9'));
      p += 2;
      num2 = 0;
      while ('0' <= s[p] && s[p] <= '9')
        num2 = num2 * 10 + s[p++] - '0';
    }
  }

  res[num2] = num1;

  return res;
}

vector<int> tocap(string s) {

  vector<int> res = vector<int>(V, 0);
  s += '+';

  string t;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '+') {
      vector<int> r = tov(t);
      Plus(res, r);
      t = "";
    } else
      t += s[i];
  }

  reverse(res.begin(), res.end());

  return res;
}

string itos(int num) {

  string res;

  while (num) {
    res = (char)(num % 10 + '0') + res;
    num /= 10;
  }

  return res;
}

void output(vector<int> ans) {

  reverse(ans.begin(), ans.end());

  string s;
  int flag = 0;

  for (int i = ans.size() - 1; i >= 0; i--) {
    if (ans[i] == 0)
      continue;
    if (flag)
      s += "+";
    flag = 1;
    if (i == 0 || (i != 0 && ans[i] != 1))
      s += itos(ans[i]);
    if (i == 1)
      s += "x";
    else if (i)
      s += "x^", s += itos(i);
  }
  if (s == "")
    s = "0";
  cout << s << endl;
}

signed main() {

  int n, m;

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    int a, b;
    string s;

    for (int i = 0; i < m; i++) {
      cin >> a >> b >> s;
      vector<int> cap = tocap(s);
      add_edge(a - 1, b - 1, cap);
      add_edge(b - 1, a - 1, cap);
    }
    assert(0 != n - 1);
    output(max_flow(0, n - 1));

    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define INF 1e4
#define MAX_V 505
using namespace std;
/*?????§?????¢?????´?????????(Ford_Fulerson????????¨)???O(F|E|)*/

//????????¨????§???????(???????????????????????????)
struct edge {
  int to;
  vector<int> cap;
  int rev;
};

vector<edge> G[MAX_V]; //??°???????????£??\\?????????
bool used[MAX_V];      // DFS??§?????§??????????????????????????°

// from??????to??????????????????cap???????????°?????????????????????
void add_edge(int from, int to, vector<int> &cap) {
  G[from].push_back((edge){to, cap, (int)G[to].size()});
  G[to].push_back(
      (edge){from, vector<int>((int)cap.size(), 0), (int)G[from].size() - 1});
}

bool check(vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    if (A[i] != 0)
      return A[i] > 0;
  return false;
}

bool check2(vector<int> &A) {
  for (int i = 0; i < A.size(); i++)
    if (A[i] == INF)
      return false;
  return true;
}

void Minus(vector<int> &res, vector<int> &v) {
  for (int i = 0; i < res.size(); i++)
    res[i] -= v[i];
}

void Plus(vector<int> &res, vector<int> &v) {
  for (int i = 0; i < res.size(); i++)
    res[i] += v[i];
}

vector<int> Min(vector<int> &A, vector<int> &B) {
  for (int i = 0; i < A.size(); i++) {
    if (A[i] < B[i])
      return A;
    if (A[i] > B[i])
      return B;
  }
  return A;
}

//?¢?????????????DFS??§??¢???
vector<int> dfs(int v, int t, vector<int> f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && check(e.cap)) {
      vector<int> d = dfs(e.to, t, Min(f, e.cap));

      for (int j = 0; j < d.size(); j++)
        assert(d[j] != INF);

      if (check(d) && check2(d)) {
        Minus(e.cap, d);
        Plus(G[e.to][e.rev].cap, d);
        return d;
      }
    }
  }
  return vector<int>((int)f.size(), 0);
}

bool isall0(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] != 0)
      return false;
  return true;
}

bool isallnotINF(vector<int> &v) {
  for (int i = 0; i < v.size(); i++)
    if (v[i] >= INF)
      return false;
  return true;
}

int V = 55;

// s??????t???????????§???????±???????
vector<int> max_flow(int s, int t) {
  vector<int> flow = vector<int>(V, 0);
  for (;;) {
    memset(used, 0, sizeof(used));
    vector<int> r = vector<int>(V, 0);
    r[0] = INF;
    vector<int> f = dfs(s, t, r);
    if (isall0(f))
      return flow;
    /*if(isallnotINF(f)) */ Plus(flow, f);
  }
}

vector<int> tov(string s) {

  vector<int> res = vector<int>(V, 0);

  int num1, num2, p = 0;

  if ('0' <= s[p] && s[p] <= '9') {
    num1 = 0;
    while ('0' <= s[p] && s[p] <= '9')
      num1 = num1 * 10 + s[p++] - '0';
  } else
    num1 = 1;

  if (p == s.size()) {
    num2 = 0;
  } else {
    if (p + 1 == s.size())
      num2 = 1;
    else {
      // assert(s[p+1]=='^'&&('0'<=s[p+2]&&s[p+2]<='9'));
      p += 2;
      num2 = 0;
      while ('0' <= s[p] && s[p] <= '9')
        num2 = num2 * 10 + s[p++] - '0';
    }
  }

  res[num2] = num1;

  return res;
}

vector<int> tocap(string s) {

  vector<int> res = vector<int>(V, 0);
  s += '+';

  string t;

  for (int i = 0; i < s.size(); i++) {
    if (s[i] == '+') {
      vector<int> r = tov(t);
      Plus(res, r);
      t = "";
    } else
      t += s[i];
  }

  reverse(res.begin(), res.end());

  return res;
}

string itos(int num) {

  string res;

  while (num) {
    res = (char)(num % 10 + '0') + res;
    num /= 10;
  }

  return res;
}

void output(vector<int> ans) {

  reverse(ans.begin(), ans.end());

  string s;
  int flag = 0;

  for (int i = ans.size() - 1; i >= 0; i--) {
    if (ans[i] == 0)
      continue;
    if (flag)
      s += "+";
    flag = 1;
    if (i == 0 || (i != 0 && ans[i] != 1))
      s += itos(ans[i]);
    if (i == 1)
      s += "x";
    else if (i)
      s += "x^", s += itos(i);
  }
  if (s == "")
    s = "0";
  cout << s << endl;
}

signed main() {

  int n, m;

  while (1) {

    cin >> n >> m;
    if (!n && !m)
      break;

    int a, b;
    string s;

    for (int i = 0; i < m; i++) {
      cin >> a >> b >> s;
      vector<int> cap = tocap(s);
      add_edge(a - 1, b - 1, cap);
      add_edge(b - 1, a - 1, cap);
    }
    assert(0 != n - 1);
    output(max_flow(0, n - 1));

    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
  }

  return 0;
}