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

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000

struct Edge {
  int to, cap, cost, rev;
};

vector<vector<Edge>> G;

void addEdge(int from, int to, int cap, int cost) {
  G[from].push_back({to, cap, cost, (int)G[to].size()});
  G[to].push_back({from, 0, -cost, (int)G[from].size() - 1});
}

typedef pair<int, int> P;

int prevV[2100], prevE[2100];
int dist[2100];
int h[2100];
int flow(int s, int t, int f) {
  int res = 0;
  int V = G.size();
  while (f > 0) {
    fill(&dist[0], &dist[0] + 2100, INF);
    dist[s] = 0;
    priority_queue<P, vector<P>, greater<P>> Q;
    Q.push({0, s});
    while (!Q.empty()) {
      P p = Q.top();
      Q.pop();
      int i = p.second;
      for (int j = 0; j < G[i].size(); ++j) {
        Edge &e = G[i][j];
        if (e.cap > 0 && dist[i] + e.cost + h[i] - h[e.to] < dist[e.to]) {
          dist[e.to] = dist[i] + e.cost + h[i] - h[e.to];
          prevV[e.to] = i;
          prevE[e.to] = j;

          Q.push({dist[e.to], e.to});
        }
      }
    }

    for (int i = 0; i < V; ++i)
      h[i] += dist[i];

    int 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[e.to][e.rev].cap += d;
    }
  }
  return res;
}

int main() {
  int N, M, L;
  while (cin >> N >> M >> L, N || M || L) {
    int m[100][100];
    fill(&m[0][0], &m[0][0] + 100 * 100, INF);
    for (int i = 0; i < N; ++i) {
      m[i][i] = 0;
    }
    for (int i = 0; i < M; ++i) {
      int u, v, d;
      cin >> u >> v >> d;
      m[u][v] = m[v][u] = d;
    }
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
        }
      }
    }

    int s = 2 * L + 1, t = s + 1;
    G.clear();
    G.resize(t + 1);

    vector<int> p(L), time(L);
    for (int i = 0; i < L; ++i) {
      cin >> p[i] >> time[i];
    }
    for (int i = 0; i < L; ++i) {
      for (int j = 0; j < L; ++j) {
        if (i != j && time[i] + m[p[i]][p[j]] <= time[j]) {
          addEdge(L + i, j, 1, 0);
        }
      }
    }
    for (int i = 0; i < L; ++i) {
      addEdge(s, i, 1, 0);
      addEdge(L + i, t, 1, 0);
      addEdge(i, L + i, 1, -1);
    }

    int r = 0;
    fill(&h[0], &h[0] + 2100, 0);
    for (int f = 1; f <= L; ++f) {
      r += flow(s, t, 1);
      if (r == -L) {
        cout << f << endl;
        break;
      }
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define INF 1000000000

struct Edge {
  int to, cap, cost, rev;
};

vector<vector<Edge>> G;

void addEdge(int from, int to, int cap, int cost) {
  G[from].push_back({to, cap, cost, (int)G[to].size()});
  G[to].push_back({from, 0, -cost, (int)G[from].size() - 1});
}

int prevV[2100], prevE[2100];
int dist[2100];
int flow(int s, int t, int f) {
  int res = 0;
  int V = G.size();
  while (f > 0) {
    fill(&dist[0], &dist[0] + 2100, INF);
    dist[s] = 0;
    while (1) {
      bool isUpdated = false;
      for (int i = 0; i < V; ++i) {
        if (dist[i] == INF)
          continue;
        for (int j = 0; j < G[i].size(); ++j) {
          Edge &e = G[i][j];
          if (e.cap > 0 && dist[i] + e.cost < dist[e.to]) {
            dist[e.to] = dist[i] + e.cost;
            prevV[e.to] = i;
            prevE[e.to] = j;
            isUpdated = true;
          }
        }
      }
      if (!isUpdated)
        break;
    }

    int d = f;
    for (int v = t; v != s; v = prevV[v]) {
      d = min(d, G[prevV[v]][prevE[v]].cap);
    }
    f -= d;
    res += d * dist[t];
    for (int v = t; v != s; v = prevV[v]) {
      Edge &e = G[prevV[v]][prevE[v]];
      e.cap -= d;
      G[e.to][e.rev].cap += d;
    }
  }
  return res;
}

int main() {
  int N, M, L;
  while (cin >> N >> M >> L, N || M || L) {
    int m[100][100];
    fill(&m[0][0], &m[0][0] + 100 * 100, INF);
    for (int i = 0; i < N; ++i) {
      m[i][i] = 0;
    }
    for (int i = 0; i < M; ++i) {
      int u, v, d;
      cin >> u >> v >> d;
      m[u][v] = m[v][u] = d;
    }
    for (int k = 0; k < N; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          m[i][j] = min(m[i][j], m[i][k] + m[k][j]);
        }
      }
    }

    int s = 2 * L + 1, t = s + 1;
    G.clear();
    G.resize(t + 1);

    vector<int> p(L), time(L);
    for (int i = 0; i < L; ++i) {
      cin >> p[i] >> time[i];
    }
    for (int i = 0; i < L; ++i) {
      for (int j = 0; j < L; ++j) {
        if (i != j && time[i] + m[p[i]][p[j]] <= time[j]) {
          addEdge(L + i, j, 1, 0);
        }
      }
    }
    for (int i = 0; i < L; ++i) {
      addEdge(s, i, 1, 0);
      addEdge(L + i, t, 1, 0);
      addEdge(i, L + i, 1, -1);
    }

    int r = 0;
    for (int l = 1; l <= L; ++l) {
      r += flow(s, t, 1);
      if (r == -L) {
        cout << l << endl;
        break;
      }
    }
  }
}