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

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info() { to = dist = cost = road_index = 0; }

  bool operator<(const struct Info &arg) const {
    if (dist != arg.dist)
      return dist < arg.dist;
    else {
      return cost < arg.cost;
    }
  };
  Info(int arg_to, int arg_dist, int arg_cost, int arg_road_index) {
    to = arg_to;
    dist = arg_dist;
    cost = arg_cost;
    road_index = arg_road_index;
  }

  int to, dist, cost, road_index;
};

int N, M, min_dist[10001], min_cost[10001];

vector<Info> V[10001];

void func() {

  for (int i = 1; i <= N; i++) {
    V[i].clear();
    min_dist[i] = BIG_NUM;
    min_cost[i] = BIG_NUM;
  }

  int from, to, dist, cost;

  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &from, &to, &dist, &cost);
    V[from].push_back(Info(to, dist, cost, i));
    V[to].push_back(Info(from, dist, cost, i));
  }

  priority_queue<Info> Q;

  min_dist[1] = 0;
  min_cost[1] = 0;

  for (int i = 0; i < V[1].size(); i++) {
    min_dist[V[1][i].to] = V[1][i].dist;
    min_cost[V[1][i].to] = V[1][i].cost;
    Q.push(Info(V[1][i].to, V[1][i].dist, V[1][i].cost, 0));
  }

  int min_index;

  while (!Q.empty()) {
    if (Q.top().dist > min_dist[Q.top().to] ||
        Q.top().cost > min_cost[Q.top().to]) {
      Q.pop();
    } else {
      min_index = Q.top().to;
      Q.pop();

      for (int i = 0; i < V[min_index].size(); i++) {
        if (min_dist[V[min_index][i].to] >
            min_dist[min_index] + V[min_index][i].dist) {
          min_dist[V[min_index][i].to] =
              min_dist[min_index] + V[min_index][i].dist;
          min_cost[V[min_index][i].to] = V[min_index][i].cost;
          Q.push(Info(V[min_index][i].to, min_dist[V[min_index][i].to],
                      min_cost[V[min_index][i].to], 0));
        } else if (min_dist[V[min_index][i].to] ==
                       min_dist[min_index] + V[min_index][i].dist &&
                   min_cost[V[min_index][i].to] > V[min_index][i].cost) {
          min_cost[V[min_index][i].to] = V[min_index][i].cost;
          Q.push(Info(V[min_index][i].to, min_dist[V[min_index][i].to],
                      min_cost[V[min_index][i].to], 0));
        }
      }
    }
  }

  int ans = 0;

  for (int i = 2; i <= N; i++)
    ans += min_cost[i];

  printf("%d\\n", ans);
}

int main() {

  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    func();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <cmath>
#include <iostream>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string>
#include <vector>
typedef long long int ll;
typedef unsigned long long int ull;
#define BIG_NUM 2000000000
#define MOD 1000000007
#define EPS 0.000000001
using namespace std;

struct Info {
  Info() { to = dist = cost = road_index = 0; }

  bool operator<(const struct Info &arg) const {
    if (dist != arg.dist)
      return dist < arg.dist;
    else {
      return cost < arg.cost;
    }
  };
  Info(int arg_to, int arg_dist, int arg_cost, int arg_road_index) {
    to = arg_to;
    dist = arg_dist;
    cost = arg_cost;
    road_index = arg_road_index;
  }
  void set(int arg_to, int arg_dist, int arg_cost, int arg_road_index) {
    to = arg_to;
    dist = arg_dist;
    cost = arg_cost;
    road_index = arg_road_index;
  }

  int to, dist, cost, road_index;
  vector<int> History;
};

int N, M, min_dist[10001], min_cost[10001], table[20000];
bool check[20000];

vector<Info> V[10001], LOG[10001];

void func() {

  for (int i = 0; i < M; i++)
    check[i] = false;

  for (int i = 1; i <= N; i++) {
    V[i].clear();
    LOG[i].clear();
    min_dist[i] = BIG_NUM;
    min_cost[i] = BIG_NUM;
  }

  int from, to, dist, cost;

  for (int i = 0; i < M; i++) {
    scanf("%d %d %d %d", &from, &to, &dist, &cost);
    V[from].push_back(Info(to, dist, cost, i));
    V[to].push_back(Info(from, dist, cost, i));
    table[i] = cost;
  }

  priority_queue<Info> Q;

  // dist,cost????????¢??????dikstra
  min_dist[1] = 0;
  min_cost[1] = 0;

  for (
      int i = 0; i < V[1].size();
      i++) { //????????????????£?????????¶2??¬??\\??????????????????????????¨?????????????????????????°???\\???
    min_dist[V[1][i].to] = V[1][i].dist;
    min_cost[V[1][i].to] = V[1][i].cost;
    Info new_info;
    new_info.set(V[1][i].to, V[1][i].dist, V[1][i].cost, 0);
    new_info.History.push_back(V[1][i].road_index);
    LOG[V[1][i].to].push_back(new_info);
    Q.push(new_info);
  }

  int min_index;

  while (!Q.empty()) {
    if (Q.top().dist > min_dist[Q.top().to]) {
      Q.pop();
    } else {
      min_index = Q.top().to;
      Q.pop();

      for (int i = 0; i < V[min_index].size(); i++) {
        if (min_dist[V[min_index][i].to] >
            min_dist[min_index] +
                V[min_index][i]
                    .dist) { //?????¢?????´??°???????????????min_cost?????¶????????????<?????????????????£??§????????????>
          min_dist[V[min_index][i].to] =
              min_dist[min_index] + V[min_index][i].dist;
          min_cost[V[min_index][i].to] =
              V[min_index][i].cost; //??´???????????????
          Info next;
          next.set(V[min_index][i].to, min_dist[V[min_index][i].to],
                   min_cost[V[min_index][i].to], 0);
          for (int k = 0; k < V[min_index][i].History.size();
               k++) { //?????±???????????????????¶????
            next.History.push_back(V[min_index][i].History[k]);
          }
          next.History.push_back(V[min_index][i].road_index);
          LOG[V[min_index][i].to].clear();
          LOG[V[min_index][i].to].push_back(next);
          Q.push(next);
        } else if (min_dist[V[min_index][i].to] ==
                       min_dist[min_index] + V[min_index][i].dist &&
                   min_cost[V[min_index][i].to] > V[min_index][i].cost) {
          min_cost[V[min_index][i].to] = V[min_index][i].cost;
          Info next;
          next.set(V[min_index][i].to, min_dist[V[min_index][i].to],
                   min_cost[V[min_index][i].to], 0);
          for (int k = 0; k < V[min_index][i].History.size();
               k++) { //?????±???????????????????¶????
            next.History.push_back(V[min_index][i].History[k]);
          }
          next.History.push_back(V[min_index][i].road_index);
          LOG[V[min_index][i].to].clear();
          LOG[V[min_index][i].to].push_back(next);
          Q.push(next);
        }
      }
    }
  }

  //??????????????°???
  for (int i = 2; i <= N; i++) {
    for (int k = 0; k < LOG[i][0].History.size(); k++) {
      check[LOG[i][0].History[k]] = true;
    }
  }

  int ans = 0;

  for (int i = 0; i < M; i++) {
    if (check[i])
      ans += table[i];
  }

  printf("%d\\n", ans);
}

int main() {

  while (true) {
    scanf("%d %d", &N, &M);
    if (N == 0 && M == 0)
      break;

    func();
  }

  return 0;
}