Problem p03634 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define pb push_back
#define mp make_pair
#define ll long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ull unsigned long long
#define iloop(a, b) for (ll i = a; i < b; ++i)
#define jloop(a, b) for (ll j = a; j < b; ++j)
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeofa((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define p(a) cout << a << "\n";
#define p1(a) cout << a << " ";

#define PI 3.141592653589793
#define MOD (ll)(1e9 + 7)

using namespace std;
static struct IO {
  char tmp[1 << 10];

  // fast input routines
  char cur;

  // #define nextChar() (cur = getc_unlocked(stdin))
  // #define peekChar() (cur)
  inline char nextChar() { return cur = getc_unlocked(stdin); }
  inline char peekChar() { return cur; }

  inline operator bool() { return peekChar(); }
  inline static bool isBlank(char c) { return (c < '-' && c); }
  inline bool skipBlanks() {
    while (isBlank(nextChar()))
      ;
    return peekChar() != 0;
  }

  inline IO &operator>>(char &c) {
    c = nextChar();
    return *this;
  }

  inline IO &operator>>(char *buf) {
    if (skipBlanks()) {
      if (peekChar()) {
        *(buf++) = peekChar();
        while (!isBlank(nextChar()))
          *(buf++) = peekChar();
      }
      *(buf++) = 0;
    }
    return *this;
  }

  inline IO &operator>>(string &s) {
    if (skipBlanks()) {
      s.clear();
      s += peekChar();
      while (!isBlank(nextChar()))
        s += peekChar();
    }
    return *this;
  }

  inline IO &operator>>(double &d) {
    if ((*this) >> tmp)
      sscanf(tmp, "%lf", &d);
    return *this;
  }

#define defineInFor(intType)                                                   \
  inline IO &operator>>(intType &n) {                                          \
    if (skipBlanks()) {                                                        \
      int sign = +1;                                                           \
      if (peekChar() == '-') {                                                 \
        sign = -1;                                                             \
        n = nextChar() - '0';                                                  \
      } else                                                                   \
        n = peekChar() - '0';                                                  \
      while (!isBlank(nextChar())) {                                           \
        n += n + (n << 3) + peekChar() - 48;                                   \
      }                                                                        \
      n *= sign;                                                               \
    }                                                                          \
    return *this;                                                              \
  }

  defineInFor(int) defineInFor(unsigned int) defineInFor(long long)

      // fast output routines

      // #define putChar(c) putc_unlocked((c), stdout)
      inline void putChar(char c) {
    putc_unlocked(c, stdout);
  }
  inline IO &operator<<(char c) {
    putChar(c);
    return *this;
  }
  inline IO &operator<<(const char *s) {
    while (*s)
      putChar(*s++);
    return *this;
  }

  inline IO &operator<<(const string &s) {
    for (int i = 0; i < (int)s.size(); ++i)
      putChar(s[i]);
    return *this;
  }

  char *toString(double d) {
    sprintf(tmp, "%lf%c", d, '\0');
    return tmp;
  }
  inline IO &operator<<(double d) { return (*this) << toString(d); }

#define defineOutFor(intType)                                                  \
  inline char *toString(intType n) {                                           \
    char *p = (tmp + 30);                                                      \
    if (n) {                                                                   \
      bool isNeg = 0;                                                          \
      if (n < 0)                                                               \
        isNeg = 1, n = -n;                                                     \
      while (n)                                                                \
        *--p = (n % 10) + '0', n /= 10;                                        \
      if (isNeg)                                                               \
        *--p = '-';                                                            \
    } else                                                                     \
      *--p = '0';                                                              \
    return p;                                                                  \
  }                                                                            \
  inline IO &operator<<(intType n) { return (*this) << toString(n); }

  defineOutFor(int) defineOutFor(long long)

#define endl ('\n')
#define cout __io__
#define cin __io__
} __io__;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;

ll n;
vector<pair<ll, ll>> vec[100000];
vector<ll> dist(100000, LLONG_MAX);
void shortestPath(ll s) {
  set<pair<ll, ll>> st;

  st.insert(mp(0, s));
  dist[s] = 0;
  while (!st.empty()) {
    pair<ll, ll> p = *(st.begin());
    st.erase(st.begin());
    ll u = p.second;
    for (auto i = vec[u].begin(); i != vec[u].end(); ++i) {
      ll v = (*i).first;
      ll weight = (*i).second;
      if (dist[v] > dist[u] + weight) {
        if (dist[v] != LLONG_MAX) {
          st.erase(st.find(mp(dist[v], v)));
        }
        dist[v] = dist[u] + weight;
        st.insert(mp(dist[v], v));
      }
    }
  }
}
void solve() {
  cin >> n;
  ll a, b, c;
  iloop(0, n - 1) {
    cin >> a >> b >> c;
    vec[a].pb(mp(b, c));
    vec[b].pb(mp(a, c));
  }
  ll q, k;
  cin >> q >> k;
  shortestPath(k);
  iloop(0, q) {
    cin >> a >> b;
    p(dist[a] + dist[b])
  }
}
int main() {
  bool testing = false;
  // IOS
  if (testing) {
    freopen("test.txt", "rt", stdin);
    int start = clock();
    solve();
    int end = clock();
    cout << "time: " << (end - start) / (double)(CLOCKS_PER_SEC)*1000
         << " milliseconds\n";
  } else {
    solve();
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define pb push_back
#define mp make_pair
#define ll long long
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ull unsigned long long
#define iloop(a, b) for (ll i = a; i < b; ++i)
#define jloop(a, b) for (ll j = a; j < b; ++j)
#define sz(a) int((a).size())
#define pb push_back
#define all(c) (c).begin(), (c).end()
#define tr(c, i) for(typeofa((c).begin() i = (c).begin(); i != (c).end(); i++)
#define present(c, x) ((c).find(x) != (c).end())
#define cpresent(c, x) (find(all(c), x) != (c).end())
#define p(a) cout << a << "\n";
#define p1(a) cout << a << " ";

#define PI 3.141592653589793
#define MOD (ll)(1e9 + 7)

using namespace std;
static struct IO {
  char tmp[1 << 10];

  // fast input routines
  char cur;

  // #define nextChar() (cur = getc_unlocked(stdin))
  // #define peekChar() (cur)
  inline char nextChar() { return cur = getc_unlocked(stdin); }
  inline char peekChar() { return cur; }

  inline operator bool() { return peekChar(); }
  inline static bool isBlank(char c) { return (c < '-' && c); }
  inline bool skipBlanks() {
    while (isBlank(nextChar()))
      ;
    return peekChar() != 0;
  }

  inline IO &operator>>(char &c) {
    c = nextChar();
    return *this;
  }

  inline IO &operator>>(char *buf) {
    if (skipBlanks()) {
      if (peekChar()) {
        *(buf++) = peekChar();
        while (!isBlank(nextChar()))
          *(buf++) = peekChar();
      }
      *(buf++) = 0;
    }
    return *this;
  }

  inline IO &operator>>(string &s) {
    if (skipBlanks()) {
      s.clear();
      s += peekChar();
      while (!isBlank(nextChar()))
        s += peekChar();
    }
    return *this;
  }

  inline IO &operator>>(double &d) {
    if ((*this) >> tmp)
      sscanf(tmp, "%lf", &d);
    return *this;
  }

#define defineInFor(intType)                                                   \
  inline IO &operator>>(intType &n) {                                          \
    if (skipBlanks()) {                                                        \
      int sign = +1;                                                           \
      if (peekChar() == '-') {                                                 \
        sign = -1;                                                             \
        n = nextChar() - '0';                                                  \
      } else                                                                   \
        n = peekChar() - '0';                                                  \
      while (!isBlank(nextChar())) {                                           \
        n += n + (n << 3) + peekChar() - 48;                                   \
      }                                                                        \
      n *= sign;                                                               \
    }                                                                          \
    return *this;                                                              \
  }

  defineInFor(int) defineInFor(unsigned int) defineInFor(long long)

      // fast output routines

      // #define putChar(c) putc_unlocked((c), stdout)
      inline void putChar(char c) {
    putc_unlocked(c, stdout);
  }
  inline IO &operator<<(char c) {
    putChar(c);
    return *this;
  }
  inline IO &operator<<(const char *s) {
    while (*s)
      putChar(*s++);
    return *this;
  }

  inline IO &operator<<(const string &s) {
    for (int i = 0; i < (int)s.size(); ++i)
      putChar(s[i]);
    return *this;
  }

  char *toString(double d) {
    sprintf(tmp, "%lf%c", d, '\0');
    return tmp;
  }
  inline IO &operator<<(double d) { return (*this) << toString(d); }

#define defineOutFor(intType)                                                  \
  inline char *toString(intType n) {                                           \
    char *p = (tmp + 30);                                                      \
    if (n) {                                                                   \
      bool isNeg = 0;                                                          \
      if (n < 0)                                                               \
        isNeg = 1, n = -n;                                                     \
      while (n)                                                                \
        *--p = (n % 10) + '0', n /= 10;                                        \
      if (isNeg)                                                               \
        *--p = '-';                                                            \
    } else                                                                     \
      *--p = '0';                                                              \
    return p;                                                                  \
  }                                                                            \
  inline IO &operator<<(intType n) { return (*this) << toString(n); }

  defineOutFor(int) defineOutFor(long long)

#define endl ('\n')
#define cout __io__
#define cin __io__
} __io__;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> ii;

ll n;
vector<pair<ll, ll>> vec[100001];
vector<ll> dist(100001, LLONG_MAX);
void shortestPath(ll s) {
  set<pair<ll, ll>> st;

  st.insert(mp(0, s));
  dist[s] = 0;
  while (!st.empty()) {
    pair<ll, ll> p = *(st.begin());
    st.erase(st.begin());
    ll u = p.second;
    for (auto i = vec[u].begin(); i != vec[u].end(); ++i) {
      ll v = (*i).first;
      ll weight = (*i).second;
      if (dist[v] > dist[u] + weight) {
        if (dist[v] != LLONG_MAX) {
          st.erase(st.find(mp(dist[v], v)));
        }
        dist[v] = dist[u] + weight;
        st.insert(mp(dist[v], v));
      }
    }
  }
}
void solve() {
  cin >> n;
  ll a, b, c;
  iloop(0, n - 1) {
    cin >> a >> b >> c;
    vec[a].pb(mp(b, c));
    vec[b].pb(mp(a, c));
  }
  ll q, k;
  cin >> q >> k;
  shortestPath(k);
  iloop(0, q) {
    cin >> a >> b;
    p(dist[a] + dist[b])
  }
}
int main() {
  bool testing = false;
  // IOS
  if (testing) {
    freopen("test.txt", "rt", stdin);
    int start = clock();
    solve();
    int end = clock();
    cout << "time: " << (end - start) / (double)(CLOCKS_PER_SEC)*1000
         << " milliseconds\n";
  } else {
    solve();
  }
}