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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int n, m, u, v, vis[100007], id[100007];
long long c, dis[100007], d, ans = 1e16, now = 0;
vector<pair<int, long long>> edge[100007];
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    pr;
int cmp(int a, int b) {
  return (dis[a] < dis[b] || (dis[a] == dis[b] && a < b));
}
signed main() {
  // ios_base::sync_with_stdio(0);
  cin >> n >> m >> c;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v >> d;
    edge[u].pb(mp(v, d));
    edge[v].pb(mp(u, d));
    now += d;
  }
  for (int i = 1; i <= n; i++)
    dis[i] = 1e16;
  dis[1] = 0;
  pr.push(mp(0, 1));
  while (!pr.empty()) {
    long long val = pr.top().fi;
    int u = pr.top().se;
    pr.pop();
    if (dis[u] != val)
      continue;
    for (int i = 0; i < (int)edge[u].size(); i++) {
      int v = edge[u][i].fi;
      long long l = edge[u][i].se;
      if (dis[v] > val + l) {
        dis[v] = val + l;
        pr.push(mp(dis[v], v));
      }
    }
  }
  for (int i = 1; i <= n; i++)
    id[i] = i;
  sort(id + 1, id + 1 + n, cmp);
  ans = now;
  vis[1] = 1;
  vis[n + 1] = 1e16;
  id[n + 1] = n + 1;
  for (int i = 2; i <= n; i++) {
    int j = id[i];
    // cout<<dis[j]<<endl;
    for (int ii = 0; ii < edge[j].size(); ii++) {
      v = edge[j][ii].fi;
      d = edge[j][ii].se;
      if (vis[v] == 0)
        continue;
      now -= d;
    }
    vis[j] = 1;
    if (dis[j] == dis[id[i + 1]])
      continue;
    ans = min(ans, now + c * dis[j]);
  }
  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define mp make_pair
#define pb push_back
#define fi first
#define se second
int n, m, u, v, vis[100007], id[100007];
long long c, dis[100007], d, ans = 1e16, now = 0;
vector<pair<int, long long>> edge[100007];
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    pr;
int cmp(int a, int b) { return dis[a] <= dis[b]; }
signed main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> c;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v >> d;
    edge[u].pb(mp(v, d));
    edge[v].pb(mp(u, d));
    now += d;
  }
  for (int i = 1; i <= n; i++)
    dis[i] = 1e16;
  dis[1] = 0;
  pr.push(mp(0, 1));
  while (!pr.empty()) {
    long long val = pr.top().fi;
    u = pr.top().se;
    pr.pop();
    if (dis[u] != val)
      continue;
    for (int i = 0; i < (int)edge[u].size(); i++) {
      v = edge[u][i].fi;
      long long l = edge[u][i].se;
      if (dis[v] > dis[u] + l) {
        dis[v] = dis[u] + l;
        pr.push(mp(dis[v], v));
      }
    }
  }
  for (int i = 1; i <= n; i++)
    id[i] = i;
  sort(id + 1, id + 1 + n, cmp);
  ans = now;
  vis[1] = 1;
  for (int i = 2; i <= n; i++) {
    int j = id[i];
    // cout<<dis[j]<<endl;
    for (int ii = 0; ii < edge[j].size(); ii++) {
      v = edge[j][ii].fi;
      d = edge[j][ii].se;
      if (vis[v] == 0)
        continue;
      now -= d;
    }
    vis[j] = 1;
    ans = min(ans, now + c * dis[j]);
  }
  cout << ans << endl;
}