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

#include <bits/stdc++.h>

using namespace std;

struct cww {
  cww() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;

typedef long long LL;
struct edge {
  int flow, to, rev;
};
typedef vector<edge> E;
typedef vector<E> FlowGraph;

void addedge(FlowGraph &g, int from, int to, int f) {
  int a = g[from].size();
  int b = g[to].size();
  g[from].push_back(edge{f, to, b});
  g[to].push_back(edge{0, from, a});
}
typedef vector<LL> V;
typedef vector<V> VV;

const LL INF = 1e15;

V bfs(FlowGraph &g, int s) {
  int n = g.size();
  queue<int> que;
  V dist(n, -1);
  dist[s] = 0;
  que.push(s);
  for (; que.size(); que.pop()) {
    auto v = que.front();
    for (auto &e : g[v]) {
      if (e.flow > 0 && dist[e.to] == -1) {
        dist[e.to] = dist[v] + 1;
        que.push(e.to);
      }
    }
  }
  return dist;
}
int maxflow(FlowGraph &g, int s, int t) {
  int res = 0, n = g.size();
  while (true) {
    auto dist = bfs(g, s);
    if (dist[t] < 0)
      break;
    vector<unsigned> iter(n, 0);
    std::function<int(int, int)> dfs = [&](int v, int f) {
      if (v == s)
        return f;
      for (auto &i = iter[v]; i < g[v].size(); i++) {
        edge &e = g[v][i];
        edge &re = g[e.to][e.rev];
        if (re.flow > 0 && dist[v] > dist[e.to]) {
          int d = dfs(e.to, min(f, re.flow));
          if (d > 0) {
            e.flow += d;
            re.flow -= d;
            return d;
          }
        }
      }
      return 0;
    };
    int f;
    while ((f = dfs(t, 114514)) > 0)
      res += f;
  }
  return res;
}

int main() {
  for (int N, M, L; cin >> N >> M >> L, N + M + L;) {
    VV g(N, V(N, INF));
    for (int i = 0; i < M; i++) {
      int u, v, d;
      cin >> u >> v >> d;
      g[u][v] = g[v][u] = d;
    }
    for (int i = 0; i < N; i++)
      g[i][i] = 0;
    for (int k = 0; k < N; k++)
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    using P = tuple<int, int>;
    vector<P> pt(L);
    for (int i = 0; i < L; i++) {
      int p, t;
      cin >> p >> t;
      pt[i] = P(t, p);
    }
    sort(pt.begin(), pt.end());
    FlowGraph fg(2 * L + 2);
    int id = 0;
    int S = id++;
    int T = id++;
    vector<int> bg(L), ed(L);
    for (int i = 0; i < L; i++) {
      bg[i] = id++;
      ed[i] = id++;
      addedge(fg, S, ed[i], 1);
      addedge(fg, bg[i], T, 1);
      int pv, tv, pu, tu;
      tie(tv, pv) = pt[i];
      for (int j = 0; j < i; j++) {
        tie(tu, pu) = pt[j];
        if (g[pv][pu] <= abs(tv - tu))
          addedge(fg, ed[j], bg[i], 1);
      }
    }
    cout << L - maxflow(fg, S, T) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

struct I {
  I() {
    ios::sync_with_stdio(false);
    cin.tie(0);
  }
} init;

typedef long long LL;
struct edge {
  int flow, to, rev;
};
typedef vector<edge> E;
typedef vector<E> FlowGraph;

void addedge(FlowGraph &g, int from, int to, int f) {
  int a = g[from].size();
  int b = g[to].size();
  g[from].push_back(edge{f, to, b});
  g[to].push_back(edge{0, from, a});
}
typedef vector<LL> V;
typedef vector<V> VV;

const LL INF = 1e15;

V bfs(FlowGraph &g, int s) {
  int n = g.size();
  queue<int> que;
  V dist(n, -1);
  que.push(s);
  for (; que.size(); que.pop()) {
    auto v = que.front();
    for (auto &e : g[v]) {
      if (e.flow > 0 && dist[e.to] == -1) {
        dist[e.to] = dist[v] + 1;
        que.push(e.to);
      }
    }
  }
  return dist;
}
int maxflow(FlowGraph &g, int s, int t) {
  int res = 0, n = g.size();
  while (true) {
    auto dist = bfs(g, s);
    if (dist[t] < 0)
      break;
    vector<unsigned> iter(n, 0);
    std::function<int(int, int)> dfs = [&](int v, int f) {
      if (v == s)
        return f;
      for (auto &i = iter[v]; i < g[v].size(); i++) {
        edge &e = g[v][i];
        edge &re = g[e.to][e.rev];
        if (re.flow > 0 && dist[v] > dist[e.to]) {
          int d = dfs(e.to, min(f, re.flow));
          if (d > 0) {
            e.flow += d;
            re.flow -= d;
            return d;
          }
        }
      }
      return 0;
    };
    int f;
    while ((f = dfs(t, 114514)) > 0)
      res += f;
  }
  return res;
}

int main() {
  for (int N, M, L; cin >> N >> M >> L, N + M + L;) {
    VV g(N, V(N, INF));
    for (int i = 0; i < M; i++) {
      int u, v, d;
      cin >> u >> v >> d;
      g[u][v] = g[v][u] = d;
    }
    for (int k = 0; k < N; k++)
      for (int i = 0; i < N; i++)
        for (int j = 0; j < N; j++)
          g[i][j] = min(g[i][j], g[i][k] + g[k][j]);
    using P = tuple<int, int>;
    vector<P> pt(L);
    for (int i = 0; i < L; i++) {
      int p, t;
      cin >> p >> t;
      pt[i] = P(t, p);
    }
    sort(pt.begin(), pt.end());
    FlowGraph fg(2 * L + 2);
    int id = 0;
    int S = id++;
    int T = id++;
    vector<int> bg(L), ed(L);
    for (int i = 0; i < L; i++) {
      bg[i] = id++;
      ed[i] = id++;
      addedge(fg, S, ed[i], 1);
      addedge(fg, bg[i], T, 1);
      int pv, tv, pu, tu;
      tie(tv, pv) = pt[i];
      for (int j = 0; j < i; j++) {
        tie(tu, pu) = pt[j];
        if (g[pv][pu] <= abs(tv - tu))
          addedge(fg, ed[j], bg[i], 1);
      }
    }
    cout << L - maxflow(fg, S, T) << endl;
  }

  return 0;
}