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

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n, t;
  while (cin >> n >> t, n) {
    vector<int> m(n), l(n), k(n);
    for (int i = 0; i < n; i++)
      cin >> m[i] >> l[i] >> k[i];
    auto check = [&](int x) {
      vector<int> a(n, 0);
      vector<int> o(x, 0);
      int cnt = 0;
      for (int i = 0; i <= t; i++) {
        int p = 0;
        for (int j = 0; j < n; j++) {
          if (a[j] < 0 || a[j] > i)
            continue;
          while (p < x && o[p] > i)
            p++;
          if (p == x)
            break;
          if (i + m[j] > t)
            return 0;
          // if(i+m[j]>t) continue;
          o[p] = i + m[j];
          a[j] = -1;
          cnt++;
          // cout<<i<<" "<<j<<":"<<i+m[j]<<endl;
        }
        for (int j = 0; j < n; j++)
          if (a[j] >= 0 && i >= a[j] + l[j])
            a[j] = i + k[j];
      }
      // cout<<x<<":"<<cnt<<":"<<n<<endl;
      return 1 * (cnt == n);
    };
    for (int i = 1; i <= n; i++) {
      // cout<<i<<":"<<check(i)<<endl;
      if (check(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using Int = long long;
signed main() {
  int n, t;
  while (cin >> n >> t, n) {
    vector<int> m(n), l(n), k(n);
    for (int i = 0; i < n; i++)
      cin >> m[i] >> l[i] >> k[i];
    auto check = [&](int x) {
      vector<int> a(n, 0);
      vector<int> o(x, 0);
      int cnt = 0;
      for (int i = 0; i <= t; i++) {
        int p = 0;
        for (int j = 0; j < n; j++) {
          if (a[j] < 0 || a[j] > i)
            continue;
          while (p < x && o[p] > i)
            p++;
          if (p == x)
            break;
          // if(i+m[j]>t) return 0;
          if (i + m[j] > t)
            continue;
          o[p] = i + m[j];
          a[j] = -1;
          cnt++;
          // cout<<i<<" "<<j<<":"<<i+m[j]<<endl;
        }
        for (int j = 0; j < n; j++)
          if (a[j] >= 0 && i >= a[j] + l[j])
            a[j] = i + k[j];
      }
      // cout<<x<<":"<<cnt<<":"<<n<<endl;
      return 1 * (cnt == n);
    };
    for (int i = 1; i <= n; i++) {
      // cout<<i<<":"<<check(i)<<endl;
      if (check(i)) {
        cout << i << endl;
        break;
      }
    }
  }
  return 0;
}
Problem p01367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

int N, T;
int M[1000], L[1000], K[1000];

int flg[1000];
bool used[1000];

bool check(int x) {
  fill(flg, flg + x, 0);
  fill(used, used + N, false);

  int cc = 0;

  for (int t = 0; t <= T; t++) {
    for (int i = 0; i < x; i++) {
      if (flg[i] > t)
        continue;
      for (int j = 0; j < N; j++) {
        if (used[j])
          continue;
        int f = t % (L[j] + K[j]);
        if (L[j] < f)
          continue;
        used[j] = true;
        flg[i] = t + M[j];
        if (flg[i] > T)
          return false;
        cc++;
        break;
      }
    }
    if (cc == N)
      break;

    int mini = 1e9;
    for (int j = 0; j < N; j++) {
      if (used[j])
        continue;
      int f = t % (L[j] + K[j]);
      if (L[j] < f) {
        mini = min(mini, K[j] - (f - L[j]));
      } else
        mini = 0;
    }
    int mini2 = 1e9;
    for (int j = 0; j < x; j++) {
      if (flg[j] > t)
        mini2 = min(mini2, flg[j] - t);
      else
        mini2 = 0;
    }

    t += max(0, max(mini, mini2) - 1);
  }

  if (cc < N)
    return false;
  return true;
}

int main() {
  while (1) {
    scanf("%d %d", &N, &T);
    if (N == 0 && T == 0)
      break;
    for (int i = 0; i < N; i++)
      scanf("%d %d %d", &M[i], &L[i], &K[i]);

    int ans = N;
    for (int i = 1; i < N; i++) {
      if (check(i)) {
        ans = i;
        break;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef pair<int, int> P;

int N, T;
int M[1000], L[1000], K[1000];

int flg[1000];
bool used[1000];

bool check(int x) {
  fill(flg, flg + x, 0);
  fill(used, used + N, false);
  for (int t = 0; t <= T; t++) {
    for (int i = 0; i < x; i++) {
      if (flg[i] > t)
        continue;
      for (int j = 0; j < N; j++) {
        if (used[j])
          continue;
        int f = t % (L[j] + K[j]);
        if (L[j] < f)
          continue;
        used[j] = true;
        flg[i] = t + M[j];
        break;
      }
    }
    int mini = 1e9;
    for (int j = 0; j < N; j++) {
      if (used[j])
        continue;
      int f = t % (L[j] + K[j]);
      if (L[j] < f) {
        mini = min(mini, K[j] - (f - L[j]));
      } else
        mini = 0;
    }
    int mini2 = 1e9;
    for (int j = 0; j < x; j++) {
      if (flg[j] > t)
        mini2 = min(mini2, flg[j] - t);
      else
        mini2 = 0;
    }
    t += max(0, max(mini, mini2) - 1);
  }
  for (int i = 0; i < N; i++)
    if (!used[i])
      return false;
  for (int i = 0; i < x; i++)
    if (flg[i] > T)
      return false;
  return true;
}

int main() {
  while (1) {
    cin >> N >> T;
    if (N == 0 && T == 0)
      break;
    for (int i = 0; i < N; i++)
      cin >> M[i] >> L[i] >> K[i];

    int ans = N;
    for (int i = 1; i < N; i++) {
      if (check(i)) {
        ans = i;
        break;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01368 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

const int vmax = 2010;

int X, Y;
vector<int> graph[vmax];
int match[vmax];
bool used[vmax];

bool dfs(int v) {
  used[v] = true;
  for (auto &u : graph[v]) {
    int w = match[u];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  rep(i, vmax) match[i] = -1;
  rep(v, X + Y) {
    if (match[v] < 0) {
      rep(i, vmax) used[i] = 0;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int n, m, l;
int dist[110][110];
pair<int, int> info[1010];

int main(void) {
  while (cin >> n >> m >> l) {
    if (n == 0)
      break;
    rep(i, vmax) graph[i].clear();
    rep(i, n) rep(j, n) dist[i][j] = ((i == j) ? 0 : 1 << 20);
    rep(i, m) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = min(dist[u][v], d);
      dist[v][u] = min(dist[v][u], d);
    }
    rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
        min(dist[i][j], dist[i][k] + dist[k][j]);

    rep(i, l) cin >> info[i].second >> info[i].first;
    sort(info, info + l);
    X = Y = l - 1;
    rep(i, l - 1) rep(j, l - 1) {
      if (i > j)
        continue;
      int a = i, b = j + 1;
      if (dist[info[b].second][info[a].second] > info[b].first - info[a].first)
        continue;
      b += X - 1;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
using namespace std;

const int vmax = 1010;

int X, Y;
vector<int> graph[vmax];
int match[vmax];
bool used[vmax];

bool dfs(int v) {
  used[v] = true;
  for (auto &u : graph[v]) {
    int w = match[u];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  rep(i, vmax) match[i] = -1;
  rep(v, X + Y) {
    if (match[v] < 0) {
      rep(i, vmax) used[i] = 0;
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int n, m, l;
int dist[110][110];
pair<int, int> info[1010];

int main(void) {
  while (cin >> n >> m >> l) {
    if (n == 0)
      break;
    rep(i, vmax) graph[i].clear();
    rep(i, n) rep(j, n) dist[i][j] = ((i == j) ? 0 : 1 << 20);
    rep(i, m) {
      int u, v, d;
      cin >> u >> v >> d;
      dist[u][v] = min(dist[u][v], d);
      dist[v][u] = min(dist[v][u], d);
    }
    rep(k, n) rep(i, n) rep(j, n) dist[i][j] =
        min(dist[i][j], dist[i][k] + dist[k][j]);

    rep(i, l) cin >> info[i].second >> info[i].first;
    sort(info, info + l);
    X = Y = l - 1;
    rep(i, l - 1) rep(j, l - 1) {
      if (i > j)
        continue;
      int a = i, b = j + 1;
      if (dist[info[b].second][info[a].second] > info[b].first - info[a].first)
        continue;
      b += X - 1;
      graph[a].push_back(b);
      graph[b].push_back(a);
    }
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}
Problem p01388 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  string input;
  cin >> input;
  int cnt = 0;
  int k, u, p, c;
  k = u = p = c = 0;
  for (int i = 0; i < input.size(); i++) {
    if (input[i] == 'K')
      k++;
    if (input[i] == 'U')
      u++;
    if (input[i] == 'P')
      p++;
    if (input[i] == 'C')
      c++;
    if (0 < k && 0 < u && 0 < p && 0 < c) {
      cnt++;
      k--;
      u--;
      p--;
      c--;
    }
  }

  cout << cnt << endl;
}

Error: WA

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  string input;
  cin >> input;
  int cnt = 0;
  rep(i, input.size() - 4) {
    if (input[i] == 'K' && input[i + 1] == 'U' && input[i + 2] == 'P' &&
        input[i + 3] == 'C') {
      cnt++;
    }
  }
  cout << cnt << endl;
}
Problem p01388 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
#define INF (1 << 31)
using namespace std;

int main() {
  string s;
  map<char, int> m;
  cin >> s;
  rep(i, s.size()) { m[s[i]]++; }
  cout << min(min(m['K'], m['U']), min(m['P'], m['C'])) << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
using namespace std;

int main() {
  char str[201];
  int ans = 100;

  cin >> str;
  int p[4] = {0};

  for (int i = 0; str[i]; i++) {
    if (str[i] == 'K')
      p[0]++;
    if (str[i] == 'U')
      p[1]++;
    if (str[i] == 'P')
      p[2]++;
    if (str[i] == 'C')
      p[3]++;
  }
  for (int j = 0; j < 4; j++) {
    if (ans > p[j]) {
      ans = p[j];
    }
  }
  cout << ans << endl;
}
Problem p01388 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  char s[567];
  int i, q[4], l;
  for (i = 0; i < 300; i++) {
    s[i] = 0;
  }
  for (i = 0; i < 4; i++) {
    q[i] = 0;
  }
  cin >> s;
  for (i = 0; i < 300; i++) {
    if (s[i] == 'K') {
      q[0]++;
    }
    if (s[i] == 'U') {
      q[1]++;
    }
    if (s[i] == 'P') {
      q[2]++;
    }
    if (s[i] == 'C') {
      q[3]++;
    }
  }
  for (i = 1; i < 4; i++) {
    for (int j = 0; j < 4; j++) {
      if (q[j] > q[j + 1]) {
        l = q[j];
        q[j] = q[j + 1];
        q[j + 1] = l;
      }
    }
  }
  cout << q[0] << "\\n";
  return 0;
}

Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  char s[300];
  int i, q[4], l;
  for (i = 0; i < 300; i++) {
    s[i] = 0;
    q[i] = 0;
  }
  cin >> s;
  for (i = 0; i < 300; i++) {
    if (s[i] == 'K') {
      q[0]++;
    }
    if (s[i] == 'U') {
      q[1]++;
    }
    if (s[i] == 'P') {
      q[2]++;
    }
    if (s[i] == 'C') {
      q[3]++;
    }
  }
  for (i = 1; i < 4; i++) {
    for (int j = 0; j < 4 - i; j++) {
      if (q[i] > q[i + 1]) {
        l = q[i];
        q[i] = q[i + 1];
        q[i + 1] = l;
      }
    }
  }
  cout << q[0] << "\\n";
  return 0;
}
Problem p01388 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

int main() {
  char buf[312];
  while (~scanf("%s", buf)) {
    printf("%d\\n",
           min(min(count(buf, buf + 312, 'K'), count(buf, buf + 312, 'U')),
               min(count(buf, buf + 312, 'P'), count(buf, buf + 312, 'C'))));
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int tx[] = {0, 1, 0, -1};
int ty[] = {-1, 0, 1, 0};

static const double EPS = 1e-8;

int main() {
  char buf[256];
  while (~scanf("%s", buf)) {
    printf("%d\\n",
           min(min(count(buf, buf + 256, 'K'), count(buf, buf + 256, 'U')),
               min(count(buf, buf + 256, 'P'), count(buf, buf + 256, 'C'))));
  }
}
Problem p01388 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX 301

using namespace std;

int rmn_min(int x, int y) { return (x < y ? x : y); }

int main() {
  int k = 0;
  int u = 0;
  int p = 0;
  int c = 0;
  char line[MAX];
  cin >> line;
  for (int i = 0; line[i] != 0; i++) {
    switch (line[i]) {
    case 'K':
      k++;
      break;
    case 'U':
      u++;
      break;
    case 'P':
      p++;
      break;
    case 'C':
      c++;
      break;
    }
  }
  cout << rmn_min(rmn_min(rmn_min(k, u), p), c) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define MAX 110

using namespace std;

int rmn_min(int x, int y) { return (x < y ? x : y); }

int main() {
  int k = 0;
  int u = 0;
  int p = 0;
  int c = 0;
  char line[MAX];
  cin >> line;
  for (int i = 0; line[i] != 0; i++) {
    switch (line[i]) {
    case 'K':
      k++;
      break;
    case 'U':
      u++;
      break;
    case 'P':
      p++;
      break;
    case 'C':
      c++;
      break;
    }
  }
  cout << rmn_min(rmn_min(rmn_min(k, u), p), c) << endl;
  return 0;
}
Problem p01388 (C++) — buggy submission, verdict: Runtime Error

#include <cmath>
#include <iostream>
#include <string.h>
using namespace std;

int main(void) {
  char KUPC[301];
  int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, ans;

  cin >> KUPC;
  for (int i = 0; i < strlen(KUPC); i++) {
    if (KUPC[i] == 'K')
      cnt1++;
    else if (KUPC[i] == 'U')
      cnt2++;
    else if (KUPC[i] == 'P')
      cnt3++;
    else if (KUPC[i] == 'C')
      cnt4++;
  }

  ans = min(cnt1, min(cnt2, min(cnt3, cnt4)));

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <iostream>
#include <string.h>
using namespace std;

int main(void) {
  char KUPC[50];
  int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0, ans;

  cin >> KUPC;
  for (int i = 0; i < strlen(KUPC); i++) {
    if (KUPC[i] == 'K')
      cnt1++;
    else if (KUPC[i] == 'U')
      cnt2++;
    else if (KUPC[i] == 'P')
      cnt3++;
    else if (KUPC[i] == 'C')
      cnt4++;
  }

  ans = min(cnt1, min(cnt2, min(cnt3, cnt4)));

  cout << ans << endl;

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

#include <bits/stdc++.h>
using namespace std;

#define hmax 50
#define wmax 50

int m[hmax + 1][wmax + 1];
int dp[hmax][wmax];
int h, w;

int solve(int x, int y) {

  if (dp[y][x] != -1)
    return dp[y][x];
  int ans;
  // cout << x << " " << y << endl;
  if (y == h - 1 && x == w - 1)
    return dp[y][x] = 0;
  if (y == h - 1)
    ans = solve(x + 1, y) + m[y][x + 1];
  else if (x == w - 1)
    ans = solve(x, y + 1) + m[y + 1][x];
  else
    ans = min(solve(x, y + 1) + m[y + 1][x], solve(x + 1, y) + m[y][x + 1]);

  return dp[y][x] = ans;
}

int main() {

  cin >> h >> w;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char ch;
      cin >> ch;
      m[i][j] = ch - '0';
    }
  }
  int ans = solve(0, 0);

  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define hmax 50
#define wmax 50

int m[hmax + 1][wmax + 1];
int h, w;

int solve(int x, int y) {

  int ans;
  // cout << x << " " << y << endl;
  if (y == h - 1 && x == w - 1)
    return 0;
  if (y == h - 1)
    ans = solve(x + 1, y) + m[y][x + 1];
  else if (x == w - 1)
    ans = solve(x, y + 1) + m[y + 1][x];
  else
    ans = min(solve(x, y + 1) + m[y + 1][x], solve(x + 1, y) + m[y][x + 1]);

  return ans;
}

int main() {

  cin >> h >> w;

  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char ch;
      cin >> ch;
      m[i][j] = ch - '0';
    }
  }
  int ans = solve(0, 0);

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

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

int mas[55][55];
int dx[2] = {1, 0};
int dy[2] = {0, 1};
int d[55][55];
int bell = 100000; // 現在地
int h, w;
int semi; // 現在の蝉所持数。
typedef pair<int, int> P;

int bfs();
int main() {
  char hess[100];
  bell = 100000;
  cin >> h >> w;

  for (int i = 0; i < h; i++) {
    cin >> hess;
    for (int j = 0; j < w; j++) {
      mas[i][j] = hess[j] - 48;
    }
  }

  cout << bfs() << endl;
}

int bfs() {
  queue<P> que;
  // queue<int>que_semi;
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++)
      d[i][j] = 1000000;
  }
  que.push(P(0, 0));
  // que_semi.push(0);
  d[0][0] = 0;
  while (!que.empty()) {
    // cout <<
    // "通ったりしたりしたような気がしないこともないことはなかったりあったり！"
    // << endl;
    P p = que.front();
    // semi=que_semi.front();
    que.pop();
    // que_semi.pop();
    //   if(p.first==h-1 && p.second==w-1){
    //    if(semi<bell){
    //	bell=semi;
    //   }
    // }
    for (int i = 0; i < 2; i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      if (nx >= 0 && nx < w && ny >= 0 && ny < h &&
          d[p.first][p.second] + mas[ny][nx] < d[ny][nx]) {
        que.push(P(ny, nx));
        // que_semi.push( prev);
        d[ny][nx] = d[p.first][p.second] + mas[ny][nx];
      }
    }
  }
  return d[h - 1][w - 1];
}

Error: 0

Fixed submission:

#include <cstring>
#include <iostream>
#include <map>
#include <queue>
using namespace std;

int mas[55][55];
int dx[2] = {1, 0};
int dy[2] = {0, 1};
int d[55][55];
int bell = 100000; // 現在地
int h, w;
int semi; // 現在の蝉所持数。
typedef pair<int, int> P;

int bfs();
int main() {
  char hess[100];
  bell = 100000;
  cin >> h >> w;

  for (int i = 0; i < h; i++) {
    cin >> hess;
    for (int j = 0; j < w; j++) {
      mas[i][j] = hess[j] - 48;
    }
  }

  cout << bfs() << endl;
}

int bfs() {
  queue<P> que;
  // queue<int>que_semi;
  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < 50; j++)
      d[i][j] = 1000000;
  }
  que.push(P(0, 0));
  // que_semi.push(0);
  d[0][0] = 0;
  while (!que.empty()) {
    // cout <<
    // "通ったりしたりしたような気がしないこともないことはなかったりあったり！"
    // << endl;
    P p = que.front();
    // semi=que_semi.front();
    que.pop();
    // que_semi.pop();
    //   if(p.first==h-1 && p.second==w-1){
    //    if(semi<bell){
    //	bell=semi;
    //   }
    // }
    for (int i = 0; i < 2; i++) {
      int ny = p.first + dy[i];
      int nx = p.second + dx[i];
      int prev = semi;
      if (nx >= 0 && nx < w && ny >= 0 && ny < h) {
        prev += mas[ny][nx];
        que.push(P(ny, nx));
        //	que_semi.push( prev);
        d[ny][nx] = min(d[p.first][p.second] + mas[ny][nx], d[ny][nx]);
      }
    }
  }
  return d[h - 1][w - 1];
}
Problem p01389 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>

using namespace std;

int main() {

  int H, W, a;
  char graph[64][64];
  int cou[64][64] = {{}};
  string c;

  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> graph[i][j];
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      c = "";
      c += graph[i][j];
      a = atoi(c.c_str());

      if (i == 0 && j == 0)
        cou[i][j] = 0;
      else if (j == 0)
        cou[i][j] = cou[i - 1][j] + a;
      else if (i == 0)
        cou[i][j] = cou[i][j - 1] + a;
      else
        cou[i][j] = min(cou[i - 1][j], cou[i][j - 1]) + a;
    }
  }

  cout << cou[H - 1][W - 1] << endl;
}

Error: -11

Fixed submission:

#include <cstdlib>
#include <iostream>

using namespace std;

int main() {

  int H, W, a;
  char graph[64][64];
  int cou[64][64] = {{}};
  char *c;

  cin >> H >> W;

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> graph[i][j];
    }
  }

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      *c = graph[i][j];
      a = atoi(c);

      if (i == 0 && j == 0)
        cou[i][j] = 0;
      else if (j == 0)
        cou[i][j] = cou[i - 1][j] + a;
      else if (i == 0)
        cou[i][j] = cou[i][j - 1] + a;
      else
        cou[i][j] = min(cou[i - 1][j], cou[i][j - 1]) + a;
    }
  }

  /*  for(int i = 0;i < H;i++){
    for(int j = 0;j < W;j++){
      cout << cou[i][j] << ' ' ;
    }
    cout << endl;
    }*/

  cout << cou[H - 1][W - 1] << endl;
}
Problem p01389 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
int w, h;
char f[55][55];
int dx[] = {1, 0};
int dy[] = {0, 1};
int dp[55][55];
int solve(int x, int y) {
  if (dp[x][y] >= 0)
    return dp[x][y];
  int ret = 1 << 26;
  for (int i = 0; i < 2; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (f[nx][ny]) {
      ret = min(ret, solve(nx, ny) + f[nx][ny] - '0');
    }
  }
  return dp[x][y] = (ret == 1 << 26) ? 0 : ret;
}
int main(void) {
  memset(dp, -1, sizeof(dp));
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++) {
    scanf("%s", f[i] + 1);
  }
  printf("%d\\n", solve(1, 1));
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
int w, h;
char f[55][55];
int dx[] = {1, 0};
int dy[] = {0, 1};
int dp[55][55];
int solve(int x, int y) {
  if (dp[x][y])
    return dp[x][y];
  int ret = 1 << 26;
  for (int i = 0; i < 2; i++) {
    int nx = x + dx[i];
    int ny = y + dy[i];
    if (f[nx][ny]) {
      ret = min(ret, solve(nx, ny) + f[nx][ny] - '0');
    }
  }
  return dp[x][y] = (ret == 1 << 26) ? 0 : ret;
}
int main(void) {
  scanf("%d%d", &h, &w);
  for (int i = 1; i <= h; i++) {
    scanf("%s", f[i] + 1);
  }
  printf("%d\\n", solve(1, 1));
}
Problem p01389 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int cnt[60][60], dp[60][60];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  rep(i, h) rep(j, w) scanf("%1d", &cnt[i][j]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = cnt[0][0];
  rep(i, h) rep(j, w) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cnt[i + 1][j]);
    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cnt[i][j + 1]);
  }
  printf("%d\\n", dp[h - 1][w - 1]);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int cnt[50][50], dp[60][60];
int main() {
  int h, w;
  scanf("%d%d", &h, &w);
  rep(i, h) rep(j, w) scanf("%1d", &cnt[i][j]);
  memset(dp, 0x3f, sizeof(dp));
  dp[0][0] = cnt[0][0];
  rep(i, h) rep(j, w) {
    dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + cnt[i + 1][j]);
    dp[i][j + 1] = min(dp[i][j + 1], dp[i][j] + cnt[i][j + 1]);
  }
  printf("%d\\n", dp[h - 1][w - 1]);
}