Problem p00897 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef priority_queue<P, vector<P>, greater<P>> PQ;

int n, m, cap;
string src, dest;
int st, en;
map<string, int> ma;
string c[2][8000];
int d[8000];
int counter;
vector<P> e[8000];
vector<P> edge[8000];
set<int> sta;

void make_edge() {
  rep(i, n) {
    cin >> c[0][i] >> c[1][i] >> d[i];
    if (ma.count(c[0][i]) == 0) {
      ma[c[0][i]] = counter;
      counter++;
    }
    if (ma.count(c[1][i]) == 0) {
      ma[c[1][i]] = counter;
      counter++;
    }
  }
  st = ma[src];
  en = ma[dest];
  rep(i, n) {
    int a = ma[c[0][i]], b = ma[c[1][i]];
    e[a].push_back(P(b, d[i]));
    e[b].push_back(P(a, d[i]));
  }
}

void dijkstra(int start) {
  int dist[8000];
  PQ que;
  rep(i, 8000) dist[i] = INF;
  dist[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P q = que.top();
    que.pop();
    if (q.first > dist[q.second])
      continue;
    rep(i, e[q.second].size()) {
      P p = e[q.second][i];
      if (dist[p.first] > q.first + p.second &&
          q.first + p.second <= cap * 10) {
        dist[p.first] = q.first + p.second;
        que.push(P(dist[p.first], p.first));
      }
    }
  }
  rep(i, counter) {
    if (i == start)
      continue;
    if (sta.count(i) == 0)
      continue;
    if (dist[i] == INF)
      continue;
    edge[start].push_back(P(i, dist[i]));
  }
}

void dijkstra() {
  int dist[8000];
  PQ que;
  rep(i, 8000) dist[i] = INF;
  dist[st] = 0;
  que.push(P(0, st));
  while (!que.empty()) {
    P q = que.top();
    que.pop();
    if (q.first > dist[q.second])
      continue;
    rep(i, edge[q.second].size()) {
      P p = edge[q.second][i];
      if (dist[p.first] > q.first + p.second) {
        dist[p.first] = q.first + p.second;
        que.push(P(dist[p.first], p.first));
      }
    }
  }
  if (dist[en] == INF)
    dist[en] = -1;
  cout << dist[en] << endl;
}

void solve() {
  ma.clear();
  counter = 0;
  rep(i, 8000) e[i].clear();
  rep(i, 8000) edge[i].clear();
  sta.clear();
  counter = 0;
  cin >> src >> dest;
  make_edge();
  rep(i, m) {
    string tmp;
    cin >> tmp;
    sta.insert(ma[tmp]);
  }
  sta.insert(st);
  sta.insert(en);
  rep(i, counter) {
    if (sta.count(i) == 0)
      continue;
    dijkstra(i);
  }
  dijkstra();
}

int main() {
  while (cin >> n >> m >> cap) {
    if (n == 0 && m == 0 && cap == 0)
      break;
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;
typedef pair<int, int> P;
typedef priority_queue<P, vector<P>, greater<P>> PQ;

int n, m, cap;
string src, dest;
int st, en;
map<string, int> ma;
string c[2][3000];
int d[3000];
int counter;
vector<P> e[3000];
vector<P> edge[3000];
set<int> sta;

void make_edge() {
  rep(i, n) {
    cin >> c[0][i] >> c[1][i] >> d[i];
    if (ma.count(c[0][i]) == 0) {
      ma[c[0][i]] = counter;
      counter++;
    }
    if (ma.count(c[1][i]) == 0) {
      ma[c[1][i]] = counter;
      counter++;
    }
  }
  st = ma[src];
  en = ma[dest];
  rep(i, n) {
    int a = ma[c[0][i]], b = ma[c[1][i]];
    e[a].push_back(P(b, d[i]));
    e[b].push_back(P(a, d[i]));
  }
}

void dijkstra(int start) {
  int dist[3000];
  PQ que;
  rep(i, 3000) dist[i] = INF;
  dist[start] = 0;
  que.push(P(0, start));
  while (!que.empty()) {
    P q = que.top();
    que.pop();
    if (q.first > dist[q.second])
      continue;
    rep(i, e[q.second].size()) {
      P p = e[q.second][i];
      if (dist[p.first] > q.first + p.second &&
          q.first + p.second <= cap * 10) {
        dist[p.first] = q.first + p.second;
        que.push(P(dist[p.first], p.first));
      }
    }
  }
  rep(i, counter) {
    if (i == start)
      continue;
    if (sta.count(i) == 0)
      continue;
    if (dist[i] == INF)
      continue;
    edge[start].push_back(P(i, dist[i]));
  }
}

void dijkstra() {
  int dist[3000];
  PQ que;
  rep(i, 3000) dist[i] = INF;
  dist[st] = 0;
  que.push(P(0, st));
  while (!que.empty()) {
    P q = que.top();
    que.pop();
    if (q.first > dist[q.second])
      continue;
    rep(i, edge[q.second].size()) {
      P p = edge[q.second][i];
      if (dist[p.first] > q.first + p.second) {
        dist[p.first] = q.first + p.second;
        que.push(P(dist[p.first], p.first));
      }
    }
  }
  if (dist[en] == INF)
    dist[en] = -1;
  cout << dist[en] << endl;
}

void solve() {
  ma.clear();
  counter = 0;
  rep(i, 3000) e[i].clear();
  rep(i, 3000) edge[i].clear();
  sta.clear();
  counter = 0;
  cin >> src >> dest;
  make_edge();
  rep(i, m) {
    string tmp;
    cin >> tmp;
    sta.insert(ma[tmp]);
  }
  sta.insert(st);
  sta.insert(en);
  rep(i, counter) {
    if (sta.count(i) == 0)
      continue;
    dijkstra(i);
  }
  dijkstra();
}

int main() {
  while (cin >> n >> m >> cap) {
    if (n == 0 && m == 0 && cap == 0)
      break;
    solve();
  }
}