Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <climits>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

typedef long long ll;

int main() {
  ll n, m, s;
  pair<ll, ll> node[51][51];
  ll min_length[51][51];
  pair<ll, ll> city[51];
  cin >> n >> m >> s;
  pair<ll, ll> zero = make_pair(0, 0);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      node[i][j] = zero;
    }
  }
  for (ll i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    // (ginka, minute)
    node[u][v] = make_pair(a, b);
    node[v][u] = make_pair(a, b);
  }
  for (int i = 1; i <= n; i++) {
    ll c, d;
    cin >> c >> d;
    city[i] = make_pair(c, d);
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (node[i][j] != zero) {
        min_length[i][j] = node[i][j].second;
      } else if (i == j) {
        min_length[i][j] = 0;
      } else {
        min_length[i][j] = INT32_MAX;
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (min_length[i][j] > min_length[i][k] + min_length[k][j]) {
          min_length[i][j] = min_length[i][k] + min_length[k][j];
        }
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
                   greater<>>
        q;
    set<pair<ll, ll>> ss;
    // (kitaiti, ginka, position)
    q.push(make_pair(min_length[1][i], make_pair(s, 1)));
    while (!q.empty()) {
      auto current = q.top();
      q.pop();
      ll possession = current.second.first;
      ll position = current.second.second;
      ll expected_time = current.first - min_length[position][i];
      ss.insert(current.second);
      if (position == i) {
        cout << expected_time << endl;
        break;
      }
      for (int j = 1; j <= n; j++) {
        if (node[position][j] != zero &&
            possession >= node[position][j].first) {
          auto state = make_pair(possession - node[position][j].first, j);
          if (ss.find(state) == ss.end()) {
            auto pp = make_pair(expected_time + node[position][j].second +
                                    min_length[j][i],
                                state);
            q.push(pp);
          }
        }
      }
      auto state = make_pair(possession + city[position].first, position);
      if (ss.find(state) == ss.end()) {
        auto pp = make_pair(expected_time + city[position].second +
                                min_length[position][i],
                            state);
        q.push(pp);
      }
    }
  }
}


Fixed submission:
#include <climits>
#include <iostream>
#include <queue>
#include <set>

using namespace std;

typedef long long ll;

int main() {
  ll n, m, s;
  pair<ll, ll> node[51][51];
  ll min_length[51][51];
  pair<ll, ll> city[51];
  cin >> n >> m >> s;
  pair<ll, ll> zero = make_pair(0, 0);
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      node[i][j] = zero;
    }
  }
  for (ll i = 0; i < m; i++) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    // (ginka, minute)
    node[u][v] = make_pair(a, b);
    node[v][u] = make_pair(a, b);
  }
  for (int i = 1; i <= n; i++) {
    ll c, d;
    cin >> c >> d;
    city[i] = make_pair(c, d);
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (node[i][j] != zero) {
        min_length[i][j] = node[i][j].second;
      } else if (i == j) {
        min_length[i][j] = 0;
      } else {
        min_length[i][j] = INT32_MAX;
      }
    }
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (min_length[i][j] > min_length[i][k] + min_length[k][j]) {
          min_length[i][j] = min_length[i][k] + min_length[k][j];
        }
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    priority_queue<pair<ll, pair<ll, ll>>, vector<pair<ll, pair<ll, ll>>>,
                   greater<>>
        q;
    set<pair<ll, ll>> ss;
    // (kitaiti, ginka, position)
    q.push(make_pair(min_length[1][i], make_pair(s, 1)));
    while (!q.empty()) {
      auto current = q.top();
      q.pop();
      ll possession = current.second.first;
      ll position = current.second.second;
      ll expected_time = current.first - min_length[position][i];
      if (ss.find(current.second) != ss.end()) {
        continue;
      }
      ss.insert(current.second);
      if (position == i) {
        cout << expected_time << endl;
        break;
      }
      for (int j = 1; j <= n; j++) {
        if (node[position][j] != zero &&
            possession >= node[position][j].first) {
          auto state = make_pair(possession - node[position][j].first, j);
          if (ss.find(state) == ss.end()) {
            auto pp = make_pair(expected_time + node[position][j].second +
                                    min_length[j][i],
                                state);
            q.push(pp);
          }
        }
      }
      auto state = make_pair(possession + city[position].first, position);
      if (ss.find(state) == ss.end()) {
        auto pp = make_pair(expected_time + city[position].second +
                                min_length[position][i],
                            state);
        q.push(pp);
      }
    }
  }
}