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

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 1e9

struct edge {
  int to;
  int length;
  int cost;
  bool operator>(const edge &e) const {
    if (length != e.length)
      return length > e.length;
    return cost > e.cost;
  }
};

void dijkstra(vector<vector<edge>> &g, vector<int> &dist, vector<int> &cost,
              vector<int> &prev, int s) {
  priority_queue<edge, vector<edge>, greater<edge>> pq;
  int V = g.size();

  for (int i = 0; i < V; i++) {
    cost[i] = (i == s) ? 0 : INF;
    dist[i] = (i == s) ? 0 : INF;
  }
  pq.push((edge){s, 0, 0});

  while (!pq.empty()) {
    auto v = pq.top();
    pq.pop();
    if (dist[v.to] < v.length)
      continue;

    if (dist[v.to] != INF and v.to != 0) {
      cost[v.to] = min(cost[v.to], v.cost);
    }

    for (auto next_v : g[v.to]) {
      if (next_v.to != v.to and next_v.length + dist[v.to] <= dist[next_v.to]) {
        dist[next_v.to] = next_v.length + dist[v.to];
        pq.push((edge){next_v.to, dist[next_v.to], next_v.cost});
      }
    }
  }
}

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;

    if (N == 0 and M == 0)
      break;
    vector<vector<edge>> vertices(N);
    FOR(i, 0, M) {
      int u, v, d, c;
      cin >> u >> v >> d >> c;
      u--;
      v--;

      vertices[v].push_back((edge){u, d, c});
      vertices[u].push_back((edge){v, d, c});
    }
    vector<int> dist(N);
    vector<int> cost(N);
    vector<int> prev(N);

    dijkstra(vertices, dist, cost, prev, 0);
    cout << accumulate(begin(cost), end(cost), 0) << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 1e9

struct edge {
  int to;
  ll cost;
  ll length;
};

typedef vector<ll>
    P; // first : ???????????¢, second : ???????????? third : ????°??????????

void dijkstra(vector<vector<edge>> &g, vector<int> &dist, vector<int> &cost,
              vector<int> &prev, int s) {
  auto compare_func = [](const P &p, const P &q) -> bool {
    return p[0] == q[0] ? p[2] < q[2] : p[0] < q[0];
  };
  priority_queue<P, vector<P>, decltype(compare_func)> pq(compare_func);
  int V = g.size();

  for (int i = 0; i < V; i++) {
    cost[i] = (i == s) ? 0 : INF;
    dist[i] = (i == s) ? 0 : INF;
  }
  pq.push({0, 0, 0});

  while (pq.size()) {
    P v = pq.top();
    pq.pop();
    if (dist[v[1]] < v[0])
      continue;
    for (auto next_v : g[v[1]]) {
      if (next_v.to == v[1] or dist[next_v.to] <= next_v.length + dist[v[1]])
        continue;
      dist[next_v.to] = next_v.length + dist[v[1]];
      cost[next_v.to] = next_v.cost;
      pq.push((P){dist[next_v.to], next_v.to, next_v.cost});
    }
  }
}

int main() {
  while (true) {
    int N, M;
    cin >> N >> M;

    if (N == 0 and M == 0)
      break;
    vector<vector<edge>> vertices(N);
    FOR(i, 0, M) {
      int u, v;
      ll d, c;
      edge vertex;
      cin >> u >> v >> d >> c;
      u--;
      v--;

      vertex.to = u;
      vertex.length = d;
      vertex.cost = c;
      vertices[v].push_back(vertex);
      vertex.to = v;
      vertices[u].push_back(vertex);
    }
    vector<int> dist(N);
    vector<int> cost(N);
    vector<int> prev(N);

    dijkstra(vertices, dist, cost, prev, 0);

    cout << accumulate(cost.begin(), cost.end(), 0) << endl;
  }

  return 0;
}