Problem p01366 (C++) — buggy submission, verdict: Time Limit Exceeded

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL << 60);
#else
const int INF = (1LL << 30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int u[20010], v[20010], d[20010], c[20010];

struct edge {
  int to, cost, c;
};

int n, D[10010];
vector<edge> g[10010];

void dijkstra(int s) {
  priority_queue<PII, vector<PII>, greater<PII>> que;
  fill(D, D + n, INF);
  D[s] = 0;
  que.push(PII{0, s});

  while (que.size()) {
    PII p = que.top();
    que.pop();
    int v = p.second;
    if (D[v] < p.first)
      continue;
    for (edge e : g[v]) {
      if (D[e.to] > D[v] + e.cost) {
        D[e.to] = D[v] + e.cost;
        que.push(PII{D[e.to], e.to});
      }
    }
  }
}

signed main(void) {
  while (true) {
    int m;
    cin >> n >> m;
    if (!n)
      break;
    REP(i, n) g[i].clear();
    REP(i, m) {
      cin >> u[i] >> v[i] >> d[i] >> c[i];
      u[i]--, v[i]--;
      g[u[i]].PB({v[i], d[i], c[i]});
      g[v[i]].PB({u[i], d[i], c[i]});
    }
    // 0??????????????¢????±???????
    dijkstra(0);

    int ret = 0;
    FOR(i, 1, n) {
      int tmp = INF;
      for (edge j : g[i]) {
        if (D[i] == D[j.to] + j.cost) {
          chmin(tmp, j.c);
        }
      }
      ret += tmp;
    }

    cout << ret << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL << 60);
#else
const int INF = (1LL << 30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int u[20010], v[20010], d[20010], c[20010];

struct edge {
  int to, cost;
};

int n, D[10010];
vector<edge> g[10010];

void dijkstra(int s) {
  priority_queue<PII, vector<PII>, greater<PII>> que;
  fill(D, D + n, INF);
  D[s] = 0;
  que.push(PII{0, s});

  while (que.size()) {
    PII p = que.top();
    que.pop();
    int v = p.second;
    if (D[v] < p.first)
      continue;
    for (edge e : g[v]) {
      if (D[e.to] > D[v] + e.cost) {
        D[e.to] = D[v] + e.cost;
        que.push(PII{D[e.to], e.to});
      }
    }
  }
}

PII p[10010];
map<PII, int> G, H;

signed main(void) {
  while (true) {
    int m;
    cin >> n >> m;
    if (!n)
      break;
    G.clear();
    H.clear();
    REP(i, n) g[i].clear();
    REP(i, m) {
      cin >> u[i] >> v[i] >> d[i] >> c[i];
      u[i]--, v[i]--;
      g[u[i]].PB({v[i], d[i]});
      g[v[i]].PB({u[i], d[i]});
      G[{u[i], v[i]}] = G[{v[i], u[i]}] = d[i];
      H[{u[i], v[i]}] = H[{v[i], u[i]}] = c[i];
    }
    // 0??????????????¢????±???????
    dijkstra(0);
    // REP(i, n) cout << D[i] << " "; cout << endl;
    REP(i, n) p[i] = {D[i], i};
    sort(p, p + n);

    set<int> st;
    st.insert(0);

    int ret = 0;
    FOR(i, 1, n) {
      // p[i].second ?????????
      int res = INF;
      for (int j : st) {
        // cout << j << " ";
        // cout << p[i].first << " " << D[j] << " " << j << " " << p[i].second
        // << /*" " << G[{j, p[i].second}] <<*/ endl;
        if (G.find({j, p[i].second}) == G.end())
          continue;
        if (p[i].first == D[j] + G[{j, p[i].second}]) {
          chmin(res, H[{j, p[i].second}]);
          // cout << "ok";
        }
        // cout << endl;
      }
      // cout << endl;
      // cout << "res:" << res << endl;
      // for(int j: st) cout << j << " "; cout << endl;
      ret += res;
      st.insert(p[i].second);
    }

    cout << ret << endl;
  }

  return 0;
}