Problem p01199 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_GN = MAX_N * 2 + 2;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

/* global variables */

int n, m, l;
vpii nbrs[MAX_N];
int ps[MAX_N], ks[MAX_N];
vi rnbrs[MAX_GN];
int dists[MAX_GN], prvs[MAX_GN];
int caps[MAX_GN][MAX_GN], flows[MAX_GN][MAX_GN];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n >> m >> l;
    if (cin.eof())
      break;

    for (int i = 0; i < n; i++)
      nbrs[i].clear();

    for (int i = 0; i < m; i++) {
      int a, b, d;
      cin >> a >> b >> d;
      a--, b--;
      nbrs[a].push_back(pii(b, d));
      nbrs[b].push_back(pii(a, d));
    }

    for (int i = 0; i < n; i++)
      cin >> ps[i];
    for (int i = 0; i < n; i++)
      cin >> ks[i];

    int gn = 2 * n + 2;
    for (int i = 0; i < gn; i++)
      rnbrs[i].clear();

    for (int st = 0; st < n; st++) {
      for (int i = 0; i < n; i++)
        dists[i] = INF;
      dists[st] = 0;
      rnbrs[st].push_back(st + n);
      rnbrs[st + n].push_back(st);
      caps[st][st + n] = INF;
      caps[st + n][st] = 0;

      priority_queue<pii, vpii, greater<pii>> q;
      q.push(pii(0, st));

      while (!q.empty()) {
        pii u = q.top();
        q.pop();

        int ud = u.first;
        int ui = u.second;
        if (ud != dists[ui])
          continue;

        vpii &nbru = nbrs[ui];
        for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
          int vi = vit->first;
          int vd = ud + vit->second;
          if (vd < l && dists[vi] > vd) {
            dists[vi] = vd;
            q.push(pii(vd, vi));
            rnbrs[st].push_back(vi + n);
            rnbrs[vi + n].push_back(st);
            caps[st][vi + n] = INF;
            caps[vi + n][st] = 0;
          }
        }
      }
    }

    int st = 2 * n, gl = 2 * n + 1;

    for (int i = 0; i < n; i++) {
      rnbrs[st].push_back(i);
      caps[st][i] = ps[i];
      caps[i][st] = 0;
      rnbrs[i + n].push_back(gl);
      caps[i + n][gl] = ks[i];
      caps[gl][i + n] = 0;
    }
    memset(flows, 0, sizeof(flows));

    int max_flow = 0;

    for (;;) {
      for (int i = 0; i < gn; i++)
        dists[i] = INF, prvs[i] = -1;
      dists[st] = 0;

      priority_queue<pii, vpii, greater<pii>> q;
      q.push(pii(0, st));

      while (!q.empty()) {
        pii u = q.top();
        q.pop();

        int ud = u.first;
        int ui = u.second;
        if (ud != dists[ui])
          continue;
        if (ui == gl)
          break;

        int vd = ud + 1;
        vi &rnbru = rnbrs[ui];
        for (vi::iterator vit = rnbru.begin(); vit != rnbru.end(); vit++) {
          int vi = *vit;
          int vc = caps[ui][vi] - flows[ui][vi];
          if (vc > 0 && dists[vi] > vd) {
            dists[vi] = vd;
            prvs[vi] = ui;
            q.push(pii(vd, vi));
          }
        }
      }

      if (dists[gl] >= INF)
        break;

      int min_flow = INF;
      for (int i = gl; i != st; i = prvs[i]) {
        int &i0 = prvs[i];
        int flow = caps[i0][i] - flows[i0][i];
        if (min_flow > flow)
          min_flow = flow;
      }

      for (int i = gl; i != st; i = prvs[i]) {
        int &i0 = prvs[i];
        flows[i0][i] += min_flow;
        flows[i][i0] -= min_flow;
      }

      max_flow += min_flow;
    }

    cout << max_flow << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_GN = MAX_N * 2 + 2;
const int INF = 1 << 30;

/* typedef */

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

/* global variables */

int n, m, l;
vpii nbrs[MAX_N];
int ps[MAX_N], ks[MAX_N];
vi rnbrs[MAX_GN];
int dists[MAX_GN], prvs[MAX_GN];
int caps[MAX_GN][MAX_GN], flows[MAX_GN][MAX_GN];

/* subroutines */

/* main */

int main() {
  for (;;) {
    cin >> n >> m >> l;
    if (cin.eof())
      break;

    for (int i = 0; i < n; i++)
      nbrs[i].clear(), rnbrs[i].clear();

    for (int i = 0; i < m; i++) {
      int a, b, d;
      cin >> a >> b >> d;
      a--, b--;
      nbrs[a].push_back(pii(b, d));
      nbrs[b].push_back(pii(a, d));
    }

    for (int i = 0; i < n; i++)
      cin >> ps[i];
    for (int i = 0; i < n; i++)
      cin >> ks[i];

    for (int st = 0; st < n; st++) {
      for (int i = 0; i < n; i++)
        dists[i] = INF;
      dists[st] = 0;
      rnbrs[st].push_back(st + n);
      rnbrs[st + n].push_back(st);
      caps[st][st + n] = INF;
      caps[st + n][st] = 0;

      priority_queue<pii, vpii, greater<pii>> q;
      q.push(pii(0, st));

      while (!q.empty()) {
        pii u = q.top();
        q.pop();

        int ud = u.first;
        int ui = u.second;
        if (ud != dists[ui])
          continue;

        vpii &nbru = nbrs[ui];
        for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
          int vi = vit->first;
          int vd = ud + vit->second;
          if (vd < l && dists[vi] > vd) {
            dists[vi] = vd;
            q.push(pii(vd, vi));
            rnbrs[st].push_back(vi + n);
            rnbrs[vi + n].push_back(st);
            caps[st][vi + n] = INF;
            caps[vi + n][st] = 0;
          }
        }
      }
    }

    int gn = 2 * n + 2;
    int st = 2 * n, gl = 2 * n + 1;

    for (int i = 0; i < n; i++) {
      rnbrs[st].push_back(i);
      caps[st][i] = ps[i];
      caps[i][st] = 0;
      rnbrs[i + n].push_back(gl);
      caps[i + n][gl] = ks[i];
      caps[gl][i + n] = 0;
    }
    memset(flows, 0, sizeof(flows));

    int max_flow = 0;

    for (;;) {
      for (int i = 0; i < gn; i++)
        dists[i] = INF, prvs[i] = -1;
      dists[st] = 0;

      priority_queue<pii, vpii, greater<pii>> q;
      q.push(pii(0, st));

      while (!q.empty()) {
        pii u = q.top();
        q.pop();

        int ud = u.first;
        int ui = u.second;
        if (ud != dists[ui])
          continue;
        if (ui == gl)
          break;

        int vd = ud + 1;
        vi &rnbru = rnbrs[ui];
        for (vi::iterator vit = rnbru.begin(); vit != rnbru.end(); vit++) {
          int vi = *vit;
          int vc = caps[ui][vi] - flows[ui][vi];
          if (vc > 0 && dists[vi] > vd) {
            dists[vi] = vd;
            prvs[vi] = ui;
            q.push(pii(vd, vi));
          }
        }
      }

      if (dists[gl] >= INF)
        break;

      int min_flow = INF;
      for (int i = gl; i != st; i = prvs[i]) {
        int &i0 = prvs[i];
        int flow = caps[i0][i] - flows[i0][i];
        if (min_flow > flow)
          min_flow = flow;
      }

      for (int i = gl; i != st; i = prvs[i]) {
        int &i0 = prvs[i];
        flows[i0][i] += min_flow;
        flows[i][i0] -= min_flow;
      }

      max_flow += min_flow;
    }

    cout << max_flow << endl;
  }

  return 0;
}