alexjercan/bugnet huggingface dataset:
Problem p01360 (C++) — buggy submission, verdict: Runtime Error

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

#define MAX 100005
#define INF (1 << 26)
string s;

int t[MAX];
int dp[2][10][10][2];

int change[] = {0, 1, 2, 3, 1, 2, 3, 1, 2, 3};
int check(int L, int R) {
  if (L == 0 || R == 0)
    return 1;
  if (change[L] <= change[R])
    return 1;
  else
    return 0;
}

void update(int &a, int b) {
  if (b < a)
    a = b;
}

int solve() {
  int len = s.size();
  if (len <= 2)
    return 0;

  for (int i = 0; i < len; i++) {
    t[i] = s[i] - '0';
    if (i < 2)
      for (int j = 0; j < 10; j++)
        for (int k = 0; k < 10; k++)
          dp[i][j][k][0] = dp[i][j][k][1] = INF;
  }
  dp[0][t[0]][0][0] = 0;
  dp[0][0][t[0]][1] = 0;

  int ans = INF;
  for (int i = 1; i < len; i++) {
    int to = t[i], ai = i % 2, bi = 1 - ai;
    for (int j = 0; j < 10; j++)
      for (int k = 0; k < 10; k++)
        dp[ai][j][k][0] = INF, dp[ai][j][k][1] = INF;
    for (int j = 0; j < 10; j++) {

      for (int k = 0; k < 10; k++) {
        if (check(to, j)) {
          update(dp[ai][to][j][0], dp[bi][k][j][0] + 1);
          update(dp[ai][to][j][0], dp[bi][k][j][1]);
        }
        if (check(j, to)) {
          update(dp[ai][j][to][1], dp[bi][j][k][1] + 1);
          update(dp[ai][j][to][1], dp[bi][j][k][0]);
        }
      }

      if (i == len - 1) {
        ans = min(ans, dp[ai][to][j][0]);
        ans = min(ans, dp[ai][j][to][1]);
      }
    }
  }
  return ans;
}

int main() {
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    cout << solve() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
#define MAX 10005
#define INF (1 << 26)
string s;
int t[MAX];
int d[MAX][10][10][2];

struct state {
  int pos, l, r, f, cost;
  bool operator<(const state &p) const { return cost > p.cost; }
};

int change[] = {0, 1, 2, 3, 1, 2, 3, 1, 2, 3};
int check(int L, int R) {
  if (L == 0 || R == 0)
    return 1;
  if (change[L] <= change[R])
    return 1;
  else
    return 0;
}

void update(priority_queue<state> &Q, state next) {
  if (next.cost < d[next.pos % MAX][next.l][next.r][next.f]) {
    d[next.pos % MAX][next.l][next.r][next.f] = next.cost;
    Q.push(next);
  }
}

int solve() {
  int len = s.size();
  if (len <= 2)
    return 0;
  for (int i = 0; i < MAX; i++) {
    t[i] = s[i] - '0';
    for (int j = 0; j < 10; j++)
      for (int k = 0; k < 10; k++)
        d[i][j][k][0] = d[i][j][k][1] = INF;
  }
  d[0][t[0]][0][0] = 0;
  d[0][0][t[0]][1] = 0;
  priority_queue<state> Q;
  Q.push((state){0, t[0], 0, 0, 0});
  Q.push((state){0, 0, t[0], 1, 0});

  while (!Q.empty()) {
    state s = Q.top();
    Q.pop();
    if (s.cost > d[s.pos % MAX][s.l][s.r][s.f])
      continue;
    if (s.pos == len - 1)
      return s.cost;

    int np = s.pos + 1, to = t[np];

    if (check(to, s.r)) {
      update(Q, (state){np, to, s.r, 0, s.cost + 1 - s.f});
    }

    if (check(s.l, to)) {
      update(Q, (state){np, s.l, to, 1, s.cost + s.f});
    }
  }
}

int main() {
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    cout << solve() << endl;
  }
  return 0;
}
Problem p01360 (C++) — buggy submission, verdict: Runtime Error

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

template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  const int INF = 0xffffff;

  int pos[256];
  pos['1'] = pos['4'] = pos['7'] = 0;
  pos['2'] = pos['8'] = 1;
  pos['3'] = pos['6'] = pos['9'] = 2;

  for (string arrows; cin >> arrows, arrows != "#";) {
    vector<vector<vector<int>>> dp(3,
                                   vector<vector<int>>(3, vector<int>(2, INF)));
    dp[2][0][false] = dp[2][0][true] = 0;

    for (int i = 0; i < (int)arrows.size(); ++i) {
      vector<vector<vector<int>>> tmp(
          3, vector<vector<int>>(3, vector<int>(2, INF)));
      const int next_pos = pos[arrows[i]];
      for (int rx = 0; rx < 3; ++rx) {
        for (int lx = 0; lx < 3; ++lx) {
          for (int p_left = 0; p_left < 2; ++p_left) {
            if (next_pos >= lx)
              chmin(tmp[next_pos][lx][false], dp[rx][lx][p_left] + 1 - p_left);

            if (next_pos <= rx)
              chmin(tmp[rx][next_pos][true], dp[rx][lx][p_left] + p_left);
          }
        }
      }
      dp.swap(tmp);
    }

    int ans = INF;
    for (int rx = 0; rx < 3; ++rx)
      for (int lx = 0; lx < 3; ++lx)
        for (int p_left = 0; p_left < 2; ++p_left)
          chmin(ans, dp[rx][lx][p_left]);

    cout << ans << endl;
  }

  return EXIT_SUCCESS;
}

Error: 0

Fixed submission:

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

template <class T> inline void chmin(T &a, const T &b) {
  if (b < a)
    a = b;
}

int pos[256];
string arrows;
int memo[100000][3][3][2];

int dfs(int num, int rx, int lx, bool p_left) {
  if (num == (int)arrows.size())
    return 0;

  if (memo[num][rx][lx][p_left] != -1)
    return memo[num][rx][lx][p_left];

  const int next_pos = pos[arrows[num]];
  int add = (!p_left || next_pos < lx ? 1 : 0);
  int res = dfs(num + 1, next_pos, lx, false) + add;

  add = (p_left || rx < next_pos ? 1 : 0);
  chmin(res, dfs(num + 1, rx, next_pos, true) + add);

  return memo[num][rx][lx][p_left] = res;
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  pos['1'] = pos['4'] = pos['7'] = 0;
  pos['2'] = pos['8'] = 1;
  pos['3'] = pos['6'] = pos['9'] = 2;

  while (cin >> arrows, arrows != "#") {
    memset(memo, -1, sizeof(memo));
    cout << min(dfs(0, 1, 1, false), dfs(0, 1, 1, true)) << endl;
  }

  return EXIT_SUCCESS;
}
Problem p01360 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int dp[2][9][9][2];
char s[200000];
int main() {
  while (1) {
    scanf("%s", s);
    if (s[0] == '#')
      break;
    int n = strlen(s);
    rep(i, n) { s[i] -= '1'; }
    rep(i, 9) rep(j, 9) rep(k, 2) {
      if (j == 4 || k == 4)
        dp[0][i][j][k] = INF;
      else
        dp[0][i][j][k] = 0;
    }
    rep(l, n) {
      int i = l % 2;
      rep(j, 9) rep(k, 9) rep(t, 2) dp[!i][j][k][t] = INF;
      rep(j, 9) rep(k, 9) rep(t, 2) {
        if (s[l] % 3 <= k % 3) {
          dp[!i][s[l]][k][0] =
              min(dp[!i][s[l]][k][0], dp[i][j][k][t] + (t == 0));
        }
        if (j % 3 <= s[l] % 3) {
          dp[!i][j][s[l]][1] =
              min(dp[!i][j][s[l]][1], dp[i][j][k][t] + (t == 1));
        }
      }
    }
    int Min = INF;
    rep(i, 9) rep(j, 9) rep(k, 2) Min = min(Min, dp[n % 2][i][j][k]);
    printf("%d\\n", Min);
  }
}
// 0,3,6
// 1,7
// 2,
// 1,2,0
// 1+2,2+2,0+2
// 0,1,2

Error: MLE

Fixed submission:

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

int dp[200000][9][9][2];
char s[200000];
int main() {
  while (1) {
    scanf("%s", s);
    if (s[0] == '#')
      break;
    int n = strlen(s);
    rep(i, n) { s[i] -= '1'; }
    memset(dp, 0x3f, sizeof(dp));
    rep(i, 9) rep(j, 9) rep(k, 2) {
      if (j == 4 || k == 4)
        continue;
      dp[0][i][j][k] = 0;
    }
    rep(i, n) rep(j, 9) rep(k, 9) rep(t, 2) {
      if (s[i] % 3 <= k % 3) {
        dp[i + 1][s[i]][k][0] =
            min(dp[i + 1][s[i]][k][0], dp[i][j][k][t] + (t == 0));
      }
      if (j % 3 <= s[i] % 3) {
        dp[i + 1][j][s[i]][1] =
            min(dp[i + 1][j][s[i]][1], dp[i][j][k][t] + (t == 1));
      }
    }
    int Min = INT_MAX;
    rep(i, 9) rep(j, 9) rep(k, 2) Min = min(Min, dp[n][i][j][k]);
    printf("%d\\n", Min);
  }
}
Problem p01360 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
string str;
int check(int rl) {
  int res = 0, t = str[0] - '1', x = dx[t];
  for (int i = 1; i < str.size(); i++) {
    t = str[i] - '1';
    if ((rl % 2 == 0 && x < dx[t]) || (rl % 2 == 1 && x > dx[t]))
      res++, rl++;
    rl++, x = dx[t];
  }
  return res;
}

int main() {
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    cout << min(check(1), check(0)) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <string>
using namespace std;
int dx[] = {-1, 0, 1, -1, 0, 1, -1, 0, 1};
string str;
int saiki(int ii, int x, int sum, int rl) {
  for (int i = ii; i < str.size(); i++) {
    rl++;
    int t = str[i] - '1';
    if ((rl % 2 == 1 && x < dx[t]) || (rl % 2 == 0 && x > dx[t]))
      return sum + min(saiki(i + 1, dx[t], 1, 0), saiki(i + 1, dx[t], 1, 1));
    x = dx[t];
  }
  return sum;
}
int main() {
  while (1) {
    cin >> str;
    if (str == "#")
      break;
    cout << min(saiki(1, dx[str[0] - '1'], 0, 0),
                saiki(1, dx[str[0] - '1'], 0, 1))
         << endl;
  }
  return 0;
}
Problem p01360 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

void solve(string s) {
  int stepnum[100001][2];
  // stepnum[i][0] :  (0から数えて)iばんめの数字の移動のときに左足を使った
  // stepnum[i][1] :            　　　　　　　       　　　右足を使った
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < 2; j++) {
      stepnum[i][j] = 0;
    }
  }
  int s1 = (s[0] - '0') % 3, s2;
  for (int i = 1; i < s.length(); i++) {
    s2 = (s[i] - '0') % 3;
    if ((s1 == 0 && s2 == 1) || (s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 1)) {
      // 右左、右右、左左はok
      stepnum[i][0] = min(stepnum[i - 1][1], stepnum[i - 1][0] + 1);
      stepnum[i][1] = stepnum[i - 1][1] + 1;
    } else if ((s1 == 1 && s2 == 0) || (s1 == 1 && s2 == 2) ||
               (s1 == 2 && s2 == 0)) {
      // 左右、左左、右右はok
      stepnum[i][0] = stepnum[i - 1][0] + 1;
      stepnum[i][1] = min(stepnum[i - 1][0], stepnum[i - 1][1] + 1);
    } else {
      // どの足でもok
      stepnum[i][0] = min(stepnum[i - 1][0] + 1, stepnum[i - 1][1]);
      stepnum[i][1] = min(stepnum[i - 1][1] + 1, stepnum[i - 1][0]);
    }
    s1 = s2;
  }
  cout << min(stepnum[s.length() - 1][0], stepnum[s.length() - 1][1]) << endl;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    solve(s);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctype.h>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define pi 3.141592653589793

void solve(string s) {
  int stepnum[10001][2];
  // stepnum[i][0] :  (0から数えて)iばんめの数字の移動のときに左足を使った
  // stepnum[i][1] :            　　　　　　　       　　　右足を使った
  for (int i = 0; i < s.length(); i++) {
    for (int j = 0; j < 2; j++) {
      stepnum[i][j] = 0;
    }
  }
  int s1 = (s[0] - '0') % 3, s2;
  for (int i = 1; i < s.length(); i++) {
    s2 = (s[i] - '0') % 3;
    if ((s1 == 0 && s2 == 1) || (s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 1)) {
      // 右左、右右、左左はok
      stepnum[i][0] = min(stepnum[i - 1][1], stepnum[i - 1][0] + 1);
      stepnum[i][1] = stepnum[i - 1][1] + 1;
    } else if ((s1 == 1 && s2 == 0) || (s1 == 1 && s2 == 2) ||
               (s1 == 2 && s2 == 0)) {
      // 左右、左左、右右はok
      stepnum[i][0] = stepnum[i - 1][0] + 1;
      stepnum[i][1] = min(stepnum[i - 1][0], stepnum[i - 1][1] + 1);
    } else {
      // どの足でもok
      stepnum[i][0] = min(stepnum[i - 1][0] + 1, stepnum[i - 1][1]);
      stepnum[i][1] = min(stepnum[i - 1][1] + 1, stepnum[i - 1][0]);
    }
    s1 = s2;
  }
  cout << min(stepnum[s.length() - 1][0], stepnum[s.length() - 1][1]) << endl;
}

int main() {
  string s;
  while (1) {
    cin >> s;
    if (s == "#")
      break;
    solve(s);
  }
  return 0;
}
Problem p01360 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define f first
#define s second

string str;

int memo[2][3][3][100010];

int solve(int next, int left, int right, int p) {
  if (p == str.size() || !isdigit(str[p]))
    return 0;
  if (memo[next][left][right][p] != -1)
    return memo[next][left][right][p];

  int ret = 10000000;
  int nn = next;
  int tmp = 0;
  int st = (str[p] - '1') % 3;
  int l = (next == 0 ? st : left);
  int r = (next == 1 ? st : right);
  if (next == 0) {
    if (st > right) {
      tmp++;
      next ^= 1;
      r = st;
    } else
      l = st;
  } else {
    if (st < left) {
      tmp++;
      next ^= 1;
      l = st;
    } else
      r = st;
  }

  return memo[nn][left][right][p] = tmp + solve(next ^ 1, l, r, p + 1);
}

int main() {
  while (getline(cin, str) && str[0] != '#') {
    int ans = 100000000;
    REP(i, 2)
    REP(j, 3) REP(k, 3)
        memset(memo[i][j][k], -1, sizeof(int) * (str.size() + 2));
    ans = min(ans, solve(0, 0, 2, 0));
    ans = min(ans, solve(1, 0, 2, 0));
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define f first
#define s second

string str;

int memo[2][3][3][100010];
int ans;

int solve(int next, int left, int right, int p, int a) {
  if (a > ans)
    return 10000000;
  if (p == str.size())
    return ans = a;
  if (memo[next][left][right][p] != -1)
    return memo[next][left][right][p];

  int ret = 10000000;
  {
    // normal
    int tmp = 0;
    int st = (str[p] - '1') % 3;
    int l = (next == 0 ? st : left);
    int r = (next == 1 ? st : right);
    if (next == 0 && st > right)
      tmp++;
    if (next == 1 && st < left)
      tmp++;

    ret = min(ret, solve(next ^ 1, l, r, p + 1, a + tmp));
  }
  {
    // abnormal
    next ^= 1;
    int tmp = 1;
    int st = (str[p] - '1') % 3;
    int l = (next == 0 ? st : left);
    int r = (next == 1 ? st : right);
    if (next == 0 && st > right)
      tmp++;
    if (next == 1 && st < left)
      tmp++;

    ret = min(ret, solve(next ^ 1, l, r, p + 1, a + tmp));
  }

  return memo[next][left][right][p] = ret;
}

int main() {
  while (getline(cin, str) && str[0] != '#') {
    ans = 100000000;
    REP(i, 2)
    REP(j, 3) REP(k, 3)
        memset(memo[i][j][k], -1, sizeof(int) * (str.size() + 2));
    solve(0, 0, 2, 0, 0);

    if (ans == 0) {
      cout << ans << endl;
      continue;
    }

    REP(i, 2)
    REP(j, 3) REP(k, 3)
        memset(memo[i][j][k], -1, sizeof(int) * (str.size() + 2));

    solve(1, 0, 2, 0, 0);
    cout << ans << endl;
  }
  return 0;
}
Problem p01361 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int H, W, hp, HP;
char t[1005][1005];
int T, S, P;
map<char, int> mp;
vector<int> u;
int dp[1005][(1 << 12)];
int v[12];

int main() {

  while (1) {
    u.clear();
    cin >> hp >> HP;
    if (hp == 0 && HP == 0)
      break;
    cin >> H >> W;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> t[i][j];
    cin >> T;
    for (int i = 0; i < T; i++) {
      char ch;
      int num;
      cin >> ch >> num;
      mp[ch] = num;
    }
    int y = 0, x = 0;
    cin >> S;
    for (int i = 0; i < S; i++) {
      char ch;
      int num;
      cin >> ch >> num;
      while (num--) {
        if (ch == 'U')
          y--;
        if (ch == 'R')
          x++;
        if (ch == 'D')
          y++;
        if (ch == 'L')
          x--;
        u.push_back(mp[t[y][x]]);
      }
    }
    cin >> P;
    for (int i = 0; i < P; i++)
      cin >> v[i];
    memset(dp, 0, sizeof(dp));
    dp[0][0] = hp;
    int N = u.size();
    for (int i = 0; i < N; i++) {
      for (int S = 0; S < (1 << P); S++) {
        if (dp[i][S] == 0)
          continue;
        for (int k = 0; k < P; k++) {
          if (S >> k & 1)
            continue;
          int nS = S | (1 << k);
          dp[i][nS] = max(dp[i][nS], min(HP, dp[i][S] + v[k]));
        }
        dp[i + 1][S] = max(dp[i + 1][S], dp[i][S] - u[i]);
      }
    }
    string ans = "NO";
    for (int i = 0; i < (1 << P); i++)
      if (dp[N][i] > 0)
        ans = "YES";
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int H, W, hp, HP;
char t[1005][1005];
int T, S, P;
map<char, int> mp;
vector<int> u;
int dp[20005][(1 << 12)];
int v[12];

int main() {

  while (1) {
    u.clear();
    cin >> hp >> HP;
    if (hp == 0 && HP == 0)
      break;
    cin >> H >> W;
    for (int i = 0; i < H; i++)
      for (int j = 0; j < W; j++)
        cin >> t[i][j];
    cin >> T;
    for (int i = 0; i < T; i++) {
      char ch;
      int num;
      cin >> ch >> num;
      mp[ch] = num;
    }
    int y = 0, x = 0;
    cin >> S;
    for (int i = 0; i < S; i++) {
      char ch;
      int num;
      cin >> ch >> num;
      while (num--) {
        if (ch == 'U')
          y--;
        if (ch == 'R')
          x++;
        if (ch == 'D')
          y++;
        if (ch == 'L')
          x--;
        u.push_back(mp[t[y][x]]);
      }
    }
    cin >> P;
    for (int i = 0; i < P; i++)
      cin >> v[i];
    memset(dp, 0, sizeof(dp));
    dp[0][0] = hp;
    int N = u.size();
    for (int i = 0; i < N; i++) {
      for (int S = 0; S < (1 << P); S++) {
        if (dp[i][S] == 0)
          continue;
        for (int k = 0; k < P; k++) {
          if (S >> k & 1)
            continue;
          int nS = S | (1 << k);
          dp[i + 1][nS] = max(dp[i + 1][nS], min(HP, dp[i][S] + v[k]));
        }
        dp[i + 1][S] = max(dp[i + 1][S], dp[i][S] - u[i]);
      }
    }
    string ans = "NO";
    for (int i = 0; i < (1 << P); i++)
      if (dp[N][i] > 0)
        ans = "YES";
    cout << ans << endl;
  }
  return 0;
}
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

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

typedef pair<int, int> P;
typedef pair<int, P> Q;

const int INF = 1 << 28;

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<vector<int>> v(N), d(N), c(N);
    for (int i = 0; i < M; i++) {
      int x, y, z, w;
      cin >> x >> y >> z >> w;
      x--, y--;
      v[x].push_back(y);
      v[y].push_back(x);
      d[x].push_back(z);
      d[y].push_back(z);
      c[x].push_back(w);
      c[y].push_back(w);
    }

    vector<int> dp(N, INF);
    vector<int> cdp(N, INF);
    int ans = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    dp[0] = 0;
    cdp[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int now = p.second;
      int dist = p.first;

      if (dp[now] < dist)
        continue;
      ans += cdp[now];

      for (int i = 0; i < v[now].size(); i++) {
        int next = v[now][i];
        int ndist = dist + d[now][i];
        if (dp[next] > ndist) {
          dp[next] = ndist;
          cdp[next] = c[now][i];
          que.push(P(ndist, next));
        } else if (dp[next] == ndist) {
          cdp[next] = min(cdp[next], c[now][i]);
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

typedef pair<int, int> P;
typedef pair<int, P> Q;

const int INF = 1 << 28;

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {
    vector<vector<int>> v(M), d(M), c(M);
    for (int i = 0; i < M; i++) {
      int x, y, z, w;
      cin >> x >> y >> z >> w;
      x--, y--;
      v[x].push_back(y);
      v[y].push_back(x);
      d[x].push_back(z);
      d[y].push_back(z);
      c[x].push_back(w);
      c[y].push_back(w);
    }

    vector<int> dp(N, INF);
    priority_queue<P, vector<P>, greater<P>> que;
    dp[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int now = p.second;
      int dist = p.first;

      if (dp[now] < dist)
        continue;

      for (int i = 0; i < v[now].size(); i++) {
        int next = v[now][i];
        int ndist = dist + d[now][i];
        if (dp[next] > ndist) {
          dp[next] = ndist;
          que.push(P(ndist, next));
        }
      }
    }

    priority_queue<Q, vector<Q>, greater<Q>> q;
    vector<int> used(N);
    used[0] = 1;
    for (int i = 0; i < v[0].size(); i++) {
      q.push(Q(d[0][i], P(c[0][i], v[0][i])));
    }
    int ans = 0;
    while (q.size()) {
      Q p = q.top();
      q.pop();
      int next = p.second.second;
      int cost = p.second.first;

      if (used[next])
        continue;
      ans += cost;
      used[next] = 1;

      for (int i = 0; i < v[next].size(); i++) {
        if (dp[next] + d[next][i] == dp[v[next][i]]) {
          q.push(Q(d[next][i], P(c[next][i], v[next][i])));
        }
      }
    }

    cout << ans << endl;
  }

  return 0;
}