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

#include <bits/stdc++.h>
using namespace std;

long long N, M, S;

// to (ginka, cost)
vector<pair<long long, pair<long long, long long>>> tree[100];

long long c[200];
long long d[200];
map<long long, long long> ans;

int main() {
  cin >> N >> M >> S;
  for (long long i = 0; i < M; i++) {
    long long u;
    long long v;
    long long a;
    long long b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    tree[u].push_back(make_pair(v, make_pair(a, b)));
    tree[v].push_back(make_pair(u, make_pair(a, b)));
  }

  for (long long i = 0; i < N; i++) {
    cin >> c[i] >> d[i];
  }

  // cost, (point, ginka)
  priority_queue<pair<long long, pair<long long, long long>>,
                 vector<pair<long long, pair<long long, long long>>>,
                 greater<pair<long long, pair<long long, long long>>>>
      Q;

  Q.push(make_pair(0, make_pair(0, S)));

  //          point, ginka
  map<pair<long long, long long>, long long> memo;
  while (!Q.empty()) {
    long long cost = Q.top().first;
    long long point = Q.top().second.first;
    long long ginka = Q.top().second.second;

    // cout << " point " << point << "ginka " << ginka << " cost " << cost <<
    // endl;

    if (ans.count(point) == 0) {
      ans[point] = cost;
      if (ans.size() == N) {
        break;
      }
    }

    Q.pop();

    if (memo.count(make_pair(point, ginka)) > 0) {
      continue;
    }

    memo[make_pair(point, ginka)] = cost;

    for (long long i = 0; i < tree[point].size(); i++) {
      long long nextpoint = tree[point][i].first;
      long long nextginka = tree[point][i].second.first;
      long long nextcost = tree[point][i].second.second;
      // 銀貨を使って次のcityにいく
      if (ginka >= nextginka) {
        Q.push(make_pair(cost + nextcost,
                         make_pair(nextpoint, ginka - nextginka)));
      }
    }

    // 金貨を銀貨に交換する
    if (ginka < 300) {
      Q.push(make_pair(cost + d[point], make_pair(point, ginka + c[point])));
    }
  }

  for (long long i = 1; i < N; i++) {
    // cout << "ans[" << i << "=" << ans[i] << endl;
    cout << ans[i] << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long N, M, S;

// to (ginka, cost)
vector<pair<long long, pair<long long, long long>>> tree[100];

long long c[200];
long long d[200];
map<long long, long long> ans;

int main() {
  cin >> N >> M >> S;
  for (long long i = 0; i < M; i++) {
    long long u;
    long long v;
    long long a;
    long long b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    tree[u].push_back(make_pair(v, make_pair(a, b)));
    tree[v].push_back(make_pair(u, make_pair(a, b)));
  }

  for (long long i = 0; i < N; i++) {
    cin >> c[i] >> d[i];
  }

  // cost, (point, ginka)
  priority_queue<pair<long long, pair<long long, long long>>,
                 vector<pair<long long, pair<long long, long long>>>,
                 greater<pair<long long, pair<long long, long long>>>>
      Q;

  Q.push(make_pair(0, make_pair(0, S)));

  //          point, ginka
  map<pair<long long, long long>, long long> memo;
  while (!Q.empty()) {
    long long cost = Q.top().first;
    long long point = Q.top().second.first;
    long long ginka = Q.top().second.second;

    // cout << " point " << point << "ginka " << ginka << " cost " << cost <<
    // endl;

    if (ans.count(point) == 0) {
      ans[point] = cost;
      if (ans.size() == N) {
        break;
      }
    }

    Q.pop();

    if (memo.count(make_pair(point, ginka)) > 0) {
      continue;
    }

    memo[make_pair(point, ginka)] = cost;

    for (long long i = 0; i < tree[point].size(); i++) {
      long long nextpoint = tree[point][i].first;
      long long nextginka = tree[point][i].second.first;
      long long nextcost = tree[point][i].second.second;
      // 銀貨を使って次のcityにいく
      if (ginka >= nextginka) {
        Q.push(make_pair(cost + nextcost,
                         make_pair(nextpoint, ginka - nextginka)));
      }
    }

    // 金貨を銀貨に交換する
    Q.push(make_pair(cost + d[point], make_pair(point, ginka + c[point])));
  }

  for (long long i = 1; i < N; i++) {
    // cout << "ans[" << i << "=" << ans[i] << endl;
    cout << ans[i] << endl;
  }
}