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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n || m || cap) {
    map<string, int> ms;
    string src, dst;
    cin >> src >> dst;
    ms[src] = 0;
    ms[dst] = 1;
    vector<P> G[10000];
    for (int i = 0; i < n; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      if (!ms.count(a)) {
        int k = ms.size();
        ms[a] = k;
      }
      if (!ms.count(b)) {
        int k = ms.size();
        ms[b] = k;
      }
      G[ms[a]].push_back(P(ms[b], c));
      G[ms[b]].push_back(P(ms[a], c));
    }

    int v = ms.size(), inf = 1 << 25;
    int dp[m + 2][v];
    vector<string> vs(m + 2);
    for (int i = 0; i < m + 2; i++) {
      for (int j = 0; j < v; j++)
        dp[i][j] = inf;
      if (i < m)
        cin >> vs[i];
      else if (i == m)
        vs[i] = src;
      else
        vs[i] = dst;
      int u = ms[vs[i]];
      priority_queue<P> q;
      dp[i][u] = 0;
      q.push(P(0, u));
      while (!q.empty()) {
        P p = q.top();
        q.pop();
        int x = p.second;
        if (dp[i][x] < -p.first)
          continue;
        for (P y : G[x]) {
          if (dp[i][x] + y.second < dp[i][y.first]) {
            dp[i][y.first] = dp[i][x] + y.second;
            q.push(P(-dp[i][y.first], y.first));
          }
        }
      }
    }

    int e[m + 2][m + 2];
    for (int i = 0; i < m + 2; i++)
      for (int j = 0; j < m + 2; j++)
        e[i][j] = inf * (i != j);

    for (int i = 0; i < m + 2; i++)
      for (int j = 0; j < m + 2; j++)
        if (dp[i][ms[vs[j]]] <= cap * 10)
          e[i][j] = dp[i][ms[vs[j]]];

    for (int k = 0; k < m + 2; k++)
      for (int i = 0; i < m + 2; i++)
        for (int j = 0; j < m + 2; j++)
          e[i][j] = min(e[i][j], e[i][k] + e[k][j]);

    cout << (e[m][m + 1] < inf ? e[m][m + 1] : -1) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n, m, cap;
  while (cin >> n >> m >> cap, n || m || cap) {
    map<string, int> ms;
    string src, dst;
    cin >> src >> dst;
    ms[src] = 0;
    ms[dst] = 1;
    vector<P> G[10000];
    for (int i = 0; i < n; i++) {
      string a, b;
      int c;
      cin >> a >> b >> c;
      if (!ms.count(a))
        ms[a] = ms.size();
      if (!ms.count(b))
        ms[b] = ms.size();
      G[ms[a]].push_back(P(ms[b], c));
      G[ms[b]].push_back(P(ms[a], c));
    }

    int v = ms.size(), inf = 1 << 25;
    int dp[m + 2][v];
    vector<string> vs(m + 2);
    for (int i = 0; i < m + 2; i++) {
      for (int j = 0; j < v; j++)
        dp[i][j] = inf;
      if (i < m)
        cin >> vs[i];
      else if (i == m)
        vs[i] = src;
      else
        vs[i] = dst;
      int u = ms[vs[i]];
      priority_queue<P> q;
      dp[i][u] = 0;
      q.push(P(0, u));
      while (!q.empty()) {
        P p = q.top();
        q.pop();
        int x = p.second;
        if (dp[i][x] < -p.first)
          continue;
        for (P y : G[x]) {
          if (dp[i][x] + y.second < dp[i][y.first]) {
            dp[i][y.first] = dp[i][x] + y.second;
            q.push(P(-dp[i][y.first], y.first));
          }
        }
      }
    }

    int e[m + 2][m + 2];
    for (int i = 0; i < m + 2; i++)
      for (int j = 0; j < m + 2; j++)
        e[i][j] = inf * (i != j);

    for (int i = 0; i < m + 2; i++)
      for (int j = 0; j < m + 2; j++)
        if (dp[i][ms[vs[j]]] <= cap * 10)
          e[i][j] = e[j][i] = dp[i][ms[vs[j]]];

    for (int k = 0; k < m + 2; k++)
      for (int i = 0; i < m + 2; i++)
        for (int j = 0; j < m + 2; j++)
          e[i][j] = min(e[i][j], e[i][k] + e[k][j]);

    cout << (e[m + 1][m] < inf ? e[m + 1][m] : -1) << endl;
  }
  return 0;
}