Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int n, m, s, d[505], sum;
int v[505], a[505], nxt[505], head[505];
long long dis[55][500005], ans[55], b[505], c[505];
bool vis[55][500005];

void spfa() {
  queue<pair<int, int>> q;
  q.push(make_pair(1, s));
  while (!q.empty()) {
    int u = q.front().first, x = q.front().second;
    q.pop(), vis[u][x] = 0;
    //		cout<<u<<' '<<x<<' '<<dis[u][x]<<endl;
    ans[u] = min(ans[u], dis[u][x]);
    for (int i = x + d[u], j = 1; i < sum + d[u]; i += d[u], j++) {
      if (i > sum)
        i = sum;
      if (dis[u][i] > dis[u][x] + c[u] * j) {
        dis[u][i] = dis[u][x] + c[u] * j;
        if (!vis[u][i]) {
          vis[u][i] = 1, q.push(make_pair(u, i));
        }
      } else
        break;
      if (i == sum)
        break;
    }
    for (int i = head[u]; i > 0; i = nxt[i]) {
      if (x < a[i])
        continue;
      if (dis[v[i]][x - a[i]] > dis[u][x] + b[i]) {
        //				cout<<' '<<v[i]<<' '<<x-a[i]<<'
        //'<<dis[u][x]+b[i]<<' '<<b[i]<<endl;
        dis[v[i]][x - a[i]] = dis[u][x] + b[i];
        if (!vis[v[i]][x - a[i]]) {
          vis[v[i]][x - a[i]] = 1;
          q.push(make_pair(v[i], x - a[i]));
        }
      }
    }
  }
}

int main() {
  memset(ans, 0x3f, sizeof(ans));
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &v[i], &v[i + m]);
    nxt[i] = head[v[i + m]], head[v[i + m]] = i;
    nxt[i + m] = head[v[i]], head[v[i]] = i + m;
    scanf("%d%d", &a[i], &b[i]);
    a[i + m] = a[i], b[i + m] = b[i];
    sum += a[i];
  }
  sum = sum * n * 2, s = min(s, sum);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &d[i], &c[i]);
    d[i] = min(d[i], sum);
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[1][s] = 0, vis[1][s] = 1, spfa();
  for (int i = 2; i <= n; i++)
    printf("%lld\n", ans[i]);
}

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

int n, m, s, d[505], sum;
int v[505], a[505], nxt[505], head[505];
long long dis[55][500005], ans[55], b[505], c[505];
bool vis[55][500005];

void spfa() {
  queue<pair<int, int>> q;
  q.push(make_pair(1, s));
  while (!q.empty()) {
    int u = q.front().first, x = q.front().second;
    q.pop(), vis[u][x] = 0;
    //		cout<<u<<' '<<x<<' '<<dis[u][x]<<endl;
    ans[u] = min(ans[u], dis[u][x]);
    for (int i = x + d[u], j = 1; i < sum + d[u]; i += d[u], j++) {
      if (i > sum)
        i = sum;
      if (dis[u][i] > dis[u][x] + c[u] * j) {
        dis[u][i] = dis[u][x] + c[u] * j;
        if (!vis[u][i]) {
          vis[u][i] = 1, q.push(make_pair(u, i));
        }
      } else
        break;
      if (i == sum)
        break;
    }
    for (int i = head[u]; i > 0; i = nxt[i]) {
      if (x < a[i])
        continue;
      if (dis[v[i]][x - a[i]] > dis[u][x] + b[i]) {
        //				cout<<' '<<v[i]<<' '<<x-a[i]<<'
        //'<<dis[u][x]+b[i]<<' '<<b[i]<<endl;
        dis[v[i]][x - a[i]] = dis[u][x] + b[i];
        if (!vis[v[i]][x - a[i]]) {
          vis[v[i]][x - a[i]] = 1;
          q.push(make_pair(v[i], x - a[i]));
        }
      }
    }
  }
}

int main() {
  memset(ans, 0x3f, sizeof(ans));
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 1; i <= m; i++) {
    scanf("%d%d", &v[i], &v[i + m]);
    nxt[i] = head[v[i + m]], head[v[i + m]] = i;
    nxt[i + m] = head[v[i]], head[v[i]] = i + m;
    scanf("%d%d", &a[i], &b[i]);
    a[i + m] = a[i], b[i + m] = b[i];
    sum += a[i];
  }
  sum = sum * 2, s = min(s, sum);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &d[i], &c[i]);
    d[i] = min(d[i], sum);
  }
  memset(dis, 0x3f, sizeof(dis));
  dis[1][s] = 0, vis[1][s] = 1, spfa();
  for (int i = 2; i <= n; i++)
    printf("%lld\n", ans[i]);
}