Problem p04003 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

#define MAX_V 500000
#define LL long long
using namespace std;

const int INF = 1000000000;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int input[3][6 * 200000];
map<P, int> m;
int cnt2 = 0;
int N, M;
void f(P x, P y, int c) {
  input[0][cnt2] = m[x];
  input[1][cnt2] = m[y], input[2][cnt2] = c;
  cnt2++;
}

int main(void) {

  cin >> N >> M;
  int cnt1 = 1;

  for (int i = 0; i < N; i++) {
    m.insert(make_pair(make_pair(i, -1), cnt1++));
  }

  for (int i = 0; i < M; i++) {
    int a, b, c;
    scanf(" %d %d %d", &a, &b, &c);
    a--;
    b--;
    P eg0 = make_pair(a, c), eg1 = make_pair(b, c);
    P ng0 = make_pair(a, -1), ng1 = make_pair(b, -1);
    if (m[eg0] == 0) {
      m[eg0] = cnt1;
      cnt1++;
      f(eg0, ng0, 0);
      f(ng0, eg0, 1);
    }
    if (m[eg1] == 0) {
      m[eg1] = cnt1;
      cnt1++;
      f(eg1, ng1, 0);
      f(ng1, eg1, 1);
    }
    f(eg0, eg1, 0);
    f(eg1, eg0, 0);
  }
  int end = m[make_pair(N - 1, -1)];
  for (int i = 0; i < cnt2; i++) {
    edge *e = new edge;
    e->to = input[1][i];
    e->cost = input[2][i];
    G[input[0][i]].push_back(*e);
  }
  V = cnt1;
  dijkstra(1);

  int ans = (d[end] == INF ? -1 : d[end]);
  cout << ans << endl;
  return (0);
}


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>

#define MAX_V 600000
#define LL long long
using namespace std;

const int INF = 1000000000;

struct edge {
  int to, cost;
};
typedef pair<int, int> P;

int V;
vector<edge> G[MAX_V];
int d[MAX_V];

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}

int input[3][6 * 200000];
map<P, int> m;
int cnt2 = 0;
int N, M;
void f(P x, P y, int c) {
  input[0][cnt2] = m[x];
  input[1][cnt2] = m[y], input[2][cnt2] = c;
  cnt2++;
}

int main(void) {

  cin >> N >> M;
  int cnt1 = 1;

  for (int i = 0; i < N; i++) {
    m.insert(make_pair(make_pair(i, -1), cnt1++));
  }

  for (int i = 0; i < M; i++) {
    int a, b, c;
    scanf(" %d %d %d", &a, &b, &c);
    a--;
    b--;
    P eg0 = make_pair(a, c), eg1 = make_pair(b, c);
    P ng0 = make_pair(a, -1), ng1 = make_pair(b, -1);
    if (m[eg0] == 0) {
      m[eg0] = cnt1;
      cnt1++;
      f(eg0, ng0, 0);
      f(ng0, eg0, 1);
    }
    if (m[eg1] == 0) {
      m[eg1] = cnt1;
      cnt1++;
      f(eg1, ng1, 0);
      f(ng1, eg1, 1);
    }
    f(eg0, eg1, 0);
    f(eg1, eg0, 0);
  }
  int end = m[make_pair(N - 1, -1)];
  for (int i = 0; i < cnt2; i++) {
    edge *e = new edge;
    e->to = input[1][i];
    e->cost = input[2][i];
    G[input[0][i]].push_back(*e);
  }
  V = cnt1;
  dijkstra(1);

  int ans = (d[end] == INF ? -1 : d[end]);
  cout << ans << endl;
  return (0);
}