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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cost;
  Edge(){};
  Edge(int to0, int cost0) {
    to = to0;
    cost = cost0;
  }
};

void shortestPath(const vector<vector<Edge>> &edges, int start,
                  vector<int> &dist) {
  dist.assign(edges.size(), INT_MAX);
  dist[start] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, start));

  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (unsigned i = 0; i < edges[v].size(); ++i) {
      Edge e = edges[v][i];
      if (dist[v] + e.cost < dist[e.to]) {
        dist[e.to] = dist[v] + e.cost;
        q.push(make_pair(dist[e.to], e.to));
      }
    }
  }

  replace(dist.begin(), dist.end(), INT_MAX, -1);
}

void shortestPath(const vector<vector<Edge>> &edges,
                  vector<vector<int>> &dist) {
  dist.resize(edges.size());
  for (unsigned i = 0; i < edges.size(); ++i)
    shortestPath(edges, i, dist[i]);
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    int m = 0;
    map<int, int> index;
    vector<int> num;

    vector<vector<Edge>> edges;
    for (int i = 0; i < n; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      if (index.find(a) != index.end())
        a = index[a];
      else {
        index.insert(make_pair(a, m));
        num.push_back(a);
        a = m;
        ++m;
        edges.resize(m);
      }
      if (index.find(b) != index.end())
        b = index[b];
      else {
        index.insert(make_pair(b, m));
        num.push_back(b);
        b = m;
        ++m;
        edges.resize(m);
      }

      edges[a].push_back(Edge(b, c));
      edges[b].push_back(Edge(a, c));
    }

    vector<vector<int>> dist;
    shortestPath(edges, dist);

    int k;
    int sum = INT_MAX;
    for (int i = 0; i < m; ++i) {
      int tmp = accumulate(dist[i].begin(), dist[i].end(), 0);
      if (tmp < sum) {
        sum = tmp;
        k = i;
      }
    }
    cout << num[k] << ' ' << sum << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

class Edge {
public:
  int to, cost;
  Edge(){};
  Edge(int to0, int cost0) {
    to = to0;
    cost = cost0;
  }
};

void shortestPath(const vector<vector<Edge>> &edges, int start,
                  vector<int> &dist) {
  dist.assign(edges.size(), INT_MAX);
  dist[start] = 0;
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, start));

  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.pop();
    int v = p.second;
    if (dist[v] < p.first)
      continue;
    for (unsigned i = 0; i < edges[v].size(); ++i) {
      Edge e = edges[v][i];
      if (dist[v] + e.cost < dist[e.to]) {
        dist[e.to] = dist[v] + e.cost;
        q.push(make_pair(dist[e.to], e.to));
      }
    }
  }

  replace(dist.begin(), dist.end(), INT_MAX, 0);
}

void shortestPath(const vector<vector<Edge>> &edges,
                  vector<vector<int>> &dist) {
  dist.resize(edges.size());
  for (unsigned i = 0; i < edges.size(); ++i)
    shortestPath(edges, i, dist[i]);
}

int main() {
  for (;;) {
    int n;
    cin >> n;
    if (n == 0)
      return 0;

    vector<vector<Edge>> edges(10);
    for (int i = 0; i < n; ++i) {
      int a, b, c;
      cin >> a >> b >> c;
      edges[a].push_back(Edge(b, c));
      edges[b].push_back(Edge(a, c));
    }

    vector<vector<int>> dist;
    shortestPath(edges, dist);

    int k;
    int sum = INT_MAX;
    for (int i = 0; i < n; ++i) {
      int tmp = accumulate(dist[i].begin(), dist[i].end(), 0);
      if (tmp != 0 && tmp < sum) {
        sum = tmp;
        k = i;
      }
    }
    cout << k << ' ' << sum << endl;
  }
}