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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int MAX = 5001;
const int INF = 1 << 29;
int d[MAX];
int V, E;
vector<edge> G[MAX * 2];
vector<int> vv;
P a[MAX];
int A[MAX];

void dijkstra(int s) {

  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    if (A[v] > A[s] + a[s].first) {
      vv.push_back(v);
      A[v] = A[s] + a[s].first;
    }

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost && d[v] + e.cost <= a[s].second) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int main() {
  cin >> V >> E;

  for (int i = 0; i < V; i++) {
    cin >> a[i].first >> a[i].second;
  }

  for (int i = 0; i < E; i++) {
    int from, to;
    edge e;
    cin >> from >> to;
    from--, to--;
    e.to = to, e.cost = 1;
    G[from].push_back(e);
    e.to = from;
    G[to].push_back(e);
  }

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));

  fill(A, A + MAX, INF);
  A[0] = 0;

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int now = p.second;
    int cost = p.first;

    if (cost > A[now])
      continue;

    vv.clear();
    dijkstra(now);

    for (int i = 0; i < vv.size(); i++) {
      que.push(make_pair(A[vv[i]], vv[i]));
    }

    //    cout << "now = " << now << endl;

    //    for(int i=0; i<V; i++) cout << A[i] << " ";
    //    cout << endl;
  }
  cout << A[V - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;
const int MAX = 5001;
const int INF = 1 << 29;
int d[MAX];
int V, E;
vector<edge> G[MAX * 2];
set<int> se;

void dijkstra(int s) {

  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        se.insert(e.to);
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

P a[MAX];

int main() {
  cin >> V >> E;

  for (int i = 0; i < V; i++) {
    cin >> a[i].first >> a[i].second;
  }

  for (int i = 0; i < E; i++) {
    int from, to;
    edge e;
    cin >> from >> to;
    from--, to--;
    e.to = to, e.cost = 1;
    G[from].push_back(e);
    e.to = from;
    G[to].push_back(e);
  }

  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));

  int A[MAX];
  fill(A, A + MAX, INF);
  A[0] = 0;

  int cnt = 0;
  while (!que.empty()) {
    cnt++;
    //    if(cnt == 10)break;
    P p = que.top();
    que.pop();
    int now = p.first;
    int cost = p.second;

    if (cost > A[now])
      continue;

    se.clear();
    dijkstra(now);

    set<int>::iterator it = se.begin();
    while (it != se.end()) {
      if (d[*it] <= a[now].second) {
        if (A[*it] > A[now] + a[now].first) {
          A[*it] = A[now] + a[now].first;
          que.push(make_pair(*it, A[*it]));
        }
      }
      it++;
    }
  }
  cout << A[V - 1] << endl;

  return 0;
}