Problem p01571 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int edit_distance(const string &str1, const string &str2) {
  int dp[1000][1000] = {};
  REP(i, str1.size() + 1) dp[i][0] = i;
  REP(i, str2.size() + 1) dp[0][i] = i;
  REP(i, str1.size()) REP(j, str2.size()) {
    dp[i + 1][j + 1] = min(min(dp[i][j + 1], dp[i + 1][j]) + 1,
                           dp[i][j] + (str1[i] == str2[j] ? 0 : 1));
  }
  return dp[str1.size()][str2.size()];
}
typedef int Weight;
struct Edge {
  int src, dst;
  Weight cap, cost;
  int rev;
  Edge(int src, int dst, Weight cap, Weight cost, int rev = -1)
      : src(src), dst(dst), cap(cap), cost(cost), rev(rev) {}
};

typedef vector<Edge> Node;
typedef vector<Node> Graph;
void add_edge(Graph &G, int src, int dst, Weight cap, Weight cost) {
  G[src].push_back(Edge(src, dst, cap, cost, G[dst].size()));
  G[dst].push_back(Edge(dst, src, 0, -cost, G[src].size() - 1));
}

Weight min_cost_flow(Graph &G, int s, int t, Weight f) {
  typedef pair<Weight, int> P;
  Weight res = 0;
  int V = G.size();
  vector<Weight> h(V, 0);
  vector<int> prevv(V);
  vector<int> preve(V);
  if (s == t)
    return 0;
  bool start = true;
  while (f > 0) {
    vector<Weight> dist(V, INF);
    if (start) {
      start = false;
      bool update = true;
      dist[s] = 0;
      while (update) {
        update = false;
        REP(v, V) {
          if (dist[v] == INF)
            continue;
          REP(i, G[v].size()) {
            Edge &e = G[v][i];
            if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost) {
              dist[e.dst] = dist[v] + e.cost;
              prevv[e.dst] = v;
              preve[e.dst] = i;
              update = true;
            }
          }
        }
      }
    } else {
      priority_queue<P, vector<P>, greater<P>> que;
      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;
        REP(i, G[v].size()) {
          Edge &e = G[v][i];
          if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
            dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
            prevv[e.dst] = v;
            preve[e.dst] = i;
            que.push(P(dist[e.dst], e.dst));
          }
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    REP(v, V) h[v] += dist[v];

    Weight 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, M;
  while (cin >> N >> M && N) {
    map<string, int> words;
    cin.ignore(); // '\n'
    REP(i, N) {
      string line;
      getline(cin, line);
      string w;
      stringstream ss(line);
      while (ss >> w) {
        words[w] += 1;
      }
    }
    vector<string> ws;
    FORIT(it, words) ws.push_back(it->first);
    vector<string> dict;
    REP(i, M) {
      string w;
      cin >> w;
      dict.push_back(w);
    }
    N = ws.size();
    int S = N + M;
    int T = S + 1;
    Graph G(T + 1);
    REP(i, N) { add_edge(G, S, i, 1, 0); }
    REP(i, M) { add_edge(G, N + i, T, 1, 0); }
    REP(i, N) REP(j, M) {
      add_edge(G, i, N + j, 1, words[ws[i]] * edit_distance(ws[i], dict[j]));
    }
    cout << min_cost_flow(G, S, T, N) << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cerr << *i << " ";
  cerr << endl;
}
inline bool valid(int x, int y, int W, int H) {
  return (x >= 0 && y >= 0 && x < W && y < H);
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
int edit_distance(const string &str1, const string &str2) {
  int dp[21][21] = {};
  REP(i, str1.size() + 1) dp[i][0] = i;
  REP(i, str2.size() + 1) dp[0][i] = i;
  REP(i, str1.size()) REP(j, str2.size()) {
    dp[i + 1][j + 1] = min(min(dp[i][j + 1], dp[i + 1][j]) + 1,
                           dp[i][j] + (str1[i] == str2[j] ? 0 : 1));
  }
  return dp[str1.size()][str2.size()];
}
typedef int Weight;
struct Edge {
  int src, dst;
  Weight cap, cost;
  int rev;
  Edge(int src, int dst, Weight cap, Weight cost, int rev = -1)
      : src(src), dst(dst), cap(cap), cost(cost), rev(rev) {}
};

typedef vector<Edge> Node;
typedef vector<Node> Graph;
void add_edge(Graph &G, int src, int dst, Weight cap, Weight cost) {
  G[src].push_back(Edge(src, dst, cap, cost, G[dst].size()));
  G[dst].push_back(Edge(dst, src, 0, -cost, G[src].size() - 1));
}

Weight min_cost_flow(Graph &G, int s, int t, Weight f) {
  typedef pair<Weight, int> P;
  Weight res = 0;
  int V = G.size();
  vector<Weight> h(V, 0);
  vector<int> prevv(V);
  vector<int> preve(V);
  if (s == t)
    return 0;
  bool start = true;
  while (f > 0) {
    vector<Weight> dist(V, INF);
    if (start) {
      start = false;
      bool update = true;
      dist[s] = 0;
      while (update) {
        update = false;
        REP(v, V) {
          if (dist[v] == INF)
            continue;
          REP(i, G[v].size()) {
            Edge &e = G[v][i];
            if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost) {
              dist[e.dst] = dist[v] + e.cost;
              prevv[e.dst] = v;
              preve[e.dst] = i;
              update = true;
            }
          }
        }
      }
    } else {
      priority_queue<P, vector<P>, greater<P>> que;
      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;
        REP(i, G[v].size()) {
          Edge &e = G[v][i];
          if (e.cap > 0 && dist[e.dst] > dist[v] + e.cost + h[v] - h[e.dst]) {
            dist[e.dst] = dist[v] + e.cost + h[v] - h[e.dst];
            prevv[e.dst] = v;
            preve[e.dst] = i;
            que.push(P(dist[e.dst], e.dst));
          }
        }
      }
    }
    if (dist[t] == INF)
      return -1;
    REP(v, V) h[v] += dist[v];

    Weight 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, M;
  while (cin >> N >> M && N) {
    map<string, int> words;
    cin.ignore(); // '\n'
    REP(i, N) {
      string line;
      getline(cin, line);
      string w;
      stringstream ss(line);
      while (ss >> w) {
        words[w] += 1;
      }
    }
    vector<string> ws;
    FORIT(it, words) ws.push_back(it->first);
    vector<string> dict;
    REP(i, M) {
      string w;
      cin >> w;
      dict.push_back(w);
    }
    N = ws.size();
    int S = N + M;
    int T = S + 1;
    Graph G(T + 1);
    REP(i, N) { add_edge(G, S, i, 1, 0); }
    REP(i, M) { add_edge(G, N + i, T, 1, 0); }
    REP(i, N) REP(j, M) {
      add_edge(G, i, N + j, 1, words[ws[i]] * edit_distance(ws[i], dict[j]));
    }
    cout << min_cost_flow(G, S, T, N) << endl;
  }
  return 0;
}