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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <memory.h>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, m, n) for (i = m; i < n; i++)
#define MIN(x, y) (x > y ? y : x)
#define ab(x, y) (x > y ? (x - y) : (y - x))
#define CLR(a, b) memset(a, b, sizeof(a))
#define ROF(i, m, n) for (i = m; i >= n; i--)
#define BUG cout << "debug" << endl;
#define RT return
#define BK break
#define HEAP priority_queue
#define SP " "
#define lowf(a, b, c) lower_bound(a, b, c)
#define upf(a, b, c) upper_bound(a, b, c)
#define CP bool cmp(node A, node B)
#define maxn 100000
typedef long long LL;
typedef vector<int> VI;
const int INF = 214748347;
const int NEN = -INF - 1;
const LL LINF = 9223372036854775807;
bool vis[10005];
int dis[10005];
int fa[10005];
typedef struct edge {
  int dest;
  int power;
  int cost;
  edge(int v, int d, int c) {
    dest = v;
    power = d;
    cost = c;
  }
  edge() { ; }
} edge;
vector<vector<edge>> Map(10010);
int N, M;
queue<int> spfa;
int main() {
  while (cin >> N >> M && (M + N)) {
    while (!spfa.empty())
      spfa.pop();
    CLR(vis, 0);
    int i, a, b, temp, u, v, d, c;
    FOR(i, 0, N + 2) dis[i] = INF;
    FOR(i, 0, N + 2) Map[i].clear();
    FOR(i, 0, M) {
      scanf("%d %d %d %d", &u, &v, &d, &c);
      Map[u].push_back(edge(v, d, c));
      Map[v].push_back(edge(u, d, c));
    }
    spfa.push(1);
    vis[1] = 1;
    fa[1] = 0;
    dis[1] = 0;
    int targ, tdis, tc;
    while (!spfa.empty()) {
      temp = spfa.front();
      spfa.pop();
      vis[temp] = 0;
      FOR(i, 0, Map[temp].size()) {
        targ = Map[temp][i].dest;
        tdis = Map[temp][i].power;
        tc = Map[temp][i].cost;
        if (tdis + dis[temp] < dis[targ]) {
          if (!vis[targ]) {
            spfa.push(targ);
            vis[targ] = 1;
          }
          dis[targ] = dis[temp] + tdis;
          fa[targ] = tc;
        } else if ((tdis + dis[temp]) == dis[targ]) {
          fa[targ] = min(fa[targ], tc);
        }
      }
    }
    int ans = 0;
    FOR(i, 1, N + 1) { ans += fa[i]; }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <memory.h>
#include <memory>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define FOR(i, m, n) for (i = m; i < n; i++)
#define MIN(x, y) (x > y ? y : x)
#define ab(x, y) (x > y ? (x - y) : (y - x))
#define CLR(a, b) memset(a, b, sizeof(a))
#define ROF(i, m, n) for (i = m; i >= n; i--)
#define BUG cout << "debug" << endl;
#define RT return
#define BK break
#define HEAP priority_queue
#define SP " "
#define lowf(a, b, c) lower_bound(a, b, c)
#define upf(a, b, c) upper_bound(a, b, c)
#define CP bool cmp(node A, node B)
#define maxn 100000
typedef long long LL;
typedef vector<int> VI;
const int INF = 2147483647;
const int NEN = -INF - 1;
const LL LINF = 9223372036854775807;
typedef struct edge {
  int dest;
  int cost;
  int dis;
  edge(int de, int c, int di) {
    dest = de;
    c = cost;
    dis = di;
  }
  edge() { ; }
  friend bool operator==(edge A, edge B) { return A.dest == B.dest; }
  friend bool operator<(edge A, edge B) { return A.dest < B.dest; }
} edge;
typedef struct bie {
  int fro, too;
  int cost, dis;
  bie() { ; }
  bie(int a, int b, int c, int d) {
    fro = a;
    too = b;
    cost = c;
    dis = d;
  }
} bie;
bool cmp(bie A, bie B) { return A.cost > B.cost; }
set<edge> Map[10010];
bie all[20010];
bool vis[10005];
int N, M;
typedef struct node {
  int key, far;
  node() { far = INF; }
  node(int k, int f) {
    key = k;
    far = f;
  }
  friend bool operator<(node A, node B) { return A.far > B.far; }
} node;
int fax[10005];
int rax[10005];
priority_queue<node> DJ;
bool dj() {
  int i = 0;
  while (!DJ.empty())
    DJ.pop();
  FOR(i, 0, N + 2) fax[i] = INF;
  CLR(vis, 0);
  DJ.push(node(1, 0));
  fax[1] = 0;
  vis[1] = 1;
  int sum = 1;
  int tempk, tempf;
  set<edge>::iterator ptr;
  while (!DJ.empty()) {
    tempk = DJ.top().key;
    tempf = DJ.top().far;
    DJ.pop();
    if (!vis[tempk]) {
      vis[tempk] = 1;
      fax[tempk] = tempf;
      if (rax[tempk] < fax[tempk]) {
        return false;
      }
      sum++;
    }
    if (sum == N)
      return true;
    for (ptr = Map[tempk].begin(); ptr != Map[tempk].end(); ptr++) {
      if (vis[ptr->dest] == 0)
        DJ.push(node(ptr->dest, ptr->dis + fax[tempk]));
    }
  }
  if (sum < N) {
    RT false;
  }
  return true;
}
int main() {
  while (cin >> N >> M) {
    if (N == 0 && M == 0)
      break;
    int u, v, d, c, i, j, I = 0;
    int ans = 0;
    FOR(i, 0, N + 2) {
      fax[i] = INF;
      rax[i] = INF;
    }
    FOR(i, 0, M) {
      scanf("%d %d %d %d", &u, &v, &d, &c);
      ans += c;
      Map[u].insert(edge(v, c, d));
      Map[v].insert(edge(u, c, d));
      all[I++] = bie(u, v, c, d);
    }
    sort(all, all + I, cmp);
    int st = 0;
    dj();
    FOR(i, 0, N + 2) rax[i] = fax[i];
    bie temp;
    int a, b;
    FOR(st, 0, I) {
      temp = all[st];
      a = temp.fro;
      b = temp.too;
      Map[a].erase(Map[a].find(edge(b, temp.cost, temp.dis)));
      Map[b].erase(Map[b].find(edge(a, temp.cost, temp.dis)));
      if (dj() == true)
        ans -= temp.cost;
      else {
        Map[a].insert(edge(b, temp.cost, temp.dis));
        Map[b].insert(edge(a, temp.cost, temp.dis));
      }
    }
    cout << ans << endl;
    FOR(i, 0, N + 2) Map[i].clear();
  }
}