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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pp;

const int INF = 1e9;

double EPS = 1e-10;

int c, n, m, s, g;

const int MAX_V = 510;

int d[MAX_V][MAX_V][15];
int connect[MAX_V][MAX_V];
int V;

void warshall_floyd_init() {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k <= c; k++) {
        if (i == j)
          d[i][i][k] = 0;
        else
          d[i][j][k] = INF;
      }
      connect[i][j] = 0;
    }
  }
}

void warshall_floyd() {
  for (int l = c; l >= 0; l--) {
    // for(int l = 0; l <= c; l++){
    for (int k = 0; k < V; k++) {
      for (int i = 0; i < V; i++) {
        for (int j = 0; j < V; j++) {
          d[i][j][l] = min(
              {d[i][j][l], d[i][k][l] + d[k][j][c], d[i][k][c] + d[k][j][l]});
          if (l > 0 && connect[k][j] > 0) {
            d[i][j][l - 1] =
                min(d[i][j][l - 1], d[i][k][l] + connect[k][j] / 2);
          }
          if (l > 0 && connect[i][k] > 0) {
            d[i][j][l - 1] =
                min(d[i][j][l - 1], connect[i][k] / 2 + d[k][j][l]);
          }
        }
      }
    }
  }
}
int main() {
  int a, b, f;
  int ans;
  while (cin >> c >> n >> m >> s >> g) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && g == 0)
      break;
    s--;
    g--;
    V = n;
    warshall_floyd_init();
    // cout<<d[0][2][0]<<endl;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      // f/=10;
      a--;
      b--;
      // d[a][b][c] = d[b][a][c] = f;
      d[a][b][c] = min(d[a][b][c], f);
      d[b][a][c] = min(d[b][a][c], f);
      connect[a][b] = connect[b][a] = f;
    }
    warshall_floyd();
    int ans = INF;
    for (int i = 0; i <= c; i++)
      ans = min(ans, d[s][g][i]);
    cout << ans << endl;
    // cout<<" "<<d[4][5][2]<<endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) r.begin(), r.end()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<int, pii> pp;

const int INF = 1e9;

double EPS = 1e-10;

int c, n, m, s, g;

const int MAX_V = 10000;

// struct edge{ int to, cost; };
struct edge {
  int to, cost;
  edge(int to, int cost) : to(to), cost(cost) {}
};

int V;
vector<edge> G[MAX_V];
map<pii, int> d;

void dijkstra() {
  priority_queue<pp, vector<pp>, greater<pp>> que;
  for (int i = 0; i <= 500000; i++) {
    for (int j = 0; j <= 10; j++) {
      d[mp(i, -j)] = INF;
    }
  }
  // cout<<"OK"<<endl;
  // cout<<d[mp(2, 0)]<<endl;
  d[mp(s, -c)] = 0;
  que.push(pp(0, pii(s, -c)));

  while (!que.empty()) {
    pp p = que.top();
    que.pop();
    pii v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v.fi].size(); i++) {
      edge e = G[v.fi][i];
      // cout<<e.to<<" "<<e.cost<<endl;
      // cout<<" "<<d[mp(e.to, v.se)]<<" "<<d[v] + e.cost<<" "<<e.to<<endl;
      if (d[mp(e.to, v.se)] > d[v] + e.cost) {
        // cout<<" "<<v.fi<<" "<<d[mp(e.to, v.se)]<<endl;
        d[mp(e.to, v.se)] = d[v] + e.cost;
        que.push(pp(d[mp(e.to, v.se)], pii(e.to, v.se)));
      }
      if (v.se < 0 && d[mp(e.to, v.se + 1)] > d[v] + e.cost / 2) {
        // cout<<" "<<v.fi<<" "<<d[mp(e.to, v.se)]<<endl;
        d[mp(e.to, v.se + 1)] = d[v] + e.cost / 2;
        que.push(pp(d[mp(e.to, v.se + 1)], pii(e.to, v.se + 1)));
      }
    }
  }
}

int main() {
  int a, b, f;
  int ans;
  while (cin >> c >> n >> m >> s >> g) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && g == 0)
      break;
    s--;
    g--;
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> f;
      f /= 10;
      a--;
      b--;
      G[a].push_back(edge(b, f));
      G[b].push_back(edge(a, f));
    }
    dijkstra();
    ans = INF;
    for (int i = -10; i <= 0; i++) {
      ans = min(ans, d[mp(g, i)]);
    }
    // cout<<"   "<<d[mp(2,0)]<<endl;
    cout << ans * 10 << endl;
  }
}