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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

const int inf = 1000000000;
const int max_v = 6000;
int V;
// map<string, vector<edge> > G1;
vector<edge> G1[max_v];
// map<string, vector<edge> > G2;
vector<edge> G2[max_v];
set<string> allcities;
map<string, int> ind;
int d[max_v];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, inf);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (edge e : G1[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

void dijkstra2(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, inf);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (edge e : G2[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);

  while (true) {
    int n, m, cap;
    cin >> n >> m >> cap;
    if (n == 0 && m == 0 && cap == 0)
      break;
    string src, dest;
    cin >> src >> dest;
    for (int i = 0; i < n; i++) {
      string c1, c2;
      int dis;
      cin >> c1 >> c2 >> dis;
      int i1, i2;
      if (allcities.find(c1) == allcities.end()) {
        i1 = allcities.size();
        allcities.insert(c1);
        ind[c1] = i1;
      } else {
        i1 = ind[c1];
      }
      if (allcities.find(c2) == allcities.end()) {
        i2 = allcities.size();
        allcities.insert(c2);
        ind[c2] = i2;
      } else {
        i2 = ind[c2];
      }
      G1[i1].push_back((edge){i2, dis});
      G1[i2].push_back((edge){i1, dis});
    }
    V = allcities.size();
    vector<int> lpg(m + 2);
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      lpg[i] = ind[s];
    }
    lpg[m] = ind[src];
    lpg[m + 1] = ind[dest];
    for (int i = 0; i < m + 2; i++) {
      dijkstra(lpg[i]);
      for (int j = 0; j < m + 2; j++) {
        if (j == i)
          continue;
        if (d[lpg[j]] <= 10 * cap)
          G2[lpg[i]].push_back((edge){lpg[j], d[lpg[j]]});
      }
    }
    dijkstra2(lpg[m]);
    if (d[lpg[m + 1]] >= inf)
      cout << -1 << endl;
    else
      cout << d[lpg[m + 1]] << endl;
    for (int i = 0; i < V; i++) {
      G1[i].clear();
      G2[i].clear();
    }
    allcities.clear();
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

struct edge {
  string to;
  int cost;
};
typedef pair<int, string> P;

const int inf = 1000000000;
int V;
map<string, vector<edge>> G1;
map<string, vector<edge>> G2;
set<string> allcities;
map<string, int> d;

void dijkstra(string s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (string c : allcities) {
    d[c] = inf;
  }
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    string v = p.second;
    if (d[v] < p.first)
      continue;
    for (edge e : G1[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

void dijkstra2(string s) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (string c : allcities) {
    d[c] = inf;
  }
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    string v = p.second;
    if (d[v] < p.first)
      continue;
    for (edge e : G2[v]) {
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(15);

  while (true) {
    int n, m, cap;
    cin >> n >> m >> cap;
    if (n == 0 && m == 0 && cap == 0)
      break;
    G1.clear();
    G2.clear();
    allcities.clear();
    d.clear();
    string src, dest;
    cin >> src >> dest;
    for (int i = 0; i < n; i++) {
      string c1, c2;
      int dis;
      cin >> c1 >> c2 >> dis;
      G1[c1].push_back((edge){c2, dis});
      G1[c2].push_back((edge){c1, dis});
      allcities.insert(c1);
      allcities.insert(c2);
    }
    vector<string> lpg(m + 2);
    for (int i = 0; i < m; i++) {
      string s;
      cin >> s;
      lpg[i] = s;
    }
    lpg[m] = src;
    lpg[m + 1] = dest;
    for (int i = 0; i < m + 2; i++) {
      dijkstra(lpg[i]);
      for (int j = 0; j < m + 2; j++) {
        if (j == i)
          continue;
        if (d[lpg[j]] <= 10 * cap)
          G2[lpg[i]].push_back((edge){lpg[j], d[lpg[j]]});
      }
    }
    dijkstra2(src);
    if (d[dest] >= inf)
      cout << -1 << endl;
    else
      cout << d[dest] << endl;
  }
}