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

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> e[60];
long long dis[60][2510];
const long long inf = (1ll << 50);
int u[60][2510];
int c[60], d[60];
struct abc {
  int x, y;
  long long v;
  bool operator<(const abc &a) const { return v > a.v; }
};
priority_queue<abc> que;
void push(int x, int y, long long v) {
  abc t;
  t.x = x;
  t.y = y;
  t.v = v;
  que.push(t);
}
void update(int x, int y, long long val) {
  if (dis[x][y] > val) {
    dis[x][y] = val;
    push(x, y, val);
  }
}
int main() {
  int n, m, s, x, y, p, q;
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d%d", &x, &y, &p, &q);
    e[x].push_back(y);
    e[x].push_back(p);
    e[x].push_back(q);
    e[y].push_back(x);
    e[y].push_back(p);
    e[y].push_back(q);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &c[i], &d[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n * 50; j++) {
      dis[i][j] = inf;
      u[i][j] = 0;
    }
  }
  dis[1][min(s, n * 50)] = 0;
  push(1, min(s, n * 50), 0);
  while (1) {
    if (!que.size())
      break;
    abc t = que.top();
    que.pop();
    x = t.x;
    y = t.y;
    if (u[x][y])
      continue;
    u[x][y] = 1;
    update(x, min(n * 50, y + c[x]), dis[x][y] + d[x]);
    for (int i = 0; i < (int)e[x].size(); i += 3) {
      int yy = e[x][i];
      p = e[x][i + 1];
      q = e[x][i + 2];
      if (p <= y) {
        update(yy, y - p, dis[x][y] + q);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    long long r = inf;
    for (int j = 0; j <= n * 50; j++) {
      r = min(r, dis[i][j]);
    }
    printf("%lld\\n", r);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <stdio.h>
#include <vector>
using namespace std;
vector<int> e[60];
long long dis[51][2510];
const long long inf = (1ll << 50);
int u[51][2510];
int c[60], d[60];
void update(long long &x, long long val) {
  if (val < x)
    x = val;
}
int main() {
  int n, m, s, x, y, p, q;
  scanf("%d%d%d", &n, &m, &s);
  for (int i = 0; i < m; i++) {
    scanf("%d%d%d%d", &x, &y, &p, &q);
    e[x].push_back(y);
    e[x].push_back(p);
    e[x].push_back(q);
    e[y].push_back(x);
    e[y].push_back(p);
    e[y].push_back(q);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &c[i], &d[i]);
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= n * 50; j++) {
      dis[i][j] = inf;
      u[i][j] = 0;
    }
  }
  dis[1][min(s, n * 50)] = 0;
  while (1) {
    x = -1;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= n * 50; j++) {
        if (u[i][j])
          continue;
        if (x < 0 || dis[x][y] > dis[i][j]) {
          x = i;
          y = j;
        }
      }
    }
    if (x < 0)
      break;
    u[x][y] = 1;
    update(dis[x][min(n * 50, y + c[x])], dis[x][y] + d[x]);
    for (int i = 0; i < (int)e[x].size(); i += 3) {
      int yy = e[x][i];
      p = e[x][i + 1];
      q = e[x][i + 2];
      if (p <= y) {
        update(dis[yy][y - p], dis[x][y] + q);
      }
    }
  }
  for (int i = 2; i <= n; i++) {
    long long r = inf;
    for (int j = 0; j <= n * 50; j++) {
      r = min(r, dis[i][j]);
    }
    printf("%lld\\n", r);
  }
  return 0;
}