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

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int INF = 1e9;
vector<vector<pair<int, int>>> G;

struct Edge {
  int cost, from, to;
  Edge(int cost, int from, int to) : cost(cost), from(from), to(to) {}
};

bool operator<(const Edge &a, const Edge &b) {
  return a.cost != b.cost   ? a.cost < b.cost
         : a.from != b.from ? a.from < b.from
                            : a.to < b.to;
}

vector<int> dijkstra(const vector<vector<pair<int, int>>> &G, int s) {
  int n = G.size();
  vector<int> D(n, INF);
  priority_queue<pair<int, int>> wait;
  wait.push(make_pair(0, s));
  while (!wait.empty()) {
    int d = -wait.top().first, v = wait.top().second;
    wait.pop();
    if (d > D[v])
      continue;
    D[v] = d;
    for (int i = 0; i < G[v].size(); ++i) {
      int d_ = d + G[v][i].first, v_ = G[v][i].second;
      if (d_ < D[v_]) {
        wait.push(make_pair(-d_, v_));
        D[v_] = d_;
      }
    }
  }
  return D;
}

int main() {
  int N, M, cap;
  while (cin >> N >> M >> cap, N) {
    string src, dest;
    cin >> src >> dest;
    map<string, int> mp;
    vector<string> C1(N), C2(N);
    vector<int> D(N);
    int t = 1;
    for (int i = 0; i < N; ++i) {
      cin >> C1[i] >> C2[i] >> D[i];
      if (!mp[C1[i]]) {
        mp[C1[i]] = t;
        ++t;
      }
      if (!mp[C2[i]]) {
        mp[C2[i]] = t;
        ++t;
      }
    }
    int n = mp.size();
    G = vector<vector<pair<int, int>>>(n);
    for (int i = 0; i < N; ++i) {
      int u = mp[C1[i]] - 1, v = mp[C2[i]] - 1, d = D[i];
      G[u].push_back(make_pair(d, v));
      G[v].push_back(make_pair(d, u));
    }
    string c;
    vector<int> S(M + 2);
    for (int i = 0; i < M; ++i) {
      cin >> c;
      S[i] = mp[c] - 1;
    }
    int s = mp[src] - 1, g = mp[dest] - 1;
    S[M] = s;
    S[M + 1] = g;
    vector<vector<pair<int, int>>> G_(M + 2);
    for (int i = 0; i < M + 2; ++i) {
      vector<int> D_ = dijkstra(G, S[i]);
      for (int j = 0; j < M + 2; ++j) {
        int d = D_[S[j]];
        if (d <= cap * 10)
          G_[i].push_back(make_pair(d, j));
      }
    }
    int ans = dijkstra(G_, M)[M + 1];
    if (ans >= INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    vector<int> HP(N);
    for (int i = 0; i < N; ++i) {
      cin >> HP[i];
    }
    int M;
    cin >> M;
    vector<int> single_d, single_mp, all_d, all_mp;
    string name, target;
    int mp, damege;
    for (int i = 0; i < M; ++i) {
      cin >> name >> mp >> target >> damege;
      if (target == "Single") {
        single_d.push_back(damege);
        single_mp.push_back(mp);
      } else {
        all_d.push_back(damege);
        all_mp.push_back(mp);
      }
    }
    long long int INF = 1e10, n = 2e5;
    vector<int> dp1(n, INF), dp2(n, INF);
    dp1[0] = 0;
    dp2[0] = 0;
    for (int i = 0; i < single_d.size(); ++i) {
      for (int j = 0; j < n; ++j) {
        if (dp1[j] >= INF)
          continue;
        if (j + single_d[i] < n)
          dp1[j + single_d[i]] =
              min(dp1[j + single_d[i]], dp1[j] + single_mp[i]);
        else
          dp1[n - 1] = min(dp1[n - 1], dp1[j] + single_mp[i]);
      }
    }
    for (int i = n - 2; i >= 0; --i)
      dp1[i] = min(dp1[i], dp1[i + 1]);
    for (int i = n - 2; i >= 0; --i)
      dp2[i] = min(dp2[i], dp2[i + 1]);

    for (int i = 0; i < all_d.size(); ++i) {
      for (int j = 0; j < n; ++j) {
        if (dp2[j] >= INF)
          continue;
        if (j + all_d[i] < n)
          dp2[j + all_d[i]] = min(dp2[j + all_d[i]], dp2[j] + all_mp[i]);
        else
          dp2[n - 1] = min(dp2[n - 1], dp2[j] + all_mp[i]);
      }
    }
    long long int ans = INF;
    for (int i = 0; i < n; ++i) {
      if (dp2[i] >= INF)
        continue;
      long long int t = dp2[i];
      for (int j = 0; j < N; ++j) {
        t += dp1[max(0, HP[j] - i)];
      }
      ans = min(ans, t);
    }
    cout << ans << endl;
  }
  return 0;
}