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

#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxm = 20005;
const int maxn = 10005;
struct EdgeNode {
  int to;
  int w;
  int d;
  int next;
};
EdgeNode edges[maxn * maxn]; //?里要注意
int n, m;
int head[maxn], edge;
bool vis[maxn];
queue<int> que;
long long dis[maxn], cost[maxn];
void add(int u, int v, int d, int c) {
  edges[edge].w = c;
  edges[edge].d = d;
  edges[edge].to = v;
  edges[edge].next = head[u];
  head[u] = edge++;
}
void init() {
  memset(head, -1, sizeof(head));
  edge = 0;
}
int spfa(int n) {
  int u, sum2 = 0;
  dis[0] = 0;
  cost[0] = 0;
  for (int i = 1; i <= n; i++) {
    dis[i] = INF;
    cost[i] = INF;
  }
  memset(vis, 0, sizeof(vis));
  while (!que.empty())
    que.pop();
  que.push(1);
  // sum2++;
  vis[1] = true;
  dis[1] = 0;
  cost[1] = 0;
  while (!que.empty()) {
    u = que.front();
    que.pop();
    vis[u] = false;

    for (int i = head[u]; i != -1; i = edges[i].next) {
      int v = edges[i].to;
      int w = edges[i].w;
      int d = edges[i].d;
      if (dis[v] > dis[u] + d) // 不能在?里直接用等号，用了之后第三??例?不去
      {
        dis[v] = dis[u] + d;
        cost[v] = w;
        if (!vis[v]) {
          vis[v] = true;
          que.push(v);
        }

      } else if (dis[v] == dis[u] + d) {
        if (cost[v] > w)
          cost[v] = w;
      }
    }
  }
  for (int i = 1; i <= n; i++)
    cost[0] += cost[i];
  return cost[0];
}
int main() {
  int u0, v0, d0, c0;
  while (cin >> n >> m) {
    init();
    if (n == 0 && m == 0)
      break;
    int sum = 0;
    for (int i = 1; i <= m; i++) {
      cin >> u0 >> v0 >> d0 >> c0;
      add(u0, v0, d0, c0);
      add(v0, u0, d0, c0);
    }
    sum = spfa(n);
    cout << sum << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int maxn = 20005;
bool used[maxn];
int d[maxn];
int n, m;
int len;
struct Tree {
  int to;
  int from;
  int value;
  int next;
  int cost;

} tree[maxn];
int head[maxn];
int cost[maxn];
void add(int u, int v, int c, int d) {
  tree[len].from = u;
  tree[len].to = v;
  tree[len].value = c;
  tree[len].next = head[u];
  tree[len].cost = d;
  head[u] = len++;
}
void init() {
  memset(head, -1, sizeof(head));
  len = 0;
  memset(cost, 0, sizeof(cost));
}
int spfa() {
  queue<int> q;
  int sum = 0;
  memset(d, 0x3f, sizeof(d));
  memset(used, false, sizeof(used));
  while (!q.empty())
    q.pop();
  used[1] = true;
  q.push(1);
  d[1] = 0;
  while (!q.empty()) { // ceshi=false;
    int u = q.front();
    used[u] = false;
    // q.pop();
    for (int i = head[u]; i != -1; i = tree[i].next) {
      int s = tree[i].to;
      int w = tree[i].cost;
      if (d[s] > d[u] + tree[i].value) {
        if (!used[s]) {
          used[s] = true;
          q.push(s);
          cost[s] = w;
        }
        d[s] = d[u] + tree[i].value;
      }
      if (d[s] == d[u] + tree[i].value) {
        if (cost[s] > w)
          cost[s] = w;
      }
    }
    q.pop();
  }
  for (int i = 1; i <= n; i++)
    sum += cost[i];
  return sum;
}
int main() {
  int a, b, c, d;
  while (~scanf("%d%d", &n, &m), m + n) {
    init();
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d%d", &a, &b, &c, &d);
      add(a, b, c, d);
      add(b, a, c, d);
    }
    printf("%d\\n", spfa());
  }

  return 0;
}