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

#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 110
using namespace std;
ll n, m, s, dp[55][N * N], c[55], d[55], sum = 0;
vector<pair<ll, pair<ll, ll>>> vt[55];
priority_queue<pair<ll, pair<ll, ll>>> pq;
int main() {
  ll i, j, x, y, a, b;
  scanf("%lld%lld%lld", &n, &m, &s);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld%lld", &x, &y, &a, &b);
    sum += a;
    x--;
    y--;
    vt[x].push_back(make_pair(y, make_pair(a, b)));
    vt[y].push_back(make_pair(x, make_pair(a, b)));
  }
  for (i = 0; i < n; i++) {
    scanf("%lld%lld", &c[i], &d[i]);
  }
  memset(dp, 127, sizeof(dp));
  dp[0][min(s, sum)] = 0;
  pq.push(make_pair(0, make_pair((ll)0, min(s, sum))));
  while (!pq.empty()) {
    x = pq.top().S.F;
    a = -pq.top().F;
    b = pq.top().S.S;
    pq.pop();
    if (dp[x][b] < a) {
      continue;
    }
    for (i = 0; i < vt[x].size(); i++) // 走到另外一个地方
    {
      if (b < vt[x][i].S.F) {
        continue;
      }
      y = vt[x][i].F;
      if (dp[y][b - vt[x][i].S.F] > a + vt[x][i].S.S) {
        dp[y][b - vt[x][i].S.F] = a + vt[x][i].S.S;
        pq.push(make_pair(-dp[y][b - vt[x][i].S.F],
                          make_pair(y, b - vt[x][i].S.F)));
      }
    }
    for (i = b + c[x]; i <= sum; i += c[x]) // 兑换银币
    {
      if (dp[x][i] > a + (i - b) / c[x] * d[x]) {
        dp[x][i] = a + (i - b) / c[x] * d[x];
        pq.push(make_pair(-dp[x][i], make_pair(x, i)));
      }
    }
  }
  for (i = 1; i < n; i++) {
    a = LINF;
    for (j = 0; j <= sum; j++) {
      a = min(a, dp[i][j]);
    }
    printf("%lld\\n", a);
  }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define INF 1000000000
#define LINF 1000000000000000000
#define MOD 1000000007
#define mod 1000007
#define INF63 1061109567
#define INF127 9187201950435737471
#define UINF 18446744073709551615
#define F first
#define S second
#define ll long long
#define N 110
using namespace std;
ll n, m, s, dp[55][N * N], c[55], d[55], sum = 0;
vector<pair<ll, pair<ll, ll>>> vt[55];
priority_queue<pair<ll, pair<ll, ll>>> pq;
int main() {
  ll i, j, x, y, a, b;
  scanf("%lld%lld%lld", &n, &m, &s);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld%lld", &x, &y, &a, &b);
    sum += a;
    x--;
    y--;
    vt[x].push_back(make_pair(y, make_pair(a, b)));
    vt[y].push_back(make_pair(x, make_pair(a, b)));
  }
  for (i = 0; i < n; i++) {
    scanf("%lld%lld", &c[i], &d[i]);
  }
  memset(dp, 127, sizeof(dp));
  dp[0][min(s, sum * 2)] = 0;
  pq.push(make_pair(0, make_pair((ll)0, min(s, 2 * sum))));
  while (!pq.empty()) {
    x = pq.top().S.F;
    a = -pq.top().F;
    b = pq.top().S.S;
    pq.pop();
    if (dp[x][b] < a) {
      continue;
    }
    for (i = 0; i < vt[x].size(); i++) // 走到另外一个地方
    {
      if (b < vt[x][i].S.F) {
        continue;
      }
      y = vt[x][i].F;
      if (dp[y][b - vt[x][i].S.F] > a + vt[x][i].S.S) {
        dp[y][b - vt[x][i].S.F] = a + vt[x][i].S.S;
        pq.push(make_pair(-dp[y][b - vt[x][i].S.F],
                          make_pair(y, b - vt[x][i].S.F)));
      }
    }
    for (i = b + c[x]; i <= 2 * sum; i += c[x]) // 兑换银币
    {
      if (dp[x][i] > a + (i - b) / c[x] * d[x]) {
        dp[x][i] = a + (i - b) / c[x] * d[x];
        pq.push(make_pair(-dp[x][i], make_pair(x, i)));
      }
    }
  }
  for (i = 1; i < n; i++) {
    a = LINF;
    for (j = 0; j <= 2 * sum; j++) {
      a = min(a, dp[i][j]);
    }
    printf("%lld\\n", a);
  }
  return 0;
}