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

#include <bits/stdc++.h>
using namespace std;

const int N = 100;
const int L = 1000;
const int INF = 1e+8 + 10;
const int MAX_V = L * 2 + 2;

struct data {
  int p, t;
  data(int p = 0, int t = 0) : p(p), t(t) {}
  bool operator<(const data &d) const { return t < d.t; }
};

struct edge {
  int to, cap, rev;
  edge(int t = 0, int c = 0, int r = 0) : to(t), cap(c), rev(r) {}
};

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

int dfs_f(int v, int t, int f, vector<edge> G[MAX_V], bool used[MAX_V]) {
  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] && e.cap > 0) {
      int d = dfs_f(e.to, t, min(f, e.cap), G, used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t, vector<edge> G[MAX_V]) {
  int flow = 0;
  while (1) {
    bool used[MAX_V];
    fill(used, used + MAX_V, false);
    int f = dfs_f(s, t, INF, G, used);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int n, m, l, G[N][N];
vector<data> v;

int solve() {
  vector<edge> fG[MAX_V];

  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]);
      }
    }
  }

  sort(v.begin(), v.end());
  for (int i = 0; i < l; i++) {
    add_edge(l * 2, i, 1, fG);
    add_edge(l + i, l * 2 + 1, 1, fG);
    // add_edge(i, l+i, 1, fG);
    for (int j = i + 1; j < l; j++) {
      if (G[v[i].p][v[j].p] < INF && v[i].t + G[v[i].p][v[j].p] <= v[j].t) {
        add_edge(j, l + i, 1, fG);
      }
    }
  }

  // ?????§???????????? = V - ????°?????????????
  return l - max_flow(l * 2, l * 2 + 1, fG);
}

int main() {
  int x, y, d;
  while (cin >> n >> m >> l && (n | m | l)) {
    fill(G[0], G[n], INF);
    v.clear();
    for (int i = 0; i < m; i++) {
      cin >> x >> y >> d;
      G[x][y] = min(G[x][y], d);
      G[y][x] = min(G[y][x], d);
    }
    for (int i = 0; i < l; i++) {
      cin >> x >> y;
      v.push_back(data(x, y));
    }
    cout << solve() << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int N = 100;
const int L = 1000;
const int INF = 1e+8 + 10;
const int MAX_V = L * 2 + 2;

struct data {
  int p, t;
  data(int p = 0, int t = 0) : p(p), t(t) {}
  bool operator<(const data &d) const { return t < d.t; }
};

struct edge {
  int to, cap, rev;
  edge(int t = 0, int c = 0, int r = 0) : to(t), cap(c), rev(r) {}
};

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

int dfs_f(const int &v, const int &t, const int &f, vector<edge> G[MAX_V],
          bool used[MAX_V]) {
  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] && e.cap > 0) {
      int d = dfs_f(e.to, t, min(f, e.cap), G, used);
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t, vector<edge> G[MAX_V]) {
  int flow = 0;
  while (1) {
    bool used[MAX_V];
    fill(used, used + MAX_V, false);
    int f = dfs_f(s, t, INF, G, used);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int n, m, l, G[N][N];
vector<data> v;

int solve() {
  vector<edge> fG[MAX_V];

  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]);
      }
    }
  }

  sort(v.begin(), v.end());
  for (int i = 0; i < l; i++) {
    add_edge(l * 2, i, 1, fG);
    add_edge(l + i, l * 2 + 1, 1, fG);
    // add_edge(i, l+i, 1, fG);
    for (int j = i + 1; j < l; j++) {
      if (G[v[i].p][v[j].p] < INF && v[i].t + G[v[i].p][v[j].p] <= v[j].t) {
        add_edge(j, l + i, 1, fG);
      }
    }
  }

  // ?????§???????????? = V - ????°?????????????
  return l - max_flow(l * 2, l * 2 + 1, fG);
}

int main() {
  int x, y, d;
  while (cin >> n >> m >> l && (n | m | l)) {
    fill(G[0], G[n], INF);
    v.clear();
    for (int i = 0; i < m; i++) {
      cin >> x >> y >> d;
      G[x][y] = min(G[x][y], d);
      G[y][x] = min(G[y][x], d);
    }
    for (int i = 0; i < l; i++) {
      cin >> x >> y;
      v.push_back(data(x, y));
    }
    cout << solve() << endl;
  }
}