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

#include <algorithm>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 20000
#define MAXM 400000
#define wint int
#define cint int
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
  n = _n;
  m = 0;
  memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
  next[m] = ptr[u];
  ptr[u] = m;
  zu[m] = v;
  capa[m] = w;
  cost[m] = +c;
  ++m;
  next[m] = ptr[v];
  ptr[v] = m;
  zu[m] = u;
  capa[m] = 0;
  cost[m] = -c;
  ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
  int i, u, v;
  wint f;
  cint c, cc;
  memset(pot, 0, n * sizeof(cint));
  //*
  for (bool cont = 1; cont;) {
    cont = 0;
    for (u = 0; u < n; ++u)
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
            pot[zu[i]] = pot[u] + cost[i];
            cont = 1;
          }
        }
  }
  //*/
  for (toc = 0, tof = 0; tof + wEPS < flo;) {
    typedef pair<cint, int> node;
    priority_queue<node, vector<node>, greater<node>> q;
    for (u = 0; u < n; ++u) {
      d[u] = cINF;
      vis[u] = 0;
    }
    for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
      c = q.top().first;
      u = q.top().second;
      q.pop();
      if (vis[u]++)
        continue;
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          cc = c + cost[i] + pot[u] - pot[v = zu[i]];
          if (d[v] > cc) {
            q.push(make_pair(d[v] = cc, v));
            pree[v] = i;
          }
        }
    }
    if (!vis[ink])
      return 0;
    f = flo - tof;
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      f = min(f, capa[i]);
    }
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      capa[i] -= f;
      capa[i ^ 1] += f;
    }
    tof += f;
    toc += f * (d[ink] - pot[src] + pot[ink]);
    for (u = 0; u < n; ++u)
      pot[u] += d[u];
  }
  return 1;
}
} // namespace MCF
char str[1100];
char s[400010][30];
int ks[410000];
int slen[410000];
int tlen[410];
char t[410][30];
int dp[30][30];
map<string, int> co;
string tmp;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  // gets(str);
  getline(cin, tmp);
  int sz = 0;
  for (int i = 0; i < a; i++) {
    getline(cin, tmp);
    for (int j = 0; j < tmp.size(); j++)
      str[j] = tmp[j];
    str[tmp.size()] = 0;
    // in.get(str,1024);
    // gets(str);
    int len = 0;
    for (int j = 0; str[j]; j++) {
      if (str[j] == ' ') {
        if (len) {
          sz++;
        }
        len = 0;
      } else
        s[sz][len++] = str[j];
    }
    if (len)
      sz++;
  }
  for (int i = 0; i < b; i++)
    scanf("%s", t[i]);
  for (int i = 0; i < sz; i++) {
    string now = s[i];
    if (co.count(now))
      co[now]++;
    else {
      co[now] = 1;
      ks[i] = 1;
    }
  }

  int nsz = 0;
  for (int i = 0; i < sz; i++) {
    if (!ks[i])
      continue;
    for (int j = 0; j < 30; j++)
      s[nsz][j] = s[i][j];
    string now = s[nsz];
    ks[nsz] = co[now];
    nsz++;
  }
  sz = nsz;
  for (int i = 0; i < sz; i++)
    slen[i] = strlen(s[i]);
  for (int i = 0; i < b; i++)
    tlen[i] = strlen(t[i]);
  int S = sz + b;
  int T = sz + b + 1;
  MCF::init(T + 1);

  for (int i = 0; i < b; i++)
    MCF::ae(i + sz, T, 1, 0);
  int f = 0;
  for (int i = 0; i < sz; i++)
    if (ks[i]) {
      MCF::ae(S, i, 1, 0);
      f++;
    }
  for (int i = 0; i < sz; i++) {

    for (int j = 0; j < b; j++) {
      for (int k = 0; k < 30; k++)
        for (int l = 0; l < 30; l++)
          dp[k][l] = 999999999;
      dp[0][0] = 0;
      for (int k = 0; k <= slen[i]; k++) {
        for (int l = 0; l <= tlen[j]; l++) {
          if (k < slen[i] && l < tlen[j]) {
            if (s[i][k] == t[j][l])
              dp[k + 1][l + 1] = min(dp[k + 1][l + 1], dp[k][l]);
            else
              dp[k + 1][l + 1] = min(dp[k + 1][l + 1], dp[k][l] + 1);
          }
          if (k < slen[i])
            dp[k + 1][l] = min(dp[k + 1][l], dp[k][l] + 1);
          if (l < tlen[j])
            dp[k][l + 1] = min(dp[k][l + 1], dp[k][l] + 1);
        }
      }
      MCF::ae(i, sz + j, 1, ks[i] * dp[slen[i]][tlen[j]]);
    }
  }
  MCF::solve(S, T, f);
  printf("%d\\n", MCF::toc);
}

Error: 0

Fixed submission:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
#include <vector>
using namespace std;
namespace MCF {
// required <string.h> <vector> <queue> <algorithm>
#define MAXN 20000
#define MAXM 400000
#define wint int
#define cint int
const wint wEPS = 0;
const wint wINF = 1001001001;
const cint cEPS = 0;
const cint cINF = 1001001001;
int n, m, ptr[MAXN], next[MAXM], zu[MAXM];
wint capa[MAXM], tof;
cint cost[MAXM], toc, d[MAXN], pot[MAXN];
int vis[MAXN], pree[MAXN];
void init(int _n) {
  n = _n;
  m = 0;
  memset(ptr, ~0, n * 4);
}
void ae(int u, int v, wint w, cint c) {
  next[m] = ptr[u];
  ptr[u] = m;
  zu[m] = v;
  capa[m] = w;
  cost[m] = +c;
  ++m;
  next[m] = ptr[v];
  ptr[v] = m;
  zu[m] = u;
  capa[m] = 0;
  cost[m] = -c;
  ++m;
}
bool solve(int src, int ink, wint flo = wINF) {
  int i, u, v;
  wint f;
  cint c, cc;
  memset(pot, 0, n * sizeof(cint));
  //*
  for (bool cont = 1; cont;) {
    cont = 0;
    for (u = 0; u < n; ++u)
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          if (pot[zu[i]] > pot[u] + cost[i] + cEPS) {
            pot[zu[i]] = pot[u] + cost[i];
            cont = 1;
          }
        }
  }
  //*/
  for (toc = 0, tof = 0; tof + wEPS < flo;) {
    typedef pair<cint, int> node;
    priority_queue<node, vector<node>, greater<node>> q;
    for (u = 0; u < n; ++u) {
      d[u] = cINF;
      vis[u] = 0;
    }
    for (q.push(make_pair(d[src] = 0, src)); !q.empty();) {
      c = q.top().first;
      u = q.top().second;
      q.pop();
      if (vis[u]++)
        continue;
      for (i = ptr[u]; ~i; i = next[i])
        if (capa[i] > wEPS) {
          cc = c + cost[i] + pot[u] - pot[v = zu[i]];
          if (d[v] > cc) {
            q.push(make_pair(d[v] = cc, v));
            pree[v] = i;
          }
        }
    }
    if (!vis[ink])
      return 0;
    f = flo - tof;
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      f = min(f, capa[i]);
    }
    for (v = ink; v != src; v = zu[i ^ 1]) {
      i = pree[v];
      capa[i] -= f;
      capa[i ^ 1] += f;
    }
    tof += f;
    toc += f * (d[ink] - pot[src] + pot[ink]);
    for (u = 0; u < n; ++u)
      pot[u] += d[u];
  }
  return 1;
}
} // namespace MCF
char str[1100];
char s[40010][30];
int ks[41000];
int slen[41000];
int tlen[410];
char t[410][30];
int dp[30][30];
string tmp;
int main() {
  int a, b;
  scanf("%d%d", &a, &b);
  // gets(str);
  getline(cin, tmp);
  int sz = 0;
  for (int i = 0; i < a; i++) {
    getline(cin, tmp);
    for (int j = 0; j < tmp.size(); j++)
      str[j] = tmp[j];
    str[tmp.size()] = 0;
    // in.get(str,1024);
    // gets(str);
    int len = 0;
    for (int j = 0; str[j]; j++) {
      if (str[j] == ' ') {
        if (len) {
          sz++;
        }
        len = 0;
      } else
        s[sz][len++] = str[j];
    }
    if (len)
      sz++;
  }
  for (int i = 0; i < b; i++)
    scanf("%s", t[i]);
  for (int i = 0; i < sz; i++)
    slen[i] = strlen(s[i]);
  for (int i = 0; i < b; i++)
    tlen[i] = strlen(t[i]);
  int S = sz + b;
  int T = sz + b + 1;
  MCF::init(T + 1);

  for (int i = 0; i < b; i++)
    MCF::ae(i + sz, T, 1, 0);
  for (int i = 0; i < sz; i++)
    ks[i] = 1;
  // for(int i=0;i<sz;i++)printf("%d\\n",slen[i]);
  for (int i = 0; i < sz; i++) {
    if (!ks[i])
      continue;
    for (int j = 0; j < sz; j++) {
      if (i == j || slen[j] != slen[i])
        continue;
      // printf("%d %d\\n",i,j);
      bool ok = true;
      for (int k = 0; k < slen[i]; k++)
        if (s[i][k] != s[j][k])
          ok = false;
      if (ok) {
        //		printf("%d %d\\n",i,j);
        ks[i]++;
        ks[j] = 0;
      }
    }
  }
  int f = 0;

  for (int i = 0; i < sz; i++)
    if (ks[i]) {
      MCF::ae(S, i, 1, 0);
      f++;
    }
  // printf("%d %d\\n",sz,f);
  for (int i = 0; i < sz; i++) {
    if (!ks[i])
      continue;
    for (int j = 0; j < b; j++) {
      for (int k = 0; k < 30; k++)
        for (int l = 0; l < 30; l++)
          dp[k][l] = 999999999;
      dp[0][0] = 0;
      for (int k = 0; k <= slen[i]; k++) {
        for (int l = 0; l <= tlen[j]; l++) {
          if (k < slen[i] && l < tlen[j]) {
            if (s[i][k] == t[j][l])
              dp[k + 1][l + 1] = min(dp[k + 1][l + 1], dp[k][l]);
            else
              dp[k + 1][l + 1] = min(dp[k + 1][l + 1], dp[k][l] + 1);
          }
          if (k < slen[i])
            dp[k + 1][l] = min(dp[k + 1][l], dp[k][l] + 1);
          if (l < tlen[j])
            dp[k][l + 1] = min(dp[k][l + 1], dp[k][l] + 1);
        }
      }
      //	printf("%d %d: %d\\n",i,j,ks[i]*dp[slen[i]][tlen[j]]);
      MCF::ae(i, sz + j, 1, ks[i] * dp[slen[i]][tlen[j]]);
    }
  }
  MCF::solve(S, T, f);
  printf("%d\\n", MCF::toc);
}