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

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn = 10050;
const int maxe = 2005000;
const int INF = 1e9;
struct note {
  int to;
  int w;
  int c;
  int next;
};
note edge[maxe];
int head[maxn];
int ip;
void init() {
  memset(head, -1, sizeof(head));
  ip = 0;
}
void addedge(int u, int v, int w, int c) {
  edge[ip].to = v;
  edge[ip].c = c;
  edge[ip].w = w;
  edge[ip].next = head[u];
  head[u] = ip++;
}
int cost[maxn];
int dis[maxn];
bool vis[maxn];
queue<int> q;
void spfa(int s, int n) {
  while (!q.empty())
    q.pop();
  for (int i = 1; i <= n; i++)
    cost[i] = dis[i] = INF;
  memset(vis, false, sizeof(vis));
  dis[s] = 0;
  cost[s] = 0;
  vis[s] = true;
  q.push(s);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = false;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int to = edge[i].to;
      int val = edge[i].w;
      if (dis[to] > dis[u] + val ||
          (dis[to] == dis[u] + val &&
           cost[to] > edge[i].c)) //????????????????????¶????????±?´?????°????
      {
        dis[to] = dis[u] + val;
        cost[to] = edge[i].c;
        if (!vis[to]) {
          q.push(to);
          vis[to] = true;
        }
      }
    }
  }
}
int main() {
  // freopen("cin.txt","r",stdin);
  int n, m;
  int u, v, w, c;
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      break;
    init();
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &c, &w);
      addedge(u, v, c, w);
      addedge(v, u, c, w);
    }
    spfa(1, n);
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += cost[i];
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
#define MAXN 10005
#define MAXM 20005
struct Edge {
  int id, len, cos;
  Edge *nxt;
  Edge() {}
  Edge(int i, int l, int c) {
    id = i;
    len = l;
    cos = c;
  }
} E[MAXM], *V[MAXN], *cur;
void add_edge(int u, int v, int length, int cost) {
  *cur = Edge(v, length, cost);
  cur->nxt = V[u];
  V[u] = cur;
  cur++;
  *cur = Edge(u, length, cost);
  cur->nxt = V[v];
  V[v] = cur;
  cur++;
}
int N, M;
int dis[MAXN], cost[MAXN];
bool inque[MAXN];
queue<int> Q;
int SPFA() {
  memset(inque, 0, sizeof inque);
  for (int i = 1; i <= N; i++)
    dis[i] = cost[i] = 0x3FFFFFFF;
  while (!Q.empty())
    Q.pop();
  Q.push(1);
  dis[1] = 0;
  inque[1] = 1;
  cost[1] = 0;
  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    inque[t] = 0;
    for (Edge *p = V[t]; p; p = p->nxt) {
      int to = p->id;
      int len = p->len;
      if (dis[to] > dis[t] + len ||
          (dis[to] == dis[t] + len && cost[to] > p->cos)) {
        dis[to] = dis[t] + len;
        cost[to] = p->cos;
        if (!inque[to]) {
          Q.push(to);
          inque[to] = 1;
        }
      }
    }
  }
  int ret = 0;
  for (int i = 1; i <= N; i++)
    ret += cost[i];
  return ret;
}
int main() {
  while (1) {
    scanf("%d%d", &N, &M);
    if (!N && !M)
      break;
    memset(V, 0, sizeof(int) * (N + 1));
    cur = E;
    for (int i = 1, u, v, d, c; i <= M; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      add_edge(u, v, d, c);
    }
    printf("%d\\n", SPFA());
  }
  return 0;
}