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

#include <bits/stdc++.h>

using namespace std;

#define MAX 6001
#define INF 1e9

struct State {
  int dist, v, cap;
  State(int dist, int v, int cap) : dist(dist), v(v), cap(cap) {}

  bool operator>(const State &s) const { return dist > s.dist; }
};

struct Edge {
  int to, dist;
  Edge(int to, int dist) : to(to), dist(dist) {}
};

int N, M, cap, src, dst, idx;
set<int> LPG;
vector<Edge> G[MAX];

void init() {
  idx = 1;
  LPG.clear();
  for (int i = 0; i < MAX; i++) {
    G[i].clear();
  }
}

int dijkstra() {
  vector<int> dist[idx];
  cap *= 10;

  for (int i = 0; i < idx; i++) {
    dist[i].resize(cap + 1);
    for (int j = 0; j <= cap; j++) {
      dist[i][j] = INF;
    }
  }

  dist[src][cap] = 0;

  priority_queue<State, vector<State>, greater<State>> Q;
  Q.push(State(0, src, cap));

  while (!Q.empty()) {
    State s = Q.top();
    Q.pop();
    int v = s.v, c = s.cap;

    if (dist[v][c] < s.dist)
      continue;

    if (v == dst) {
      return dist[v][c];
    }

    for (int i = 0; i < (int)G[v].size(); i++) {
      Edge e = G[v][i];
      int nc = (LPG.find(v) != LPG.end()) ? cap : c;
      if (e.dist <= nc) {
        nc -= e.dist;
        if (dist[v][c] + e.dist < dist[e.to][nc]) {
          dist[e.to][nc] = dist[v][c] + e.dist;
          Q.push(State(dist[e.to][nc], e.to, nc));
        }
      }
    }
  }
  return -1;
}

int main() {
  int d;
  string a, b;

  while (cin >> N >> M >> cap, N) {
    init();
    map<string, int> mp;
    cin >> a;
    mp[a] = src = idx++;
    cin >> a;
    mp[a] = dst = idx++;
    for (int i = 0; i < N; i++) {
      cin >> a >> b >> d;
      if (mp.find(a) == mp.end()) {
        mp[a] = idx++;
      }
      if (mp.find(b) == mp.end()) {
        mp[b] = idx++;
      }
      G[mp[a]].push_back(Edge(mp[b], d));
      G[mp[b]].push_back(Edge(mp[a], d));
    }
    for (int i = 0; i < M; i++) {
      cin >> a;
      LPG.insert(mp[a]);
    }
    cout << dijkstra() << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
map<string, int> ind;
map<string, bool> exi;
bool gas[3333];
bool can;
int no = 0;
vector<int> route[3333];
vector<int> roulen[3333];
int n, m, cap, lo;
int ncap;
string start, dest, t1, t2;
int destid;
int ans = INT_MAX;
void cp(bool *a, bool *b) {
  for (int i = 0; i < 3333; i++)
    b[i] = a[i];
}
void dfs(int now, int gass, int ta, bool *v) {
  if (gass < 0 || v[now])
    return;
  if (gas[now])
    gass = cap;
  // cout<<now<<" "<<gass<<" "<<ta<<"\\n";
  if (now == destid) {
    ans = min(ans, ta);
    return;
  }
  v[now] = true;
  bool t[3333];
  for (int i = 0; i < route[now].size(); i++) {
    cp(v, t);
    dfs(route[now][i], gass - roulen[now][i], ta + roulen[now][i], t);
  }
}
void solve() {
  cap *= 10;
  ncap = cap;
  ind.clear();
  ans = INT_MAX;
  for (int i = 0; i > 3333; i++) {
    route[i].clear();
    roulen[i].clear();
  }
  bool v[3333];
  memset(v, 0, sizeof(v));
  dfs(0, ncap, 0, v);
  if (ans != INT_MAX)
    cout << ans << "\\n";
  else
    cout << "-1\\n";
}
int main() {
  while (cin >> n >> m >> cap && n + m + cap > 0) {
    memset(gas, 0, sizeof(gas));
    cin >> start >> dest;
    if (!exi[start]) {
      ind[start] = no++;
      exi[start] = true;
    }
    if (!exi[dest]) {
      ind[dest] = no++;
      exi[dest] = true;
    }
    destid = no - 1;
    while (n--) {
      cin >> t1 >> t2 >> lo;
      if (!exi[t1]) {
        ind[t1] = no++;
        exi[t1] = true;
      }
      if (!exi[t2]) {
        ind[t2] = no++;
        exi[t2] = true;
      }
      route[ind[t1]].push_back(ind[t2]);
      roulen[ind[t1]].push_back(lo);
      route[ind[t2]].push_back(ind[t1]);
      roulen[ind[t2]].push_back(lo);
    }
    while (m--) {
      cin >> t1;
      gas[ind[t1]] = true;
    }
    solve();
  }
}