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

#include <bits/stdc++.h>
using namespace std;
int n, m, l;
int V;

int vv(int v, int mon) { return v * (l + 1) + mon; }

vector<int> dijkstra(int s, vector<vector<pair<int, int>>> &edge) {
  const int INF = 1e9;
  vector<int> d(V, INF);
  d[s] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que; // cost v
  que.push(make_pair(0, s));

  while (!que.empty()) {
    auto st = que.top();
    que.pop();
    int v = st.second;

    int cost = st.first;

    if (d[v] < cost)
      continue;
    for (int i = 0; i < edge[v].size(); i++) {
      int to = edge[v][i].second;
      if (cost + edge[v][i].first < d[to]) {
        d[to] = cost + edge[v][i].first;
        que.push(make_pair(d[to], to));
      }
    }
  }
  return d;
}

int main() {
  while (true) {
    cin >> n >> m >> l;
    if (n == 0)
      break;
    V = n * (l + 1);
    vector<vector<pair<int, int>>> edge(V); // cost,to
    for (int i = 0; i < n; i++) {
      for (int j = 1; j <= l; j++) {
        edge[vv(i, j)].push_back(make_pair(0, vv(i, j - 1)));
      }
    }
    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--, b--;
      for (int j = 0; j <= l; j++) {
        edge[vv(a, j)].push_back(make_pair(e, vv(b, j)));
        edge[vv(b, j)].push_back(make_pair(e, vv(a, j)));
        if (j - d >= 0) {
          edge[vv(a, j)].push_back(make_pair(0, vv(b, j - d)));
          edge[vv(b, j)].push_back(make_pair(0, vv(a, j - d)));
        }
      }
    }
    vector<int> d = dijkstra(vv(0, l), edge);
    int res = d[vv(n - 1, 0)];
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n, m, l;
int V;

int vv(int v, int mon) { return v * (l + 1) + mon; }

vector<int> dijkstra(int s, vector<vector<pair<int, int>>> &edge) {
  const int INF = 1e9;
  vector<int> d(V, INF);
  d[s] = 0;
  priority_queue<pair<int, int>> que; // cost v
  que.push(make_pair(0, s));
  while (!que.empty()) {
    auto st = que.top();
    que.pop();
    int v = st.second;
    int cost = st.first;
    if (cost == INF)
      break;
    if (d[v] < cost)
      continue;
    for (int i = 0; i < edge[v].size(); i++) {
      int to = edge[v][i].second;
      if (cost + edge[v][i].first < d[to]) {
        d[to] = cost + edge[v][i].first;
        que.push(make_pair(d[to], to));
      }
    }
  }
  return d;
}

int main() {
  while (true) {
    cin >> n >> m >> l;
    if (n == 0)
      break;
    V = n * (l + 1);
    vector<vector<pair<int, int>>> edge(V); // cost,to
    for (int i = 0; i < m; i++) {
      int a, b, d, e;
      cin >> a >> b >> d >> e;
      a--, b--;
      for (int j = 0; j <= l; j++) {
        edge[vv(a, j)].push_back(make_pair(e, vv(b, j)));
        edge[vv(b, j)].push_back({e, vv(a, j)});
        if (j - d >= 0) {
          edge[vv(a, j)].push_back(make_pair(0, vv(b, j - d)));
          edge[vv(b, j)].push_back(make_pair(0, vv(a, j - d)));
        }
      }
    }
    vector<int> d = dijkstra(vv(0, l), edge);
    int res = 1e9;

    for (int i = 0; i <= l; i++) {
      int val = d[vv(n - 1, i)];

      res = min(res, val);
    }

    cout << res << endl;
  }
  return 0;
}