Problem p04003 (C++)

Buggy submission (status: Runtime Error):
// by tzupengwang™
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

int n, m;
ii edg[400005];
int cpy[400005];
int spid[400005];
vector<int> v[100005];
vector<int> spv[100005];
set<int> st[100005];
map<ii, int> smap;
int mpid;
int dis[400005];

void init() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    edg[i * 2] = ii(x, y);
    cpy[i * 2] = z;
    if (smap.count(ii(x, z)))
      spid[i * 2] = smap[ii(x, z)];
    else
      smap[ii(x, z)] = mpid, spid[i * 2] = mpid++;
    spv[spid[i * 2]].push_back(i * 2);
    v[x].push_back(i * 2);
    edg[i * 2 + 1] = ii(y, x);
    cpy[i * 2 + 1] = z;
    if (smap.count(ii(y, z)))
      spid[i * 2 + 1] = smap[ii(y, z)];
    else
      smap[ii(y, z)] = mpid, spid[i * 2 + 1] = mpid++;
    spv[spid[i * 2 + 1]].push_back(i * 2 + 1);
    v[y].push_back(i * 2 + 1);
  }
}

priority_queue<ii, vector<ii>, greater<ii>> pq;

void process() {
  memset(dis, -1, sizeof dis);
  for (int id : v[1]) {
    pq.push(ii(1, id));
  }
  while (!pq.empty()) {
    ii nnw = pq.top();
    pq.pop();
    int nid = nnw.second, dss = nnw.first;
    int now = edg[nid].second;
    int nwc = cpy[nid];
    if (now == n) {
      printf("%d\n", dss);
      return;
    }
    if (dis[nid] != -1)
      continue;
    dis[nid] = dss;
    if (st[now].count(nwc))
      continue;
    if (st[now].size() == 0) {
      for (int id : v[now])
        if (dis[id] == -1) {
          if (nwc == cpy[id]) {
            pq.push(ii(dss, id));
          } else {
            pq.push(ii(dss + 1, id));
          }
        }
    } else {
      if (smap.count(ii(now, nwc))) {
        int iddd = smap[ii(now, nwc)];
        for (int id : spv[iddd])
          if (dis[id] == -1) {
            pq.push(ii(dss, id));
          }
      }
    }
    st[now].insert(cpy[nid]);
  }
  printf("-1\n");
}

int main() {
  init();
  process();
  return 0;
}


Fixed submission:
// by tzupengwang™
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;

int n, m;
ii edg[400005];
int cpy[400005];
int spid[400005];
vector<int> v[100005];
vector<int> spv[400005];
set<int> st[100005];
map<ii, int> smap;
int mpid;
int dis[400005];

void init() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    edg[i * 2] = ii(x, y);
    cpy[i * 2] = z;
    if (smap.count(ii(x, z)))
      spid[i * 2] = smap[ii(x, z)];
    else
      smap[ii(x, z)] = mpid, spid[i * 2] = mpid++;
    spv[spid[i * 2]].push_back(i * 2);
    v[x].push_back(i * 2);
    edg[i * 2 + 1] = ii(y, x);
    cpy[i * 2 + 1] = z;
    if (smap.count(ii(y, z)))
      spid[i * 2 + 1] = smap[ii(y, z)];
    else
      smap[ii(y, z)] = mpid, spid[i * 2 + 1] = mpid++;
    spv[spid[i * 2 + 1]].push_back(i * 2 + 1);
    v[y].push_back(i * 2 + 1);
  }
}

priority_queue<ii, vector<ii>, greater<ii>> pq;

void process() {
  memset(dis, -1, sizeof dis);
  for (int id : v[1]) {
    pq.push(ii(1, id));
  }
  while (!pq.empty()) {
    ii nnw = pq.top();
    pq.pop();
    int nid = nnw.second, dss = nnw.first;
    int now = edg[nid].second;
    int nwc = cpy[nid];
    if (now == n) {
      printf("%d\n", dss);
      return;
    }
    if (dis[nid] != -1)
      continue;
    dis[nid] = dss;
    if (st[now].count(nwc))
      continue;
    if (st[now].size() == 0) {
      for (int id : v[now])
        if (dis[id] == -1) {
          if (nwc == cpy[id]) {
            pq.push(ii(dss, id));
          } else {
            pq.push(ii(dss + 1, id));
          }
        }
    } else {
      if (smap.count(ii(now, nwc))) {
        int iddd = smap[ii(now, nwc)];
        for (int id : spv[iddd])
          if (dis[id] == -1) {
            pq.push(ii(dss, id));
          }
      }
    }
    st[now].insert(cpy[nid]);
  }
  printf("-1\n");
}

int main() {
  init();
  process();
  return 0;
}