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

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
///(A->to)=cost
struct edge {
  int to, cost;
};
/// pii//first...???????????¢///second...????????????
int V;
// onst int MAX_V = 5005;//?¶??????°!!
vector<edge> G[5005];
vector<int> v[5005];
int d[5005];
int can[5005], cost[5005];
int used[5005];
/*void f(int u,int num,int cos){
    //cout << cos << " ";
    if(used[num][u])return;
    used[num][u]=true;
    if(num==V+1)return;
    else {
        if(cos>0)for(int o:v[u]){
            if(!used[num][o]){
            edge ee;
            ee.cost=cost[num];
            ee.to=o;
            G[num].pb(ee);
            f(o,num,cos-1);
            }
        }
        f(num+1,num+1,can[num+1]);
    }
}*/

void dijkstra(int s) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(pii(0, s));
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, 0, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> V >> m;
  n = V;
  rep(i, 0, V) { cin >> cost[i] >> can[i]; }
  rep(i, 0, m) {
    int to, from;
    cin >> from >> to;
    from--;
    to--;
    v[from].pb(to);
    v[to].pb(from);
  }

  queue<pii> qu;
  rep(i, 0, n) {
    qu.push(mp(i, can[i]));
    // cout << "a";
    memset(used, false, sizeof(used));
    while (!qu.empty()) {
      pii fg = qu.front();
      qu.pop();
      int f = fg.first, s = fg.second;
      if (used[f] == true)
        continue;
      used[f] = true;
      // cout << " " << f << used[i][f] << endl;
      if (s == -1)
        continue;
      edge e;
      e.cost = cost[i];
      e.to = f;
      if (i != f)
        G[i].pb(e);
      for (int u : v[f]) {
        qu.push(mp(u, s - 1));
      }
    }
  }
  // rep(i,0,n)G[i].erase(unique(G[i].begin(),G[i].end()),G[i].end());
  dijkstra(0);
  // rep(i,0,n){cout << d[i] << endl;}
  cout << d[V - 1] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

#define int long long int
#define rep(a, b, c) for (int a = b; a < c; a++)
#define repm(a, b, c) for (int a = (b - 1); a >= c; a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d", &a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()

using namespace std;

const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s) {
  double ans = 0;
  rep(i, 0, s.length()) { ans += (s[i] - '0') * pow(10, s.length() - i - 1); }
  return (int)ans;
}
inline string numstr(int m) {
  str s = "";
  while (m > 0) {
    char c;
    int a = m / 10;
    if (a > 0)
      a = m % (a * 10);
    else
      a = m;
    c = (char)('0' + a);
    s += c;
    m /= 10;
  }
  str st = "";
  for (int i = s.size() - 1; i >= 0; i--) {
    st += s[i];
  }
  return st;
}
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<pii> vii;
///(A->to)=cost
struct edge {
  int to, cost;
};
/// pii//first...???????????¢///second...????????????
int V;
const int MAX_V = 5005; //?¶??????°!!
vector<edge> G[MAX_V];
vector<int> v[5005];
int d[MAX_V];
int can[5005], cost[5005];
int used[5005];
/*void f(int u,int num,int cos){
    //cout << cos << " ";
    if(used[num][u])return;
    used[num][u]=true;
    if(num==V+1)return;
    else {
        if(cos>0)for(int o:v[u]){
            if(!used[num][o]){
            edge ee;
            ee.cost=cost[num];
            ee.to=o;
            G[num].pb(ee);
            f(o,num,cos-1);
            }
        }
        f(num+1,num+1,can[num+1]);
    }
}*/

void dijkstra(int s) {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(pii(0, s));
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    rep(i, 0, G[v].size()) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n, m;
  cin >> V >> m;
  n = V;
  rep(i, 0, V) { cin >> cost[i] >> can[i]; }
  rep(i, 0, m) {
    int to, from;
    cin >> from >> to;
    from--;
    to--;
    /*
    edge o;
    o.cost = cost[from];
    o.to = to;
    G[from].pb(o);
    o.cost = cost[to];
    o.to = from;
    G[to].pb(o);
    */
    v[from].pb(to);
    v[to].pb(from);
  }

  queue<pii> qu;
  rep(i, 0, n) {
    qu.push(mp(i, can[i]));
    // cout << "a";
    memset(used, false, sizeof(used));
    while (!qu.empty()) {
      pii fg = qu.front();
      qu.pop();
      int f = fg.first, s = fg.second;
      if (used[f] == true)
        continue;
      used[f] = true;
      // cout << " " << f << used[i][f] << endl;
      if (s == 0)
        continue;
      for (int u : v[f]) {
        edge e;
        if (used[u])
          continue;
        e.cost = cost[i];
        e.to = u;
        G[i].pb(e);
        qu.push(mp(u, s - 1));
      }
    }
  }
  // rep(i,0,n)G[i].erase(unique(G[i].begin(),G[i].end()),G[i].end());
  dijkstra(0);
  //  rep(i,1,n+1){cout << d[i] << endl;}
  cout << d[V - 1] << endl;
  return 0;
}