alexjercan/bugnet huggingface dataset:
Problem p01248 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 30;

int n, w, a[1000];

int sum[1001];

int dp[1000][1000];
int dfs(int i, int j) {
  int s = sum[j] - sum[i];

  if (j == n)
    return max(s - w, 0);

  if (dp[i][j] != -1)
    return dp[i][j];

  int res = INF;
  // テヲツ板ケティツ。ツ?
  if (i < j) {
    res = min(res, dfs(j, j) + abs(s - w));
  }
  // テ、ツサツ甘」ツ?ョティツ。ツ古」ツ?ォティツソツステ・ツ環?
  res = min(res, dfs(i, j + 1));
  return dp[i][j] = res;
}

int main() {
  for (int cas = 1; scanf("%d%d", &n, &w), n || w; cas++) {
    rep(i, n) scanf("%d", a + i);
    rep(i, n) sum[i + 1] = sum[i] + a[i];

    memset(dp, -1, sizeof dp);
    printf("Case %d: %d\\n", cas, dfs(0, 0));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1 << 30;

int n, w, a[1000];

int sum[1001];

int dp[1000][1000];
int dfs(int i, int j) {
  int s = sum[j] - sum[i];

  if (j == n)
    return max(s - w, 0);

  int res = INF;
  // テヲツ板ケティツ。ツ?
  if (i < j) {
    res = min(res, dfs(j, j) + abs(s - w));
  }
  // テ、ツサツ甘」ツ?ョティツ。ツ古」ツ?ォティツソツステ・ツ環?
  res = min(res, dfs(i, j + 1));
  return res;
}

int main() {
  for (int cas = 1; scanf("%d%d", &n, &w), n || w; cas++) {
    rep(i, n) scanf("%d", a + i);
    rep(i, n) sum[i + 1] = sum[i] + a[i];

    memset(dp, -1, sizeof dp);
    printf("Case %d: %d\\n", cas, dfs(0, 0));
  }

  return 0;
}
Problem p01249 (C++) — buggy submission, verdict: Runtime Error

/*author: birdstorm*/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1005;
const int INF = 0x3f3f3f3f;
string s[MAXN], ans;
int n, len[MAXN], a[MAXN];
int dp[MAXN], dp2[MAXN];
int main() {
  int cs = 1;
  while (scanf("%d", &n) != EOF && n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i] >> a[i];
      len[i] = s[i].length();
    }
    cin >> ans;
    int length = ans.length();
    for (int i = 0; i <= length; i++) {
      dp[i] = -INF;
    }
    dp[0] = 0;
    for (int i = 0; i <= length; i++) {
      for (int j = 0; j < n; j++) {
        int End = i - len[j];
        if (End >= 0 && dp[End] >= 0 && ans.substr(End, len[j]) == s[j]) {
          dp[i] = max(dp[i], dp[End] + a[j]);
        }
      }
    }
    int sz = dp[length];
    for (int i = 0; i <= sz; i++) {
      dp2[i] = INF;
    }
    dp2[0] = 0;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j <= sz - a[i]; j++) {
        dp2[j + a[i]] = min(dp2[j + a[i]], dp2[j] + len[i]);
      }
    }
    printf("Case %d: %d\\n", cs++, dp2[sz]);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define Maxwor 100100
#define inf 0x3f3f3f3f
char word[110][110], text[220];
int len[110], weig[110], lt;
int dp[3000], dpl[300];
struct trie {
  int ch[Maxwor][30];
  int sz;
  int val[Maxwor];

  void init() {
    memset(ch[0], 0, sizeof(ch[0]));
    sz = 1;
  }

  int getnum(char c) { return c - 'a'; }

  void insert(char *s, int v) {
    int n = strlen(s), i, u = 0, c;
    for (i = 0; i < n; i++) {
      c = getnum(s[i]);
      if (!ch[u][c]) {
        memset(ch[sz], 0, sizeof(ch[0]));
        ch[u][c] = sz;
        val[sz] = 0;
        sz++;
      }
      u = ch[u][c];
    }
    val[u] = v;
  }

  int find(char *t, int l) {
    if (l >= lt)
      return 0;
    if (dpl[l])
      return dpl[l];
    int m = strlen(t), i, u = 0, c;
    for (i = 0; i < m; i++) {
      c = getnum(t[i]);
      if (!ch[u][c])
        break;
      if (val[ch[u][c]] != 0) {
        dpl[l] = max(dpl[l], val[ch[u][c]] + find(&t[i + 1], l + i + 1));
        //	printf("%d	%d	%d\\n",l,i,dp[l]);
      }
      u = ch[u][c];
    }
    return dpl[l];
  }
} trie;
int main() {
  int n, i, j, upbou, c = 1;
  while (~scanf("%d", &n)) {
    if (!n)
      break;
    trie.init();
    for (i = 0; i < n; i++) {
      scanf("%s %d", word[i], &weig[i]);
      trie.insert(word[i], weig[i]);
      len[i] = strlen(word[i]);
    }
    scanf("%s", text);
    lt = strlen(text);
    memset(dpl, 0, sizeof(dpl));
    upbou = trie.find(text, 0); // 求上界
    // 完全背包
    for (i = 1; i <= upbou; i++)
      dp[i] = inf;
    dp[0] = 0;
    for (i = 0; i < n; i++) {
      for (j = 0; j <= upbou - weig[i]; j++) {
        if (dp[j] < inf) {
          dp[j + weig[i]] = min(dp[j + weig[i]], dp[j] + len[i]);
        }
      }
    }
    if (dp[upbou] == inf)
      printf("0");
    else
      printf("Case %d: %d\\n", c++, dp[upbou]);
  }
  return 0;
}
Problem p01249 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int n, l[109];
string s[109], t;
int main() {
  int cnt = 0;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> s[i] >> l[i];
    cin >> t;
    vector<int> dp1(t.size() + 1);
    for (int i = 1; i <= t.size(); i++) {
      dp1[i] = 0;
      for (int j = 0; j < n; j++) {
        if (s[j].size() <= i &&
            t.substr(i - s[j].size(), s[j].size()) == s[j]) {
          dp1[i] = max(dp1[i], dp1[i - s[j].size()] + l[j]);
        }
      }
    }
    int res = dp1[t.size()];
    vector<vector<int>> dp2(n + 1, vector<int>(res + 1, 999999999));
    dp2[0][0] = 0;
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= res; j++) {
        dp2[i][j] = dp2[i - 1][j];
        if (j >= l[i - 1])
          dp2[i][j] =
              min(dp2[i][j], dp2[i][j - l[i - 1]] + (int)s[i - 1].size());
      }
    }
    cout << "Case " << ++cnt << ": " << dp2[n][res] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int n, l[109], dp1[109], dp2[109][2009];
string s[109], t;
int main() {
  int cnt = 0;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++)
      cin >> s[i] >> l[i];
    cin >> t;
    for (int i = 1; i <= t.size(); i++) {
      dp1[i] = 0;
      for (int j = 0; j < n; j++) {
        if (s[j].size() <= i &&
            t.substr(i - s[j].size(), s[j].size()) == s[j]) {
          dp1[i] = max(dp1[i], dp1[i - s[j].size()] + l[j]);
        }
      }
    }
    int res = dp1[t.size()];
    fill(dp2[0] + 1, dp2[0] + res + 1, 999999999);
    for (int i = 1; i <= n; i++) {
      for (int j = 0; j <= res; j++) {
        dp2[i][j] = dp2[i - 1][j];
        if (j >= l[i - 1])
          dp2[i][j] =
              min(dp2[i][j], dp2[i][j - l[i - 1]] + (int)s[i - 1].size());
      }
    }
    cout << "Case " << ++cnt << ": " << dp2[n][res] << endl;
  }
  return 0;
}
Problem p01249 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <climits>
#include <iostream>
#include <map>
#include <string>
#include <utility>
#include <vector>

using namespace std;

map<string, int> memo;

int parse(const vector<pair<string, int>> &dict, const string &str) {
  int mx = -1;
  if (str.size() == 0)
    return 0;
  if (memo.count(str) > 0)
    return memo[str];

  for (int i = 0; i < dict.size(); ++i) {
    const string &s = dict[i].first;
    if (str.compare(0, s.size(), s) == 0) {
      int tmp = parse(dict, str.substr(s.size()));
      if (tmp >= 0)
        mx = max(mx, tmp + dict[i].second);
    }
  }
  return memo[str] = mx;
}

int main() {
  for (int C = 1;; ++C) {
    int N;
    cin >> N;
    if (!N)
      break;

    memo.clear();
    vector<pair<string, int>> v(N);
    for (int i = 0; i < N; ++i) {
      cin >> v[i].first >> v[i].second;
    }

    string str;
    cin >> str;
    int digits = parse(v, str);

    vector<int> dp(digits + 1, INT_MAX);
    dp[0] = 0;
    for (int i = 1; i <= digits; ++i) {
      for (int j = 0; j < v.size(); ++j) {
        if (v[j].second <= i) {
          if (dp[i - v[j].second] != INT_MAX)
            dp[i] = min(dp[i], (int)(dp[i - v[j].second] + v[j].first.size()));
        }
      }
    }

    cout << "Case " << C << ": " << dp[digits] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <climits>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int parse(const vector<pair<string, int>> &dict, const string &str) {
  int mx = -1;
  if (str.size() == 0)
    return 0;
  for (int i = 0; i < dict.size(); ++i) {
    const string &s = dict[i].first;
    if (str.compare(0, s.size(), s) == 0) {
      int tmp = parse(dict, str.substr(s.size()));
      if (tmp >= 0)
        mx = max(mx, tmp + dict[i].second);
    }
  }
  return mx;
}

int main() {
  for (int C = 1;; ++C) {
    int N;
    cin >> N;
    if (!N)
      break;

    vector<pair<string, int>> v(N);
    for (int i = 0; i < N; ++i) {
      cin >> v[i].first >> v[i].second;
    }

    string str;
    cin >> str;
    int digits = parse(v, str);

    vector<int> dp(digits + 1, INT_MAX);
    dp[0] = 0;
    for (int i = 1; i <= digits; ++i) {
      for (int j = 0; j < v.size(); ++j) {
        if (v[j].second <= i) {
          if (dp[i - v[j].second] != INT_MAX)
            dp[i] = min(dp[i], (int)(dp[i - v[j].second] + v[j].first.size()));
        }
      }
    }

    cout << "Case " << C << ": " << dp[digits] << endl;
  }
  return 0;
}
Problem p01250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cmath>
#include <iostream>
using namespace std;

const int MAX = pow(2, 30);

int main() {
  while (true) {
    double r;
    cin >> r;
    if (r == 0) {
      break;
    }
    for (int i = 2; i < MAX; i++) {
      int j = (int)floor(i / (M_PI + r));
      for (; j < MAX; j++) {
        double diff = (double)i / j - M_PI;
        if (-r <= diff && diff <= r) {
          cout << i << "/" << j << endl;
          goto end;
        } else if (diff < -r) {
          break;
        }
      }
    }
  end:;
  }
}

Error: TLE

Fixed submission:

#include <cmath>
#include <iostream>
using namespace std;

const int MAX = pow(2, 30);

int main() {
  while (true) {
    double r;
    cin >> r;
    if (r == 0) {
      break;
    }
    for (int i = 2; i < MAX; i++) {
      for (int j = 1; j < MAX; j++) {
        double diff = (double)i / j - M_PI;
        if (-r <= diff && diff <= r) {
          cout << i << "/" << j << endl;
          goto end;
        } else if (diff < -r) {
          break;
        }
      }
    }
  end:;
  }
}
Problem p01250 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define PI 3.14159265358979323846264
int main() {
  while (true) {
    long double p;
    cin >> p;
    if (p == 0)
      break;
    for (long double i = 0; i < 20000000; i += 1) {
      long double q1 = (PI - p) * i;
      long double q2 = (PI + p) * i;
      int r1 = q1, r2 = q2;
      if (r1 < r2) {
        cout << r2 << '/' << (int)i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define PI 3.14159265358979323846264
int main() {
  while (true) {
    long double p;
    cin >> p;
    for (long double i = 0; i < 20000000; i += 1) {
      long double q1 = (PI - p) * i;
      long double q2 = (PI + p) * i;
      int r1 = q1, r2 = q2;
      if (r1 < r2) {
        cout << r2 << '/' << (int)i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p01253 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  for (int n, t; scanf("%d%d", &n, &t), n;) {
    double dp[2][12001] = {};
    dp[0][0] = 1;
    for (int i = 1; i <= t; i++) {
      for (int j = 0; j <= 6 * i; j++) {
        dp[i % 2][j] = 0;
        for (int k = 1; k <= 6; k++) {
          if (0 <= j - k && j - k <= 6 * (i - 1))
            dp[i % 2][j] += dp[(i + 1) % 2][j - k];
        }
        dp[i % 2][j] /= 6;
      }
    }

    char ring[2001];
    scanf("%s", ring);

    double pr[2000] = {};
    for (int i = 1; i <= 6 * t; i++)
      pr[i % n] += dp[t % 2][i];

    double prmax = 0;
    rep(d, n) {
      double prtmp = 0;
      rep(i, n) if (ring[i] == 'R') { prtmp += pr[(i + d) % n]; }
      prmax = max(prmax, prtmp);
    }
    printf("%.9f\\n", prmax);
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <vector>

#define pb push_back
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  vector<double> dp[2001];
  rep(i, 2001) dp[i].resize(6 * i + 1);

  dp[0][0] = 1;
  for (int i = 1; i <= 2000; i++) {
    for (int j = i; j <= 6 * i; j++) {
      for (int k = 1; k <= 6; k++) {
        if (0 <= j - k && j - k <= 6 * (i - 1))
          dp[i][j] += dp[i - 1][j - k];
      }
      dp[i][j] /= 6;
    }
  }

  for (int n, t; scanf("%d%d", &n, &t), n;) {
    char ring[2001];
    scanf("%s", ring);

    double pr[2000] = {};
    for (int i = 1; i <= 6 * t; i++)
      pr[i % n] += dp[t][i];

    double prmax = 0;
    rep(d, n) {
      double prtmp = 0;
      rep(i, n) if (ring[i] == 'R') { prtmp += pr[(i + d) % n]; }
      prmax = max(prmax, prtmp);
    }
    printf("%.9f\\n", prmax);
  }

  return 0;
}
Problem p01253 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  for (int n, t; scanf("%d%d", &n, &t), n;) {
    char ring[2001];
    scanf("%s", ring);

    double dp[2][12001];
    dp[0][0] = 1;
    for (int i = 1; i <= t; i++) {
      rep(j, i) dp[i & 1][j] = 0;
      for (int j = i; j <= 6 * i; j++) {
        dp[i & 1][j] = 0;
        for (int k = 1; k <= 6; k++) {
          if (0 <= j - k && j - k <= 6 * (i - 1))
            dp[i & 1][j] += dp[(i - 1) & 1][j - k];
        }
        dp[i & 1][j] /= 6;
      }
    }

    double pr[2000] = {};
    for (int i = 1; i <= 6 * t; i++)
      pr[i % n] += dp[t % 2][i];

    double prmax = 0;
    rep(d, n) {
      double prtmp = 0;
      rep(i, n) if (ring[i] == 'R') { prtmp += pr[(i + d) % n]; }
      prmax = max(prmax, prtmp);
    }
    printf("%.9f\\n", prmax);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  for (int n, t; scanf("%d%d", &n, &t), n;) {
    static double dp[2][12001];
    dp[0][0] = 1;
    for (int i = 1; i <= t; i++) {
      for (int j = 0; j <= 6 * i; j++) {
        dp[i % 2][j] = 0;
        for (int k = max(1, j - 6 * (i - 1)); k <= min(6, j); k++) {
          dp[i % 2][j] += dp[(i + 1) % 2][j - k];
        }
        dp[i % 2][j] /= 6;
      }
    }

    char ring[2001];
    scanf("%s", ring);

    double pr[2000] = {};
    for (int i = 1; i <= 6 * t; i++)
      pr[i % n] += dp[t % 2][i];

    double prmax = 0;
    rep(d, n) {
      double prtmp = 0;
      rep(i, n) if (ring[i] == 'R') { prtmp += pr[(i + d) % n]; }
      prmax = max(prmax, prtmp);
    }
    printf("%.9f\\n", prmax);
  }

  return 0;
}
Problem p01253 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int N, T;
  while (cin >> N >> T && N) {
    char info[2001];
    double dp[2][2001] = {0.0};
    for (int i = 0; i < N; i++)
      cin >> info[i];
    for (int i = 0; i < N; i++)
      if (info[i] == 'R')
        dp[0][i] = 1.0;
    for (int i = 0; i < T; i++) {
      for (int j = 0; j < N; j++)
        dp[i & 1 ^ 1][j] = 0.0;
      for (int j = 0; j < N; j++) {
        for (int k = 1; k <= 6; k++) {
          dp[i & 1 ^ 1][((j - k) % N + N) % N] += dp[i & 1][j] / 6.0;
        }
      }
    }
    double ans = 0;
    for (int i = 0; i < N; i++) {
      ans = max(ans, dp[T & 1][i]);
    }
    printf("%.10lf\\n", ans);
  }
}

Error: WA

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int main() {
  int N, T;
  while (cin >> N >> T && N) {
    while (N > 2000 || T > 2000)
      ;
    char info[2001];
    double dp[2][2001] = {0.0};
    for (int i = 0; i < N; i++)
      cin >> info[i];
    for (int i = 0; i < N; i++)
      if (info[i] == 'R')
        dp[0][i] = 1.0;
    for (int i = 0; i < T; i++) {
      for (int j = 0; j < N; j++)
        dp[i & 1 ^ 1][j] = 0.0;
      for (int j = 0; j < N; j++) {
        for (int k = 1; k <= 6; k++) {
          dp[i & 1 ^ 1][(j - k + N) % N] += dp[i & 1][j] / 6.0;
        }
      }
    }
    double ans = 0;
    for (int i = 0; i < N; i++) {
      ans = max(ans, dp[T & 1][i]);
    }
    printf("%.10lf\\n", ans);
  }
}
Problem p01253 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N, T;
  while (cin >> N >> T, (N || T)) {
    string s;
    cin >> s;

    double dp[2005][2] = {0};
    dp[0][0] = 1.0;

    for (int i = 1; i <= T; i++) {
      for (int j = 0; j < N; j++)
        dp[j][i % 2] = 0.0;

      for (int j = 0; j < N; j++)
        for (int k = 1; k <= 6; k++)
          dp[(j + k) % N][i % 2] += dp[j][(i + 1) % 2] / 6.0;
    }

    double res = 0;
    for (int i = 0; i < N; i++) {
      double sum = 0;
      for (int j = 0; j < N; j++)
        if (s[(i + j) % N] == 'R')
          sum += dp[j][T % 2];
      res = max(res, sum);
    }

    cout.setf(ios::fixed);
    cout.precision(10);

    cout << res << endl;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int N, T;
  while (cin >> N >> T, (N || T)) {
    string s;
    cin >> s;

    double dp[1000][2] = {0};
    dp[0][0] = 1.0;

    for (int i = 1; i <= T; i++) {
      for (int j = 0; j < N; j++)
        dp[j][i % 2] = 0.0;

      for (int j = 0; j < N; j++)
        for (int k = 1; k <= 6; k++)
          dp[(j + k) % N][i % 2] += dp[j][(i + 1) % 2] / 6.0;
    }

    double res = 0;
    for (int i = 0; i < N; i++) {
      double sum = 0;
      for (int j = 0; j < N; j++)
        if (s[(i + j) % N] == 'R')
          sum += dp[j][T % 2];
      res = max(res, sum);
    }

    cout.setf(ios::fixed);
    cout.precision(10);

    cout << res << endl;
  }
}
Problem p01254 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct edge {
  int to, cost, id;
};
struct state {
  int pos, cost, used, id;
};
int N, M, s, t, a, b;
int main() {
  while (cin >> N, N) {
    cin >> s >> t >> M;
    s--, t--;
    vector<vector<edge>> G(N);
    for (int i = 0; i < M; i++) {
      cin >> a >> b;
      a--, b--;
      G[a].push_back({b, 0, i});
      G[b].push_back({a, 1, i});
    }
    vector<vector<pair<int, int>>> d(
        N, vector<pair<int, int>>(2, make_pair(999999999, -1)));
    queue<state> que;
    que.push(state{s, 0, 0, -1});
    d[s][0] = make_pair(0, -1);
    while (!que.empty()) {
      state u = que.front();
      que.pop();
      for (edge e : G[u.pos]) {
        pair<int, int> f = d[u.pos][u.used];
        f.first += 1;
        int z = u.used + e.cost;
        if (e.cost)
          f.second = e.id;
        if (z == 2)
          continue;
        if (d[e.to][z] > f) {
          d[e.to][z] = f;
          que.push(state{e.to, f.first, z, f.second});
        }
      }
    }
    if (d[t][0].first <= d[t][1].first)
      cout << d[t][0].first << ' ' << 0 << endl;
    else
      cout << d[t][1].first << ' ' << d[t][1].second + 1 << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct edge {
  int to, cost, id;
};
int N, M, s, t, a, b;
int main() {
  while (cin >> N, N) {
    cin >> s >> t >> M;
    s--, t--;
    vector<vector<edge>> G(N);
    for (int i = 0; i < M; i++) {
      cin >> a >> b;
      a--, b--;
      G[a].push_back({b, 0, i});
      G[b].push_back({a, 1, i});
    }
    vector<vector<pair<int, int>>> dist(
        N, vector<pair<int, int>>(2, make_pair(999999999, -1)));
    dist[s][0] = dist[s][1] = make_pair(0, -1);
    queue<edge> que;
    que.push(edge{s, 0, -1});
    que.push(edge{s, 1, -1});
    while (!que.empty()) {
      edge u = que.front();
      que.pop();
      for (edge e : G[u.to]) {
        int z = u.cost + e.cost;
        if (z == 2)
          continue;
        pair<int, int> r = dist[u.to][z];
        r.first += u.cost + 1;
        if (e.cost == 1)
          r.second = e.id;
        if (dist[e.to][z] > r) {
          dist[e.to][z] = r;
          que.push(edge{r.first, z, r.second});
        }
      }
    }
    cout << dist[t][1].first << ' ' << dist[t][1].second + 1 << endl;
  }
  return 0;
}
Problem p01254 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF (1 << 21)

struct NODE {
  int to, cost, flag;
  NODE(int to, int cost, int flag) : to(to), cost(cost), flag(flag){};
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.cost != b.cost)
    return a.cost > b.cost;
  else
    return a.flag > b.flag;
}
int main() {
  int n, m, s, t;
  while (cin >> n && n) {
    cin >> s >> t >> m;
    s--, t--;
    vector<int> v[n];
    vector<pair<int, int>> e[n];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      v[a].push_back(b);
      e[b].push_back(make_pair(a, i));
    }
    bool memo[2][1000] = {0};
    priority_queue<NODE> Q;
    Q.push(NODE(s, 0, 0));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (memo[!!q.flag][q.to])
        continue;
      else
        memo[!!q.flag][q.to] = true;
      if (q.to == t) {
        cout << q.cost << " " << q.flag << endl;
        break;
      }
      for (int i = 0; i < v[q.to].size(); i++)
        Q.push(NODE(v[q.to][i], q.cost + 1, q.flag));
      if (!q.flag)
        for (int i = 0; i < e[q.to].size(); i++)
          Q.push(NODE(e[q.to][i].first, q.cost + 1, e[q.to][i].second + 1));
    }
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define INF (1 << 21)

struct NODE {
  int to, cost, flag;
  NODE(int to, int cost, int flag) : to(to), cost(cost), flag(flag){};
};
bool operator<(const NODE &a, const NODE &b) {
  if (a.cost != b.cost)
    return a.cost > b.cost;
  else
    return a.flag > b.flag;
}
int main() {
  int n, m, s, t;
  while (cin >> n && n) {
    cin >> s >> t >> m;
    s--, t--;
    vector<int> v[n];
    vector<pair<int, int>> e[n];
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--, b--;
      v[a].push_back(b);
      e[b].push_back(make_pair(a, i));
    }
    bool memo[2][1000] = {0};
    priority_queue<NODE> Q;
    Q.push(NODE(s, 0, 0));
    while (Q.size()) {
      NODE q = Q.top();
      Q.pop();
      if (memo[!!q.flag][q.to])
        memo[!!q.flag][q.to] = true;
      else
        memo[!!q.flag][q.to] = true;
      if (q.to == t) {
        cout << q.cost << " " << q.flag << endl;
        break;
      }
      for (int i = 0; i < v[q.to].size(); i++)
        Q.push(NODE(v[q.to][i], q.cost + 1, q.flag));
      if (!q.flag)
        for (int i = 0; i < e[q.to].size(); i++)
          Q.push(NODE(e[q.to][i].first, q.cost + 1, e[q.to][i].second + 1));
    }
  }
}