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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
#define INF 1000000000

typedef long long ll;
typedef unsigned long long ull;
using namespace std;

int dp[7005][2005], head[7005];
bool inq[7005], mark[7005];
map<string, int> mapp;

struct edge {
  int to, w, next;
} edges[7005];
int em = 0;

void addedge(int u, int v, int w) {
  edges[em].next = head[u];
  edges[em].to = v;
  edges[em].w = w;
  head[u] = em++;
  edges[em].next = head[v];
  edges[em].to = u;
  edges[em].w = w;
  head[v] = em++;
}

int main() {
  int n, m, cap;
  while (~scanf("%d%d%d", &n, &m, &cap) && (n || m || cap)) {
    int cnt = 1;
    em = 0;
    cap *= 10;
    string s, t, tt, mm;
    mapp.clear();
    cin >> s >> t;
    mapp[s] = cnt++; // s:1
    mapp[t] = cnt++; // t:2
    memset(head, -1, sizeof(head));
    while (n--) {
      int w;
      cin >> tt >> mm;
      scanf("%d", &w);
      if (mapp[tt] == 0)
        mapp[tt] = cnt++;
      if (mapp[mm] == 0)
        mapp[mm] = cnt++;
      addedge(mapp[tt], mapp[mm], w);
    }
    memset(mark, false, sizeof(mark));
    while (m--) {
      cin >> tt;
      mark[mapp[tt]] = true;
    }
    queue<int> q;
    memset(inq, false, sizeof(inq));
    for (int i = 0; i <= cnt; i++)
      for (int j = 0; j <= cap; j++)
        dp[i][j] = INF;
    dp[1][cap] = 0;
    q.push(1);
    inq[1] = true;
    //		cout<<1<<endl;
    while (!q.empty()) {
      //			cout<<3<<endl;
      int now = q.front();
      q.pop();
      inq[now] = false;
      if (mark[now])
        for (int i = 0; i < cap; i++)
          dp[now][cap] = min(dp[now][i], dp[now][cap]);
      for (int e = head[now]; e != -1; e = edges[e].next) {
        //				cout<<4<<endl;
        int v = edges[e].to, w = edges[e].w;
        bool flag = false;
        for (int i = cap; i >= 0; i--)
          if (i >= w) {
            if (dp[v][i - w] > dp[now][i] + w) {
              //							cout<<5<<endl;
              dp[v][i - w] = dp[now][i] + w;
              flag = true;
            }
          }
        if (flag && !inq[v]) {
          //					cout<<6<<endl;
          inq[v] = true;
          q.push(v);
        }
      }
    }
    //		cout<<2<<endl;
    int ans = INF;
    for (int i = 0; i <= cap; i++)
      ans = min(dp[2][i], ans);
    if (ans == INF)
      printf("-1\\n");
    else
      printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

/*
諤サ逧?庄莉・蠖堤コウ謌?荳牙?扈?x,y,z):
x:逕ィ謗貞コ剰ァ」蜀ウ
y:逕ィ謗貞コ丞刈cdq蛻?イサ隗」蜀ウ
z:逕ィ譬醍憾謨ー扈?ァ」蜀ウ
*/
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <ios>
#include <iostream>
#include <map>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define wh while
#define inf (int)(~0u / 2)
#define FOR(i, n) for (int i = 0; i < n; i++)
#define FOR1(i, n) for (int i = 1; i < n; i++)
#define FOR2(i, n) for (int i = 0; i <= n; i++)
#define REP(i, n) for (int i = 1; i <= n; i++)
#define FORI(it, n) for (typeof(n.begin()) it = n.begin(); it != n.end(); it++)
#define sf scanf
#define pf printf
#define frs first
#define sec second
#define psh push_back
#define mkp make_pair
#define PB(x) push_back(x)
#define MP(x, y) make_pair(x, y)
#define clr(abc, z) memset(abc, z, sizeof(abc))
#define lt(v) v << 1
#define rt(v) v << 1 | 1
#define mid ((l + r) >> 1)
#define lson l, mid, v << 1
#define rson mid + 1, r, v << 1 | 1

#define fre freopen("1.txt", "r", stdin)

typedef long long LL;
typedef long double LD;

const int INF = 0x3f3f3f3f;
const int maxn = 6030;
int N, M, cap;
int st, ed, curcost;
bool curvis;
struct NODE {
  int to, cost;
};

bool isadd[maxn];
vector<NODE> G[maxn];
vector<NODE> G2[maxn];
char tmpA[20], tmpB[20];
int oil[350];
map<string, int> mp;
void dfs1(int cur, int fa, int curcost) {
  if (cur == fa) {
    if (curvis)
      return;
    else
      curvis = true;
  }
  if (cur != fa) {
    NODE c;
    c.to = cur;
    c.cost = curcost;
    if (!isadd[cur]) {
      G2[fa].PB(c);
      isadd[cur] = true;
    }
  }
  FOR(i, (int)G[cur].size())
  if (G[cur][i].cost + curcost <= cap * 10 && G[cur][i].to != fa)
    dfs1(G[cur][i].to, fa, G[cur][i].cost + curcost);
}

struct HeapNode {
  int d, u;
  bool operator<(const HeapNode &rhs) const { return d > rhs.d; }
};
struct Dijkstra {
  int n;
  void init(int a) { n = a; }
  bool done[maxn];
  int d[maxn];

  void dijkstra(int s) {
    priority_queue<HeapNode> Q;
    REP(i, n) { d[i] = INF; }
    d[s] = 0;
    memset(done, 0, sizeof(done));
    Q.push((HeapNode){0, s});
    while (!Q.empty()) {
      HeapNode x = Q.top();
      Q.pop();
      int u = x.u;
      if (done[u])
        continue;
      done[u] = true;
      for (int i = 0; i < (int)G2[u].size(); i++) {
        int curto = G2[u][i].to;
        int curdist = G2[u][i].cost;
        if (d[curto] > d[u] + curdist) {
          d[curto] = d[u] + curdist;
          Q.push((HeapNode){d[curto], curto});
        }
      }
    }
  }
} djk;
int main() {
  wh(sf("%d%d%d", &N, &M, &cap) != EOF) {
    if (!N && !M && !cap)
      break;
    int cnt = 0;
    mp.clear();
    int oilcnt = 0;
    REP(i, N * 2)
    G[i].clear();
    REP(i, N * 2)
    G2[i].clear();
    sf("%s", tmpA);
    sf("%s", tmpB);
    mp[tmpA] = ++cnt;
    st = mp[tmpA];
    mp[tmpB] = ++cnt;
    ed = mp[tmpB];
    FOR(i, N) {
      sf("%s", tmpA);
      sf("%s", tmpB);
      sf("%d", &curcost);
      if (!mp[tmpA])
        mp[tmpA] = ++cnt;
      if (!mp[tmpB])
        mp[tmpB] = ++cnt;
      NODE nw;
      nw.cost = curcost;
      nw.to = mp[tmpB];
      G[mp[tmpA]].PB(nw);
      nw.to = mp[tmpA];
      G[mp[tmpB]].PB(nw);
    }
    bool exi = false;
    FOR(i, M) {
      sf("%s", tmpA);
      if (mp[tmpA] == st)
        exi = true;
      oil[oilcnt++] = mp[tmpA];
    }
    if (!exi)
      oil[oilcnt++] = st;
    FOR(i, oilcnt) {
      curvis = false;
      clr(isadd, 0);
      dfs1(oil[i], oil[i], 0);
    }
    djk.init(cnt);
    djk.dijkstra(st);
    if (djk.d[ed] == INF)
      pf("-1\\n");
    else {
      pf("%d\\n", djk.d[ed]);
    }
  }
}