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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll a[100001];
struct P {
  ll pos, cost;
};
struct B {
  ll to, cost;
};
struct L {
  ll from, to, cost;
};
vector<B> e[100001];
L l[200000];
struct T {
  ll x, cost;
};
T lo[200000];
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
bool operator<(T a, T b) { return a.x < b.x; }
const ll inf = 8000000000000000;
int main() {
  ll n, m, c;
  while (cin >> n >> m >> c) {
    for (ll i = 1; i <= n; i++) {
      a[i] = inf;
      e[i].clear();
    }
    ll r = 0;
    for (ll i = 0; i < m; i++) {
      ll u1, u2, u3;
      cin >> u1 >> u2 >> u3;
      e[u1].push_back(B{u2, u3});
      e[u2].push_back(B{u1, u3});
      l[i] = L{u1, u2, u3};
      r += u3;
    }
    priority_queue<P, vector<P>, greater<P>> p;
    a[1] = 0;
    p.push(P{1, 0});
    while (!p.empty()) {
      P t = p.top();
      p.pop();
      for (ll i = 0; i < (ll)e[t.pos].size(); i++) {
        if (a[t.pos] + e[t.pos][i].cost < a[e[t.pos][i].to]) {
          a[e[t.pos][i].to] = a[t.pos] + e[t.pos][i].cost;
          p.push(P{e[t.pos][i].to, a[e[t.pos][i].to]});
        }
      }
    }
    for (ll i = 0; i < m; i++) {
      ll k = max(a[l[i].from], a[l[i].to]);
      lo[i] = T{k, l[i].cost};
    }
    sort(lo, lo + m);
    ll sum = r;
    ll sum3 = r;
    for (ll i = 0; i < m;) {
      ll k = lo[i].x;
      while (i < m && lo[i].x == k) {
        sum -= lo[i].cost;
        i++;
      }
      ll sum4 = sum + k * c;
      sum3 = min(sum3, sum4);
    }
    cout << sum3 << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
ll a[100001];
struct P {
  ll pos, cost;
};
struct B {
  ll to, cost;
};
struct L {
  ll from, to, cost;
};
vector<B> e[100001];
L l[200000];
struct T {
  ll x, cost;
};
T lo[200000];
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
bool operator<(T a, T b) { return a.x < b.x; }
const ll inf = 8000000000000000;
int main() {
  ll n, m, c;
  while (cin >> n >> m >> c) {
    for (ll i = 1; i <= n; i++) {
      a[i] = inf;
      e[i].clear();
    }
    ll r = 0;
    for (ll i = 0; i < m; i++) {
      ll u1, u2, u3;
      cin >> u1 >> u2 >> u3;
      e[u1].push_back(B{u2, u3});
      e[u2].push_back(B{u1, u3});
      l[i] = L{u1, u2, u3};
      r += u3;
    }
    priority_queue<P, vector<P>, greater<P>> p;
    a[1] = 0;
    p.push(P{1, 0});
    while (!p.empty()) {
      P t = p.top();
      p.pop();
      for (ll i = 0; i < (ll)e[t.pos].size(); i++) {
        if (a[t.pos] + e[t.pos][i].cost < a[e[t.pos][i].to]) {
          a[e[t.pos][i].to] = a[t.pos] + e[t.pos][i].cost;
          p.push(P{e[t.pos][i].to, a[e[t.pos][i].to]});
        }
      }
    }
    for (ll i = 0; i < m; i++) {
      ll k = max(a[l[i].from], a[l[i].to]);
      lo[i] = T{k, l[i].cost};
    }
    sort(lo, lo + m);
    ll sum = r;
    ll sum3 = inf;
    for (ll i = 0; i < m; i++) {
      ll sum2 = 0, k = lo[k].x;
      while (i < m && lo[i].x == k) {
        sum2 += lo[i].cost;
      }
      sum -= sum2;
      ll sum4 = sum + k * c;
      sum3 = min(sum3, sum4);
    }
    cout << sum3 << endl;
  }
}