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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;

struct edge {
  int to, cost;
};

int n, d[100001];
const int inf = 1 << 30;
vector<edge> g[100001];

void dijkstra(void) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  fill(d + 1, d + n + 1, inf);
  d[1] = 0;
  q.push(pair<int, int>(0, 1));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.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;
        q.push(pair<int, int>(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int m, c;
  long long int x, S = 0;
  cin >> n >> m >> c;
  for (int i = 0; i < m; ++i) {
    int j, k, l;
    cin >> j >> k >> l;
    g[j].push_back((edge){k, l});
    g[k].push_back((edge){j, l});
    S += l;
  }
  dijkstra();
  vector<pair<int, int>> v;
  for (int i = 1; i <= n; ++i) {
    v.push_back(pair<int, int>(d[i], i));
  }
  sort(v.begin(), v.end());
  x = S;
  long long int dS = 0;
  for (int i = 1; i < n; ++i) {
    int a = v[i].second;
    for (int j = 0; j < g[a].size(); ++j) {
      int b = g[a][j].to;
      if (d[b] < d[a]) {
        S -= g[a][j].cost;
      } else if (d[b] == d[a]) {
        dS += g[a][j].cost;
      }
    }
    if (i == n - 1 || d[a] < v[i + 1].first) {
      S -= (dS / 2);
      dS = 0;
      x = min(x, S + (long long int)c * d[a]);
    }
  }
  cout << x << endl;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
using namespace std;

struct edge {
  int to, cost;
};

int n;
int d[100001];
const int inf = 1 << 30;
vector<edge> g[100001];

void dijkstra(void) {
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  fill(d + 1, d + n + 1, inf);
  d[1] = 0;
  q.push(pair<int, int>(0, 1));
  while (!q.empty()) {
    pair<int, int> p = q.top();
    q.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;
        q.push(pair<int, int>(d[e.to], e.to));
      }
    }
  }
}

int main() {
  int m, c;
  long long int x, S = 0;
  bool f[100001][100001];
  cin >> n >> m >> c;
  for (int i = 0; i < m; ++i) {
    int j, k, l;
    cin >> j >> k >> l;
    g[j].push_back((edge){k, l});
    g[k].push_back((edge){j, l});
    S += l;
  }
  dijkstra();
  vector<pair<int, int>> v;
  for (int i = 1; i <= n; ++i) {
    v.push_back(pair<int, int>(d[i], i));
    fill(f[i] + 1, f[i] + n + 1, true);
  }
  sort(v.begin(), v.end(), greater<pair<int, int>>());
  x = S;
  for (int i = 1; i < n; ++i) {
    for (int j = 0; j < g[v[i].second].size(); ++j) {
      int z = v[i].second, y = g[v[i].second][j].to;
      if (f[z][y] && d[y] <= d[z]) {
        S -= g[z][j].cost;
        f[z][y] = false;
        f[y][z] = false;
      }
    }
    x = min(x, S + (long long)c * v[i].first);
  }
  cout << x << endl;
}