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

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i, a, b) for (in i = (in)(a); i < (in)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const in inf = 1e9 + 7;
using P = pair<in, in>;
vector<in> dx = {0, 1, -1, 0};
vector<in> dy = {1, 0, 0, -1};

using T = tuple<in, in, in, in>;

struct edge {
  in to;
  in cost;
};
vector<vector<edge>> graph(0);
vector<in> d(0);
const in MAX_dist = INF;

void init(in n) {
  graph.resize(n);
  d.resize(n, MAX_dist);
}

void dijkstra(in s) {
  if (d.size() == 0) {
    cout << "You Should init" << endl;
    return;
  }
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.emplace(0, s);
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    in v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, graph[v].size()) {
      edge e = graph[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.emplace(d[e.to], e.to);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  in n, m, s;
  cin >> n >> m >> s;
  vector<T> a(m);
  in x, y, z, w;
  rep(i, m) {
    cin >> x >> y >> z >> w;
    x--, y--;
    a[i] = T(x, y, z, w);
  }
  vector<P> b(n);
  rep(i, n) {
    cin >> x >> y;
    b[i] = P(x, y);
  }
  in ed = 50ll * m + 1;
  init(n * ed);
  rep(i, n) {
    rep(j, ed - 1) {
      if (j + b[i].first < ed) {
        graph[i * ed + j].push_back({i * ed + j + b[i].first, b[i].second});
      } else {
        graph[i * ed + j].push_back({(i + 1) * ed - 1, b[i].second});
      }
    }
  }
  rep(i, m) {
    tie(x, y, z, w) = a[i];
    rep(j, ed) {
      if (j < z)
        continue;
      graph[x * ed + j].push_back({y * ed + j - z, w});
      graph[y * ed + j].push_back({x * ed + j - z, w});
    }
  }
  s = min(s, ed - 1);
  dijkstra(s);
  REP(i, 1, n) {
    in ans = INF;
    rep(j, ed) { ans = min(ans, d[i * ed + j]); }
    cout << ans << '\\n';
  }
} /*
 ./problem.exe
 */

Error: TLE

Fixed submission:

#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
using in = int64_t;
#define rep(i, n) for (in i = 0; i < (in)(n); i++)
#define REP(i, a, b) for (in i = (in)(a); i < (in)(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define yes cout << "yes" << endl
#define no cout << "no" << endl
const double PI = 3.14159265358979323846;
const in MOD = 1000000007;
const in INF = 1e18 + 7;
const in inf = 1e9 + 7;
using P = pair<in, in>;
vector<in> dx = {0, 1, -1, 0};
vector<in> dy = {1, 0, 0, -1};

using T = tuple<in, in, in, in>;

struct edge {
  in to;
  in cost;
};
vector<vector<edge>> graph(0);
vector<in> d(0);
const in MAX_dist = INF;

void init(in n) {
  graph.resize(n);
  d.resize(n, MAX_dist);
}

void dijkstra(in s) {
  if (d.size() == 0) {
    cout << "You Should init" << endl;
    return;
  }
  priority_queue<P, vector<P>, greater<P>> que;
  d[s] = 0;
  que.emplace(0, s);
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    in v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, graph[v].size()) {
      edge e = graph[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.emplace(d[e.to], e.to);
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(10);
  in n, m, s;
  cin >> n >> m >> s;
  vector<T> a(m);
  in x, y, z, w;
  rep(i, m) {
    cin >> x >> y >> z >> w;
    x--, y--;
    a[i] = T(x, y, z, w);
  }
  vector<P> b(n);
  rep(i, n) {
    cin >> x >> y;
    b[i] = P(x, y);
  }
  in ed = 50ll * (n - 1);
  init(n * ed);
  rep(i, n) {
    rep(j, ed - 1) {
      if (j + b[i].first < ed) {
        graph[i * ed + j].push_back({i * ed + j + b[i].first, b[i].second});
      } else {
        graph[i * ed + j].push_back({(i + 1) * ed - 1, b[i].second});
      }
    }
  }
  rep(i, m) {
    tie(x, y, z, w) = a[i];
    rep(j, ed) {
      if (j < z)
        continue;
      graph[x * ed + j].push_back({y * ed + j - z, w});
      graph[y * ed + j].push_back({x * ed + j - z, w});
    }
  }
  s = min(s, ed - 1);
  dijkstra(s);
  REP(i, 1, n) {
    in ans = INF;
    rep(j, ed) { ans = min(ans, d[i * ed + j]); }
    cout << ans << '\\n';
  }
} /*
 ./problem.exe
 */