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

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N, M, S;
// i->jに運賃a、b分でいけるなら
// 集合adj[i]は要素として(j,a,b)を持つ
vector<vector<tuple<LL, LL, LL>>> adj;
LL C[51], D[51];

LL ans[51];

void solve() {
  S = min(S, 2500LL);
  // ダイクストラ
  // tuple<コスト、残り銀貨、頂点番号>
  priority_queue<tuple<LL, LL, LL>, vector<tuple<LL, LL, LL>>,
                 greater<tuple<LL, LL, LL>>>
      q;
  // 頂点iに、残り銀貨j枚で最短k分で行けるならcost[i][j] = k;
  vector<vector<LL>> cost(N + 1, vector<LL>(2501, INF));
  cost[1][S] = 0;
  q.push(tuple<LL, LL, LL>(0, S, 1));
  while (!q.empty()) {
    LL now_cost = get<0>(q.top());
    LL remain = get<1>(q.top());
    LL now_vertex = get<2>(q.top());
    q.pop();
    if (cost[now_vertex][remain] < now_cost)
      continue;
    if (remain + C[now_vertex] <= 2500) {
      if (cost[now_vertex][remain + C[now_vertex]] >
          cost[now_vertex][remain] + D[now_vertex]) {
        cost[now_vertex][remain + C[now_vertex]] =
            cost[now_vertex][remain] + D[now_vertex];
        q.push(tuple<LL, LL, LL>(cost[now_vertex][remain + C[now_vertex]],
                                 remain + C[now_vertex], now_vertex));
      }
    }
    for (auto next : adj[now_vertex]) {
      LL cost_required = get<2>(next);
      LL silver_required = get<1>(next);
      LL next_vertex = get<0>(next);
      if (remain - silver_required < 0)
        continue;
      if (cost[next_vertex][remain - silver_required] <=
          cost[now_vertex][remain] + cost_required)
        continue;
      cost[next_vertex][remain - silver_required] =
          cost[now_vertex][remain] + cost_required;
      q.push(tuple<LL, LL, LL>(cost[next_vertex][remain - silver_required],
                               remain - silver_required, next_vertex));
    }
  }

  for (int i = 2; i <= N; i++) {
    ans[i] = INF;
    for (int j = 0; j <= 2500; j++) {
      ans[i] = min(ans[i], cost[i][j]);
    }
  }
}

int main() {
  cin >> N >> M >> S;
  adj = vector<vector<tuple<LL, LL, LL>>>(N + 1);
  for (int i = 0; i < M; i++) {
    LL u, v, a, b;
    cin >> u >> v >> a >> b;
    adj[u].push_back(tuple<LL, LL, LL>(v, a, b));
    adj[v].push_back(tuple<LL, LL, LL>(u, a, b));
  }
  for (int i = 1; i <= N; i++)
    cin >> C[i] >> D[i];

  solve();

  for (int i = 2; i <= N; i++)
    cout << ans[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

// g++ -std=c++14 test.cpp -o test.out

#include <algorithm>
#include <bitset>
#include <cassert>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <unordered_map>
#include <utility>
#include <vector>
using namespace std;

#define LL long long int
const LL INF = (1LL << 60);
const int INF_INT = 2147483647 - 1e6 - 1;
const LL mod = 1000000007ll;
const int mod_int = 1000000007;

LL N, M, S;
// i->jに運賃a、b分でいけるなら
// 集合adj[i]は要素として(j,a,b)を持つ
vector<vector<tuple<LL, LL, LL>>> adj;
LL C[51], D[51];

LL ans[51];

void solve() {
  S = min(S, 2500LL);
  // ダイクストラ
  // tuple<コスト、残り銀貨、頂点番号>
  priority_queue<tuple<LL, LL, LL>, vector<tuple<LL, LL, LL>>,
                 greater<tuple<LL, LL, LL>>>
      q;
  // 頂点iに、残り銀貨j枚で最短k分で行けるならcost[i][j] = k;
  vector<vector<LL>> cost(N + 1, vector<LL>(2501, INF));
  cost[1][S] = 0;
  q.push(tuple<LL, LL, LL>(0, S, 1));
  while (!q.empty()) {
    LL now_cost = get<0>(q.top());
    LL remain = get<1>(q.top());
    LL now_vertex = get<2>(q.top());
    q.pop();
    if (cost[now_vertex][remain] < now_cost)
      continue;
    if (remain + C[now_vertex] <= 2500) {
      if (cost[now_vertex][remain + C[now_vertex]] >
          cost[now_vertex][remain] + D[now_vertex]) {
        cost[now_vertex][remain + C[now_vertex]] =
            cost[now_vertex][remain] + D[now_vertex];
        q.push(tuple<LL, LL, LL>(cost[now_vertex][remain + C[now_vertex]],
                                 remain + C[now_vertex], now_vertex));
      }
    }
    for (tuple<LL, LL, LL> next : adj[now_vertex]) {
      LL cost_required = get<2>(next);
      LL silver_required = get<1>(next);
      LL next_vertex = get<0>(next);
      if (remain - silver_required < 0)
        continue;
      if (cost[next_vertex][remain - silver_required] <
          cost[now_vertex][remain] + cost_required)
        continue;
      cost[next_vertex][remain - silver_required] =
          cost[now_vertex][remain] + cost_required;
      q.push(tuple<LL, LL, LL>(cost[next_vertex][remain - silver_required],
                               remain - silver_required, next_vertex));
    }
  }

  for (int i = 2; i <= N; i++) {
    ans[i] = INF;
    for (int j = 0; j <= 2500; j++) {
      ans[i] = min(ans[i], cost[i][j]);
    }
  }
}

int main() {
  cin >> N >> M >> S;
  adj = vector<vector<tuple<LL, LL, LL>>>(N + 1);
  for (int i = 0; i < M; i++) {
    LL u, v, a, b;
    cin >> u >> v >> a >> b;
    adj[u].push_back(tuple<LL, LL, LL>(v, a, b));
    adj[v].push_back(tuple<LL, LL, LL>(u, a, b));
  }
  for (int i = 1; i <= N; i++)
    cin >> C[i] >> D[i];

  solve();

  for (int i = 2; i <= N; i++)
    cout << ans[i] << endl;
  return 0;
}