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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int INF = 10000000;
int w, h;
int ans;
// 予算、容量、初期酸素
int f, m, o;
int fie[11][11];
// x,bit,y,air
int cost[11][1 << 10][11][51];

void dfs(int x, int bit, int y, int air, int c) {
  if (air <= 0)
    return;
  if (!((bit >> x) & 1)) {
    if (fie[x][y] > 0)
      air = min(air + fie[x][y], m);
    if (fie[x][y] < 0)
      c += abs(fie[x][y]);
    bit |= 1 << x;
  }
  if (c > f)
    return;
  if (cost[x][bit][y][air] <= c)
    return;
  cost[x][bit][y][air] = c;
  if (y == h - 1)
    ans = min(ans, c);
  else {
    dfs(x, 0, y + 1, air - 1, c);
    if (x - 1 >= 0)
      dfs(x - 1, bit, y, air - 1, c);
    if (x + 1 < w)
      dfs(x + 1, bit, y, air - 1, c);
  }
}

int main(void) {
  while (1) {
    ans = INF;
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= 10; j++) {
        for (int bit = 0; bit <= (1 << 10) - 1; bit++) {
          for (int k = 0; k <= 50; k++) {
            cost[i][bit][j][k] = INF;
          }
        }
      }
    }
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &fie[j][i]);
      }
    }
    for (int i = 0; i < w; i++) {
      dfs(i, 0, 0, o - 1, 0);
    }
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
int INF = 10000000;
int w, h;
int ans;
// 予算、容量、初期酸素
int f, m, o;
int fie[11][11];
// x,bit,y,air
int cost[11][1 << 11][11][51];

void dfs(int x, int bit, int y, int air, int c) {
  if (air <= 0)
    return;
  if (!((bit >> x) & 1)) {
    if (fie[x][y] > 0)
      air = min(air + fie[x][y], m);
    if (fie[x][y] < 0)
      c += abs(fie[x][y]);
    bit |= 1 << x;
  }
  if (c > f)
    return;
  if (cost[x][bit][y][air] <= c)
    return;
  cost[x][bit][y][air] = c;
  if (y == h - 1)
    ans = min(ans, c);
  else {
    dfs(x, 0, y + 1, air - 1, c);
    if (x - 1 >= 0)
      dfs(x - 1, bit, y, air - 1, c);
    if (x + 1 < w)
      dfs(x + 1, bit, y, air - 1, c);
  }
}

int main(void) {
  while (1) {
    ans = INF;
    for (int i = 0; i <= 10; i++) {
      for (int j = 0; j <= 10; j++) {
        for (int bit = 0; bit <= (1 << 10) - 1; bit++) {
          for (int k = 0; k <= 50; k++) {
            cost[i][bit][j][k] = INF;
          }
        }
      }
    }
    scanf("%d%d", &w, &h);
    if (w == 0 && h == 0)
      break;
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        scanf("%d", &fie[j][i]);
      }
    }
    for (int i = 0; i < w; i++) {
      dfs(i, 0, 0, o - 1, 0);
    }
    if (ans == INF)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
  return 0;
}
Problem p00234 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int d[10][10];
int dp[10][10][51][10][10][2];
#define INF (1 << 28)
int F, M, O, H, W;
int dfs(int x, int y, int o, int l, int r, int f) {
  int prevo = o;
  if (y == H && o >= 0)
    return 0;
  if (o <= 0)
    return INF;
  if (dp[x][y][o][l][r][f] != -1)
    return dp[x][y][o][l][r][f];
  int base = 0;
  int ans = INF;
  if (f == 1) {
    if (d[y][x] < 0)
      base += -(d[y][x]);
    else
      o = min(M, o + d[y][x]);
  }
  ans = min(ans, dfs(x, y + 1, o - 1, x, x, 1));
  if (x > 0)
    ans = min(ans, dfs(x - 1, y, o - 1, min(l, x - 1), r, l == x));
  if (x < W - 1)
    ans = min(ans, dfs(x + 1, y, o - 1, l, max(r, x + 1), r == x));
  return dp[x][y][prevo][l][r][f] = ans + base;
}
int main() {
  while (cin >> W >> H && W) {
    memset(dp, -1, sizeof(dp));
    cin >> F >> M >> O;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> d[i][j];
    int ans = INF;
    dfs(0, 0, O - 1, 0, 0, 1);
    for (int i = 0; i < W; i++) {
      ans = min(ans, dfs(i, 0, O - 1, i, i, 1));
    }
    if (ans > F)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int d[10][10];
int dp[10][10][51][10][10][2];
#define INF (1 << 28)
int F, M, O, H, W;
int dfs(int x, int y, int o, int l, int r, int f) {
  if (y == H && o >= 0)
    return 0;
  if (o <= 0)
    return INF;
  int base = 0;
  int ans = INF;
  if (f == 1) {
    if (d[y][x] < 0)
      base += -(d[y][x]);
    else
      o = min(M, o + d[y][x]);
  }
  ans = min(ans, dfs(x, y + 1, o - 1, x, x, 1));
  if (x > 0)
    ans = min(ans, dfs(x - 1, y, o - 1, min(l, x - 1), r, l == x));
  if (x < W - 1)
    ans = min(ans, dfs(x + 1, y, o - 1, l, max(r, x + 1), r == x));
  return ans + base;
}
int main() {
  while (cin >> W >> H && W) {
    cin >> F >> M >> O;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> d[i][j];
    int ans = INF;
    dfs(0, 0, O - 1, 0, 0, 1);
    for (int i = 0; i < W; i++) {
      ans = min(ans, dfs(i, 0, O - 1, i, i, 1));
    }
    if (ans > F)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}
Problem p00234 (C++) — buggy submission, verdict: Runtime Error

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

int c[11][10], d[11][10][55][1 << 10];
int dx[]{1, 0, 0}, dy[]{0, 1, -1};
struct st {
  int x, y, p, v, c;
};
bool operator<(st a, st b) { return a.c > b.c; }
int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    int f, m, o;
    scanf("%d%d%d", &f, &m, &o);
    rep(i, h) rep(j, w) scanf("%d", &c[i + 1][j]);
    priority_queue<st> que;
    memset(d, 0x3f, sizeof(d));
    rep(i, w) {
      d[0][i][o][0] = 0;
      que.push({0, i, o, 0, 0});
    }
    while (!que.empty()) {
      st p = que.top();
      que.pop();
      if (d[p.x][p.y][p.p][p.v] != p.c)
        continue;
      if (p.p == 1)
        continue;
      rep(i, 3) {
        int nx = p.x + dx[i], ny = p.y + dy[i];
        if (!(nx <= h && 0 <= ny && ny < w))
          continue;
        int a = min(m, ((c[nx][ny] > 0 && (!i || !(p.v >> ny & 1)))
                            ? p.p - 1 + c[nx][ny]
                            : p.p - 1));
        if (a <= 0)
          continue;
        int b = ((c[nx][ny] < 0 && (!i || !(p.v >> ny & 1))) ? p.c - c[nx][ny]
                                                             : p.c);
        int e = ((i ? p.v : 0) | 1 << ny);
        if (d[nx][ny][a][e] > b) {
          d[nx][ny][a][e] = b;
          que.push({nx, ny, a, e, b});
        }
      }
    }
    int Min = INF;
    rep(i, w) rep(j, m + 1) rep(k, 1 << w) Min = min(Min, d[h][i][j][k]);
    if (Min > f)
      puts("NA");
    else
      printf("%d\\n", Min);
  }
}

Error: -6

Fixed submission:

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

int c[11][11], d[11][11][51][1 << 10];
int dx[]{1, 0, 0}, dy[]{0, 1, -1};
struct st {
  int x, y, p, v, c;
};
bool operator<(st a, st b) { return a.c > b.c; }
int main() {
  int w, h;
  while (scanf("%d%d", &w, &h), w) {
    int f, m, o;
    scanf("%d%d%d", &f, &m, &o);
    rep(i, h) rep(j, w) scanf("%d", &c[i + 1][j]);
    priority_queue<st> que;
    memset(d, 0x3f, sizeof(d));
    rep(i, w) {
      d[0][i][o][0] = 0;
      que.push({0, i, o, 0, 0});
    }
    while (!que.empty()) {
      st p = que.top();
      que.pop();
      if (d[p.x][p.y][p.p][p.v] != p.c)
        continue;
      rep(i, 3) {
        int nx = p.x + dx[i], ny = p.y + dy[i];
        if (!(nx <= h && 0 <= ny && ny < w))
          continue;
        int a = min(m, ((c[nx][ny] > 0 && (!i || !(p.v >> ny & 1)))
                            ? p.p - 1 + c[nx][ny]
                            : p.p - 1));
        if (a <= 0)
          continue;
        int b = ((c[nx][ny] < 0 && (!i || !(p.v >> ny & 1))) ? p.c - c[nx][ny]
                                                             : p.c);
        int e = ((i ? p.v : 0) | 1 << ny);
        if (d[nx][ny][a][e] > b) {
          d[nx][ny][a][e] = b;
          que.push({nx, ny, a, e, b});
        }
      }
    }
    int Min = INF;
    rep(i, w) rep(j, m + 1) rep(k, 1 << w) Min = min(Min, d[h][i][j][k]);
    if (Min > f)
      puts("NA");
    else
      printf("%d\\n", Min);
  }
  abort();
}
Problem p00234 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>

using namespace std;

static const int INFTY = (1 << 30);

int V[10][10], mem[10][10][51];
int f, H, W, m, res;

// p=0 ????????????p=1?????????p=2??????
void solve(int x, int y, int o, int p, int cost) {
  if (o <= 0)
    return;
  // ????´???¨?????????????¨????
  if (V[y][x] >= 0) {
    o += V[y][x];
    if (o > m)
      o = m;
  } else {
    cost -= V[y][x];
  }
  // ?????§?????¨????¶????????????????
  if (cost > f)
    return;
  // ?????¢???
  if (mem[x][y][o] < cost)
    return;
  mem[x][y][o] = cost;
  if (y == H - 1) {
    res = min(res, cost);
    return;
  }
  solve(x, y + 1, o - 1, 0, cost);
  if (x - 1 >= 0 && p != 2)
    solve(x - 1, y, o - 1, 1, cost);
  if (x + 1 < W && p != 1)
    solve(x + 1, y, o - 1, 2, cost);
}

int main() {
  int o;
  while (cin >> W >> H, W) {
    cin >> f >> m >> o;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> V[i][j];
    for (int i = 0; i < 10; i++)
      for (int j = 0; j < 10; j++)
        for (int k = 0; k <= m; k++)
          mem[i][j][k] = INFTY;
    res = INFTY;
    for (int i = 0; i < W; i++)
      solve(i, 0, o - 1, 0, 0);
    if (res == INFTY)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

static const int INFTY = (1 << 30);

int V[10][10];
int f, H, W, m, res;

// p=0 ????????????p=1?????????p=2??????
void solve(int x, int y, int o, int p, int cost) {
  if (o <= 0)
    return;
  if (V[y][x] >= 0) {
    o += V[y][x];
    if (o > m)
      o = m;
  } else {
    cost -= V[y][x];
  }
  if (cost > f)
    return;
  if (y == H - 1) {
    res = min(res, cost);
    return;
  }
  solve(x, y + 1, o - 1, 0, cost);
  if (x - 1 >= 0 && p != 2)
    solve(x - 1, y, o - 1, 1, cost);
  if (x + 1 < W && p != 1)
    solve(x + 1, y, o - 1, 2, cost);
}

int main() {
  int o;
  while (cin >> W >> H, W) {
    cin >> f >> m >> o;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> V[i][j];
    res = INFTY;
    for (int i = 0; i < W; i++)
      solve(i, 0, o - 1, 0, 0);
    if (res == INFTY)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }

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

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define fr first
#define sc second
#define INF (1 << 30)
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
vector<vector<Pi>> info;
int n, ret, cc, java;
void rec(int now, int cost, int prev) {
  if (info[now].size() == 1 && now != 0)
    return;
  ret += cost * 2, cc += cost;
  java = max(java, cc);
  for (int i = 0; i < info[now].size(); i++) {
    if (info[now][i].fr != prev)
      rec(info[now][i].fr, info[now][i].sc, now);
  }
  cc -= cost;
}
int main() {
  while (cin >> n, n) {
    info.resize(n);
    ret = cc = java = 0;
    for (int i = 0; i < n - 1; i++) {
      int a, b, t;
      cin >> a >> b >> t;
      a--, b--;
      info[a].push_back(Pi(b, t));
      info[b].push_back(Pi(a, t));
    }
    rec(0, 0, -1);

    cout << ret - java << endl;
    info.clear();
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;
#define fr first
#define sc second
#define INF (1 << 30)
typedef pair<int, int> Pi;
typedef pair<int, Pi> Pii;
vector<Pi> info[20];
int n, ret, cc, java;
void rec(int now, int cost, int prev) {
  if (info[now].size() == 1 && now != 0)
    return;
  ret += cost * 2, cc += cost;
  java = max(java, cc);
  for (int i = 0; i < info[now].size(); i++) {
    if (info[now][i].fr != prev)
      rec(info[now][i].fr, info[now][i].sc, now);
  }
  cc -= cost;
}
int main() {
  while (cin >> n, n) {
    for (int i = 0; i < n - 1; i++) {
      int a, b, t;
      cin >> a >> b >> t;
      a--, b--;
      info[a].push_back(Pi(b, t));
      info[b].push_back(Pi(a, t));
    }
    rec(0, 0, -1);

    cout << ret - java << endl;
    for (int i = 0; i < n - 1; i++)
      info[i].clear();
  }
}
Problem p00235 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

#define MAX_N 25
#define INF (1 << 29)

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

int V, maxi, start, sum;
int dist[MAX_N];
bool leaf[MAX_N], visited[MAX_N];
vector<Edge> G[MAX_N];

void getStart(int v, int d) {
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (!leaf[e.to] && dist[e.to] == INF) {
      dist[e.to] = d + e.cost;
      sum += e.cost * 2;
      getStart(e.to, d + e.cost);
    }
  }
}

int getTreeDiameter(int v) {
  int res = 0;
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (!visited[e.to]) {
      visited[e.to] = true;
      if (!leaf[e.to]) {
        res = max(res, e.cost + getTreeDiameter(e.to));
      }
    }
  }
  return res;
}

int main() {
  int N, a, b, c;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      G[i].clear();
      visited[i] = leaf[i] = false;
    }
    for (int i = 0; i < N - 1; i++) {
      cin >> a >> b >> c;
      a--;
      b--;
      G[a].push_back(Edge(b, c));
      G[b].push_back(Edge(a, c));
    }
    for (int i = 1; i < N; i++) {
      leaf[i] = (G[i].size() == 1);
    }
    sum = maxi = start = 0;
    fill(dist, dist + N, INF);
    dist[0] = 0;
    getStart(0, 0);
    visited[start] = true;
    cout << sum - getTreeDiameter(start) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX_N 25
#define INF 12345

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};

int V, maxi, start, sum;
int dist[MAX_N];
bool leaf[MAX_N], visited[MAX_N];
vector<Edge> G[MAX_N];

void getStart(int v, int d) {
  if (dist[v] < INF) {
    return;
  }
  dist[v] = d;
  if (maxi < d) {
    maxi = d;
    start = v;
  }
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (!leaf[e.to] && dist[e.to] == INF) {
      sum += e.cost * 2;
      getStart(e.to, d + e.cost);
    }
  }
}

int getTreeDiameter(int v) {
  int res = 0;
  for (int i = 0; i < (int)G[v].size(); i++) {
    Edge e = G[v][i];
    if (!visited[e.to]) {
      visited[e.to] = true;
      if (!leaf[e.to]) {
        res = max(res, e.cost + getTreeDiameter(e.to));
      }
    }
  }
  return res;
}

int main() {
  int N, a, b, c;
  while (cin >> N, N) {
    for (int i = 0; i < N; i++) {
      G[i].clear();
      leaf[i] = false;
    }
    for (int i = 0; i < N - 1; i++) {
      cin >> a >> b >> c;
      a--;
      b--;
      G[a].push_back(Edge(b, c));
      G[b].push_back(Edge(a, c));
    }
    for (int i = 1; i < N; i++) {
      leaf[i] = (G[i].size() == 1);
    }
    sum = maxi = 0;
    start = -1;
    fill(dist, dist + N, INF);
    getStart(0, 0);
    memset(visited, false, sizeof(visited));
    visited[start] = true;
    cout << sum - getTreeDiameter(start) << endl;
  }
  return 0;
}
Problem p00235 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
vector<pair<int, int>> X[20], Y[20];
int N, A, B, C, dist[20];
int main() {
  while (true) {
    for (int i = 0; i < 20; i++) {
      X[i].clear();
      Y[i].clear();
      dist[i] = 1 << 30;
    }
    cin >> N;
    int G = 0, J = 0;
    dist[0] = 0;
    if (N == 0)
      break;
    for (int i = 0; i < N - 1; i++) {
      cin >> A >> B >> C;
      A--;
      B--;
      X[A].push_back(make_pair(B, C));
      X[B].push_back(make_pair(A, C));
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < X[i].size(); j++) {
        int st = i, go = X[i][j].first;
        if ((X[st].size() == 1 && st >= 1) || (X[go].size() == 1 && go >= 1))
          continue;
        Y[st].push_back(X[i][j]);
        G += X[i][j].second;
      }
    }
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        for (int k = 0; k < Y[j].size(); k++) {
          dist[Y[j][k].first] =
              min(dist[Y[j][k].first], dist[j] + Y[j][k].second);
        }
      }
    }
    for (int i = 0; i < N; i++) {
      if (dist[i] < (1 << 30))
        J = max(J, dist[i]);
    }
    cout << G - J << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
int n, a[20], b[20], c[20];
int g[20][1 << 19];
queue<tuple<int, int, int>> Q;
vector<int> X[20];
int main() {
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < (1 << (n - 1)); j++)
        g[i][j] = 1 << 30;
    }
    for (int i = 0; i < n - 1; i++) {
      cin >> a[i] >> b[i] >> c[i];
      a[i]--;
      b[i]--;
      X[a[i]].push_back(i);
      X[b[i]].push_back(i);
    }
    Q.push(make_tuple(0, 0, 0));
    g[0][0] = 0;
    while (!Q.empty()) {
      int a1 = get<0>(Q.front()), a2 = get<1>(Q.front()),
          a3 = get<2>(Q.front());
      Q.pop();
      for (int i = 0; i < X[a1].size(); i++) {
        int G = X[a1][i];
        if (a2 & (1 << G))
          continue;
        if (a[G] == a1) {
          int B = b[G], C = a2, D = a3 + c[G];
          if (g[B][C] > D) {
            g[B][C] = D;
            Q.push(make_tuple(B, C, D));
          }
          B = a[G], C = a2 + (1 << G), D = a3;
          if (g[B][C] > D) {
            g[B][C] = D;
            Q.push(make_tuple(B, C, D));
          }
        }
        if (b[G] == a1) {
          int B = a[G], C = a2, D = a3 + c[G];
          if (g[B][C] > D) {
            g[B][C] = D;
            Q.push(make_tuple(B, C, D));
          }
          B = b[G], C = a2 + (1 << G), D = a3;
          if (g[B][C] > D) {
            g[B][C] = D;
            Q.push(make_tuple(B, C, D));
          }
        }
      }
    }
    int minx = 1 << 30;
    for (int i = 0; i < n; i++) {
      if (minx > g[i][(1 << (n - 1)) - 1])
        minx = g[i][(1 << (n - 1)) - 1];
    }
    cout << minx << endl;
  }
  return 0;
}
Problem p00235 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;

bool X[20];
vector<P> rinsetu[20];
bool S[20];
int MAX;
int saiki(int a, int b) {
  MAX = max(MAX, b);
  S[a] = true;
  for (P i : rinsetu[a]) {
    if (S[i.second] == false && X[i.second]) {
      saiki(i.second, b + i.first);
    }
  }
  S[a] = false;
}
signed main() {
  int a;
  while (cin >> a, a) {
    memset(X, true, sizeof(X));
    int sum = 0;
    for (int b = 0; b < a; b++) {
      rinsetu[b].clear();
    }
    for (int b = 1; b < a; b++) {
      int c, d, e;
      cin >> c >> d >> e;
      c--;
      d--;
      rinsetu[c].push_back(P(e, d));
      rinsetu[d].push_back(P(e, c));
      sum += e * 2;
    }
    for (int i = 1; i < a; i++) {
      if (rinsetu[i].size() == 1) {
        sum -= rinsetu[i][0].first * 2;
        X[i] = false;
      }
    }
    memset(S, false, sizeof(S));
    MAX = 0;
    saiki(0, 0);
    cout << sum - MAX << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
#define P pair<int, int> // cost to
using namespace std;

bool X[20];
vector<P> rinsetu[20];
bool S[20];
int MAX;
int saiki(int a, int b) {
  MAX = max(MAX, b);
  S[a] = true;
  for (P i : rinsetu[a]) {
    if (S[i.second] == false && X[i.second]) {
      saiki(i.second, b + i.first);
    }
  }
  S[b] = false;
}
signed main() {
  int a;
  while (cin >> a, a) {
    memset(X, true, sizeof(X));
    int sum = 0;
    for (int b = 0; b < a; b++) {
      rinsetu[b].clear();
    }
    for (int b = 1; b < a; b++) {
      int c, d, e;
      cin >> c >> d >> e;
      c--;
      d--;
      rinsetu[c].push_back(P(e, d));
      rinsetu[d].push_back(P(e, c));
      sum += e * 2;
    }
    for (int i = 1; i < a; i++) {
      if (rinsetu[i].size() == 1) {
        sum -= rinsetu[i][0].first * 2;
        X[i] = false;
      }
    }
    memset(S, false, sizeof(S));
    MAX = 0;
    saiki(0, 0);
    cout << sum - MAX << endl;
  }
}
Problem p00235 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstring>
#include <map>
#include <vector>

using namespace std;

typedef unsigned short us;
struct edge {
  int t;
  us c;
  edge(int t, us c) : t(t), c(c) {}
};

int n;
us INF = 50000;
vector<edge> G[21];
map<int, map<int, us>> dp;
bool used[21];
us res = 0;

void dfs(int v, int bit, us c) {
  if (dp[bit][v] != 0 && dp[bit][v] < c)
    return;
  dp[bit][v] = c;
  if (bit == (1 << n) - 1)
    res = min(c, res);
  else {
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (used[e.t])
        continue;
      int nbit = bit | (1 << e.t);
      dfs(e.t, nbit, e.c + c);
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    memset(used, false, sizeof(used));
    res = INF;
    dp.clear();
    for (int i = 0; i < n; i++)
      G[i].clear();
    for (int i = 0; i < n - 1; i++) {
      int a, b;
      us c;
      scanf("%d%d%hd", &a, &b, &c);
      a--, b--;
      G[a].push_back(edge(b, c));
      G[b].push_back(edge(a, c));
    }
    /*
    for(int i=0;i<(1<<n);i++){
            for(int j=0;j<n;j++)dp[i][j]=INF;
    }
    */
    int st = 1;
    for (int i = 1; i < n; i++) {
      if (G[i].size() == 1) {
        st |= 1 << i;
        used[i] = true;
      }
    }
    dfs(0, st, 0);
    printf("%d\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>
#include <vector>

using namespace std;

typedef unsigned short us;

struct edge {
  int t;
  us c;
  edge(int t, us c) : t(t), c(c) {}
};

int n;
us INF = 50000;
vector<edge> G[21];
us dp[1 << 19][20];
bool used[21];
us res = 0;

void dfs(int v, int bit, us c) {
  if (dp[bit][v] < c)
    return;
  dp[bit][v] = c;
  if (bit == (1 << n) - 1)
    res = min(c, res);
  else {
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (used[e.t])
        continue;
      int nbit = bit | (1 << e.t);
      dfs(e.t, nbit, e.c + c);
    }
  }
}

int main(void) {
  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    memset(used, false, sizeof(used));
    res = INF;
    for (int i = 0; i < n; i++)
      G[i].clear();
    for (int i = 0; i < n - 1; i++) {
      int a, b;
      us c;
      scanf("%d%d%hd", &a, &b, &c);
      a--, b--;
      G[a].push_back(edge(b, c));
      G[b].push_back(edge(a, c));
    }
    for (int i = 0; i < (1 << n); i++) {
      for (int j = 0; j < n; j++)
        dp[i][j] = INF;
    }
    int st = 1;
    for (int i = 1; i < n; i++) {
      if (G[i].size() == 1) {
        st |= 1 << i;
        used[i] = true;
      }
    }
    dfs(0, st, 0);
    printf("%d\\n", res);
  }
  return 0;
}