Problem p00890 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;

vector<pair<int, int>> v[105];
int in[105][105], dp[105][105];

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  int N, M, n, m, i, j, C, a, b, c;
  while (scanf(" %d %d %d", &N, &M, &C) == 3 && (M + N + C)) {
    for (n = 1; n <= N; n++)
      v[n].clear();
    for (m = 0; m < M; m++) {
      scanf(" %d %d %d", &a, &b, &c);
      v[a].push_back(make_pair(b, c));
    }
    memset(in, 0, sizeof(in));
    queue<int> q;
    for (i = 0; i < 105; i++)
      for (j = 0; j < 105; j++)
        dp[i][j] = 1e9;
    q.push(1);
    q.push(0);
    dp[1][0] = 0;
    while (!q.empty()) {
      int now = q.front();
      q.pop();
      int cnt = q.front();
      q.pop();
      in[now][cnt] = 0;
      /*if(now==10 && cnt==0){
          printf("==%d\\n",dp[N][cnt]);
      }*/
      if (now == N)
        continue;
      if (cnt == N)
        continue;
      for (i = 0; i < v[now].size(); i++) {
        if (dp[now][cnt] + v[now][i].second < dp[v[now][i].first][cnt]) {
          dp[v[now][i].first][cnt] = dp[now][cnt] + v[now][i].second;
          // printf("-> %d %d
          // %d\\n",v[now][i].first,cnt,dp[v[now][i].first][cnt]);
          if (in[v[now][i].first][cnt] == 0) {
            in[v[now][i].first][cnt] = 1;
            q.push(v[now][i].first);
            q.push(cnt);
          }
        }
        if (dp[now][cnt] < dp[v[now][i].first][cnt + 1]) {
          dp[v[now][i].first][cnt + 1] = dp[now][cnt];
          // printf("==> %d %d
          // %d\\n",v[now][i].first,cnt+1,dp[v[now][i].first][cnt+1]);
          if (in[v[now][i].first][cnt + 1] == 0) {
            in[v[now][i].first][cnt + 1] = 1;
            q.push(v[now][i].first);
            q.push(cnt + 1);
          }
        }
      }
    }
    // printf("N = %d %d\\n",N,dp[N][0]);
    for (i = 0; i <= N; i++)
      if (dp[N][i] <= C)
        break;
    printf("%d\\n", i);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;

int dp[105][105], dis[105][105], in[105][105];
vector<int> v[105];

int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);

  int N, n, M, m, C, i, j, k, cnt, now;
  while (~scanf(" %d %d %d", &N, &M, &C) && (N + M + C)) {
    for (n = 0; n <= N; n++)
      v[n].clear();
    for (i = 0; i < 105; i++)
      for (j = 0; j < 105; j++)
        dp[i][j] = dis[i][j] = 1e9;
    for (m = 0; m < M; m++) {
      scanf(" %d %d %d", &i, &j, &k);
      dis[i][j] = k;
      v[i].push_back(j);
    }
    dp[0][1] = 0;
    queue<int> q;
    q.push(0);
    q.push(1);
    memset(in, 0, sizeof(in));
    in[0][1] = 1;
    while (!q.empty()) {
      cnt = q.front();
      q.pop();
      now = q.front();
      q.pop();
      in[cnt][now] = 0;
      for (i = 0; i < v[now].size(); i++) {
        if (dp[cnt][now] + dis[now][v[now][i]] < dp[cnt][v[now][i]]) {
          dp[cnt][v[now][i]] = dp[cnt][now] + dis[now][v[now][i]];
          if (in[cnt][v[now][i]] == 0) {
            in[cnt][v[now][i]] = 1;
            q.push(cnt);
            q.push(v[now][i]);
          }
        }
        if (dp[cnt][now] < dp[cnt + 1][v[now][i]]) {
          dp[cnt + 1][v[now][i]] = dp[cnt][now];
          if (in[cnt + 1][v[now][i]] == 0) {
            in[cnt + 1][v[now][i]] = 1;
            q.push(cnt + 1);
            q.push(v[now][i]);
          }
        }
      }
    }
    // printf("=%d\\n",dp[2][N]);
    for (i = 0; i <= N; i++)
      if (dp[i][N] <= C)
        break;
    printf("%d\\n", i);
  }

  return 0;
}