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

/* ***********************************************
Author        :kuangbin
Created Time  :2013/11/15 16:30:39
File Name     :G.cpp
************************************************ */

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
struct Edge {
  int to, next;
  int w;
} edge[20010];
int head[110];
int tot;
void addedge(int u, int v, int w) {
  edge[tot].to = v;
  edge[tot].next = head[u];
  edge[tot].w = w;
  head[u] = tot++;
}

void init() {
  tot = 0;
  memset(head, -1, sizeof(head));
}
int dp[110][1010];
int n, m;
bool vis[110][1010];
void SPFA(int s) {
  queue<pair<int, int>> q;
  q.push(make_pair(s, 0));
  for (int i = 1; i <= n; i++)
    for (int j = 0; j <= m; j++)
      dp[i][j] = INF;
  memset(vis, false, sizeof(vis));
  dp[s][0] = 0;
  vis[s][0] = true;
  while (!q.empty()) {
    pair<int, int> tmp = q.front();
    int u = tmp.first;
    int k = tmp.second;
    // printf("%d %d\\n",u,k);
    q.pop();
    vis[u][k] = false;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      // cout<<v<<endl;
      //	printf("%d %d\\n",dp[v][k],dp[u][k] + edge[i].w);

      if (dp[v][k] > dp[u][k] + edge[i].w) {
        //	cout<<"*"<<endl;
        dp[v][k] = dp[u][k] + edge[i].w;
        if (!vis[v][k]) {
          q.push(make_pair(v, k));
          vis[v][k] = true;
        }
      }

      if (dp[v][k + 1] > dp[u][k]) {
        dp[v][k + 1] = dp[u][k];
        if (!vis[v][k + 1]) {
          q.push(make_pair(v, k + 1));
          vis[v][k + 1] = true;
        }
      }
    }
  }
}
int main() {
  // freopen("in.txt","r",stdin);
  // freopen("out.txt","w",stdout);
  int c;
  while (scanf("%d%d%d", &n, &m, &c) == 3) {
    if (n == 0 && m == 0 && c == 0)
      break;
    init();
    int u, v, w;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d", &u, &v, &w);
      addedge(u, v, w);
    }
    SPFA(1);
    //	for(int i = 1;i <= n;i++)
    //		for(int j = 0;j <= m;j++)
    //			printf(" %d %d %d\\n",i,j,dp[i][j]);
    int ans = m;
    for (int i = 0; i <= m; i++)
      if (dp[n][i] <= c) {
        ans = i;
        break;
      }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;

struct node {
  int shu;
  int next, fa;
} s[1005];
int head[105];
struct nod {
  int shu[105];
  bool used[105];
  int l, sum, wei, gai;
};
int ans[105];
int n, m, c;

int main() {
  while (scanf("%d%d%d", &n, &m, &c) != EOF) {
    if (!n && !m && !c)
      break;
    memset(head, -1, sizeof(head));
    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d%d%d", &a, &b, &c);
      s[i].shu = c;
      s[i].next = head[a];
      s[i].fa = b;
      head[a] = i;
    }
    for (int i = 0; i <= n; i++) {
      ans[i] = 2000;
    }
    ans[1] = 0;
    queue<nod> ss;
    nod a, b;
    a.l = 1;
    a.sum = 0;
    a.wei = 1;
    a.gai = 0;
    a.used[1] = true;
    for (int i = 2; i <= n; i++) {
      a.used[i] = false;
    }
    ss.push(a);
    while (!ss.empty()) {
      a = ss.front();
      ss.pop();
      b = a;
      b.l++;
      int wei = a.wei;
      // cout<<wei<<endl;
      int j = head[wei];
      while (j != -1) {
        if (!b.used[s[j].fa]) {
          b.shu[a.l] = s[j].fa;
          b.used[s[j].fa] = true;
          b.sum = b.sum + s[j].shu;
          b.wei = s[j].fa;
          if (b.sum > c) {
            int max = 0;
            int i;
            for (i = 1; i < b.l; i++) {
              if (b.shu[max] < b.shu[i])
                max = i;
            }
            b.sum -= b.shu[max];
            b.shu[max] = 0;
            b.gai++;
          }
          ans[s[j].fa] = min(b.gai, ans[s[j].fa]);
          if (b.wei != n) {
            ss.push(b);
          }
          b.used[s[j].fa] = false;
          // cout<<s[j].fa;
        }
        j = s[j].next;
      }
    }
    printf("%d\\n", ans[n]);
  }
  return 0;
}