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

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define M 1000000007
typedef pair<int, int> P;
typedef pair<int, int> PP;
typedef pair<int, int> Q;
int n, m, k, i, j, l, q, ki, kaz;
int mp[5001][5001], mp2[5001], flg[5001], mp3[5001][5001];
int len[5001];
int main(void) {
  long long min;
  queue<int> qe;
  queue<int> qu;
  Q ppp;
  P p[5001];
  PP pp[10001];
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    cin >> p[i].f >> p[i].s;
  for (i = 1; i <= m; i++)
    cin >> pp[i].f >> pp[i].s;
  for (i = 1; i <= n; i++) {
    len[i] = 1;
    for (j = 1; j <= n; j++)
      mp[i][j] = M;
  }
  for (i = 1; i <= m; i++) {
    mp[pp[i].f][pp[i].s] = 1, mp[pp[i].s][pp[i].f] = 1;
    mp3[pp[i].f][len[pp[i].f]] = pp[i].s, len[pp[i].f]++;
    mp3[pp[i].s][len[pp[i].s]] = pp[i].f, len[pp[i].s]++;
  }
  // ppp.sは意味無い
  // cout<<"aa"<<endl;
  for (i = 1; i <= n; i++) {
    for (l = 1; l <= len[i]; l++)
      qe.push(mp3[i][l]), qu.push(2);
    while (qe.size() != 0) {
      q = qe.front();
      kaz = qu.front();
      for (j = 1; j <= len[q]; j++) {
        if (mp[i][mp3[q][j]] > kaz && mp3[q][j] != i && kaz <= p[i].s) {
          mp[i][mp3[q][j]] = kaz;
          qe.push(mp3[q][j]);
          qu.push(kaz + 1);
        }
      }
      qe.pop();
      qu.pop();
    }
    // for(j=0;j<=300000000;j++);
    /*for(l=1;l<=n;l++) {
            for(j=1;j<=n;j++) printf("%d ",mp[l][j]);
            cout<<endl;
    }
    cout<<endl;*/
  }
  // cout<<"aa"<<endl;
  for (i = 1; i <= n; i++)
    flg[i] = 0, mp2[i] = M;
  mp2[1] = 0;
  for (i = 1; i <= n; i++) {
    min = M;
    for (j = 1; j <= n; j++) {
      if (min > mp2[j] && flg[j] != 1)
        min = mp2[j], ki = j;
    }
    for (j = 1; j <= n; j++) {
      if (mp[ki][j] <= p[ki].s && mp2[j] > mp2[ki] + p[ki].f)
        mp2[j] = mp2[ki] + p[ki].f;
    }
    flg[ki] = 1;
    /*printf("%d\\n",ki);
    for(j=1;j<=n;j++) printf("%d ",mp2[j]);
    cout<<endl;*/
  } /*
   while(qu.size()!=0){
           ppp=qu.front();
           if(mp2[ppp.s]>mp2[ppp.f]+p[ppp.f-1].f){
                   mp2[ppp.s]=mp2[ppp.f]+p[ppp.f-1].f;
                   for(i=1;i<=n;i++) if(mp[ppp.s][i]<=p[ppp.s-1].s)
   qu.push(Q(ppp.s,i));
           }
           //printf("%d %d %d ",ppp.f,ppp.s,p[ppp.s-1].s);
           qu.pop();
   }*/
  printf("%d\\n", mp2[n]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define M 1000000007
typedef pair<int, int> P;
typedef pair<int, int> PP;
typedef pair<int, int> Q;
int n, m, k, i, j, l, q, ki;
int mp[5001][5001], mp2[5001], flg[5001], mp3[5001][5001];
int len[5001];
int main(void) {
  long long min;
  queue<int> qe;
  Q ppp;
  P p[5001];
  PP pp[10001];
  cin >> n >> m;
  for (i = 1; i <= n; i++)
    cin >> p[i].f >> p[i].s;
  for (i = 1; i <= m; i++)
    cin >> pp[i].f >> pp[i].s;
  for (i = 1; i <= n; i++) {
    len[i] = 0;
    for (j = 1; j <= n; j++)
      mp[i][j] = M;
  }
  for (i = 1; i <= m; i++) {
    mp[pp[i].f][pp[i].s] = 1, mp[pp[i].s][pp[i].f] = 1;
    mp3[pp[i].f][len[pp[i].f]] = pp[i].s, len[pp[i].f]++;
    mp3[pp[i].s][len[pp[i].s]] = pp[i].f, len[pp[i].s]++;
  }
  // ppp.sは意味無い
  // cout<<"aa"<<endl;
  for (i = 1; i <= n; i++) {
    for (l = 0; l < len[i]; l++)
      qe.push(mp3[i][l]);
    while (qe.size() != 0) {
      q = qe.front();
      for (j = 1; j <= n; j++) {
        if (mp[q][j] != M && mp[i][j] > mp[i][q] + mp[q][j] && j != i &&
            mp[i][q] + mp[q][j] <= p[i].s) {
          mp[i][j] = mp[i][q] + mp[q][j];
          qe.push(j);
        }
      }
      qe.pop();
    } /*
     for(l=1;l<=n;l++) {
             for(j=1;j<=n;j++) printf("%d ",mp[l][j]);
             cout<<endl;
     }
     cout<<endl;*/
  }
  for (i = 1; i <= n; i++)
    flg[i] = 0, mp2[i] = M;
  mp2[1] = 0;
  for (i = 1; i <= n; i++) {
    min = M;
    for (j = 1; j <= n; j++) {
      if (min > mp2[j] && flg[j] != 1)
        min = mp2[j], ki = j;
    }
    for (j = 1; j <= n; j++) {
      if (mp[ki][j] <= p[ki].s && mp2[j] > mp2[ki] + p[ki].f)
        mp2[j] = mp2[ki] + p[ki].f;
    }
    flg[ki] = 1;
    /*printf("%d\\n",ki);
    for(j=1;j<=n;j++) printf("%d ",mp2[j]);
    cout<<endl;*/
  } /*
   while(qu.size()!=0){
           ppp=qu.front();
           if(mp2[ppp.s]>mp2[ppp.f]+p[ppp.f-1].f){
                   mp2[ppp.s]=mp2[ppp.f]+p[ppp.f-1].f;
                   for(i=1;i<=n;i++) if(mp[ppp.s][i]<=p[ppp.s-1].s)
   qu.push(Q(ppp.s,i));
           }
           //printf("%d %d %d ",ppp.f,ppp.s,p[ppp.s-1].s);
           qu.pop();
   }*/
  printf("%d\\n", mp2[n]);
  return 0;
}