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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

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

vector<Edge> es[110];
int d[110][2];
bool used[110];

void dijkstra() {
  d[0][1] = d[0][0] = 0;
  used[0] = true;
  set<pair<int, pii>> st;
  st.insert(mp(0, mp(0, 1)));
  st.insert(mp(0, mp(0, 0)));
  pair<int, pii> p;
  while (!st.empty()) {
    p = *st.begin();
    st.erase(st.begin());
    int from = p.se.fi, f = p.se.se, c = d[from][f];
    for (int i = 0; i < es[from].size(); i++) {
      int to = es[from][i].to, cost = c + es[from][i].c;
      if (d[to][f] > cost) {
        d[to][f] = cost;
        st.insert(mp(cost, mp(to, f)));
      }
      if (f == 1) {
        for (int j = 0; j < es[to].size(); j++) {
          int to2 = es[to][j].to;
          if (d[to2][0] > c) {
            d[to2][0] = c;
            st.insert(mp(c, mp(to2, 0)));
          }
        }
      }
    }
    used[from] = true;
  }
}

int main() {
  while (1) {
    int n = in.nextInt(), m = in.nextInt();
    if (n == 0 && m == 0)
      break;
    rep(i, 110) es[i].clear();
    rep(i, 110) rep(j, 2) d[i][j] = INF;
    rep(i, 110) used[i] = false;
    rep(i, m) {
      int a = in.nextInt() - 1, b = in.nextInt() - 1, c = in.nextInt();
      es[a].pb(Edge(b, c));
      es[b].pb(Edge(a, c));
    }

    dijkstra();

    printf("%d\\n", min(d[n - 1][1], d[n - 1][0]));
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<double, int> pdi;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const double EPS = 1e-8;
const int mod = 1e9 + 7;
const ll LINF = 1e18;
const double PI = acos(-1);

struct Input {
  int n;
  char c;
  char str[1024];
  string s;
  ll l;
  int nextInt() {
    scanf("%d", &n);
    return n;
  }
  ll nextLong() {
    scanf("%lld", &l);
    return l;
  }
  char nextChar() {
    scanf("%c", &c);
    return c;
  }
  string next() {
    scanf("%s", str);
    return string(str);
  }
  string nextLine() {
    getline(cin, s);
    return s;
  }
};
Input in;

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

vector<Edge> es[110];
int d[110][2];
bool used[110];

void dijkstra() {
  d[0][1] = d[0][0] = 0;
  used[0] = true;
  set<pii> st;
  st.insert(mp(0, 0));
  pii p;
  while (!st.empty()) {
    p = *st.begin();
    st.erase(st.begin());
    int from = p.se, c = p.fi;
    for (int i = 0; i < es[from].size(); i++) {
      int to = es[from][i].to, cost = c + es[from][i].c;
      if (used[to])
        continue;

      if (d[to][1] > cost)
        d[to][1] = cost;
      if (d[to][0] > d[from][0] + es[from][i].c)
        d[to][0] = d[from][c] + es[from][i].c;
      for (int j = 0; j < es[to].size(); j++) {
        d[es[to][j].to][0] = min(d[es[to][j].to][0], d[from][1]);
      }
      st.insert(mp(cost, to));
    }
    used[from] = true;
  }
}

int main() {
  while (1) {
    // int n = in.nextInt(), m = in.nextInt();
    int n, m;
    scanf("%d %d", &n, &m);
    if (n == 0 && m == 0)
      break;
    rep(i, 110) es[i].clear();
    rep(i, 110) rep(j, 2) d[i][j] = INF;
    rep(i, 110) used[i] = false;
    int a, b, c;
    rep(i, m) {
      // int a = in.nextInt() - 1, b = in.nextInt() - 1, c = in.nextInt();
      // if(a < 0 || b < 0 || a >= n || b >= n) continue;
      scanf("%d%d%d", &a, &b, &c);
      a--;
      b--;
      es[a].pb(Edge(b, c));
      es[b].pb(Edge(a, c));
    }

    dijkstra();

    printf("%d\\n", min(d[n - 1][1], d[n - 1][0]));
  }
}