alexjercan/bugnet huggingface dataset:
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 10006
#define MAX_M 20000
#define INF 1e9 + 1
int n, m, x;
struct edge {
  int to, dis, cost;
  edge();
  edge(int to, int dis, int cost) : to(to), dis(dis), cost(cost) {}
};
int sum;
int ans[MAX_N];
typedef pair<int, int> P; // first??????????????????second?????¶????????????
vector<edge> G[MAX_N];
int d[MAX_N]; //??¶?????°??????????????????
int dijkstra(int s) {
  int res = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + MAX_N, INF);
  fill(ans, ans + MAX_N, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.dis) {
        ans[e.to] = e.cost;
        d[e.to] = d[v] + e.dis;
        que.push(P(d[e.to], e.to));

      } else if (d[e.to] == d[v] + e.dis && ans[e.to] > e.cost) {
        ans[e.to] = e.cost;
      }
    }
  }
  return accumulate(ans + 2, ans + n + 1, res);
}

int main() {
  while (scanf("%d%d", &n, &m) && n != 0) {
    int u, v, d, c;
    for (int i = 0; i <= n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      G[u].push_back(edge(v, d, c));
      G[v].push_back(edge(u, d, c));
    }
    sum = dijkstra(1);
    printf("%d\\n", sum);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <vector>
using namespace std;
#define MAX_N 1005
#define MAX_M 100005
#define INF 1e9 + 1
int n, m, x;
struct edge {
  int to, dis, cost;
  edge();
  edge(int to, int dis, int cost) : to(to), dis(dis), cost(cost) {}
};
int sum;
int ans[MAX_N];
typedef pair<int, int> P; // first??????????????????second?????¶????????????
vector<edge> G[MAX_N];
int d[MAX_N]; //??¶?????°??????????????????
int dijkstra(int s) {
  int res = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + MAX_N, INF);
  fill(ans, ans + MAX_N, INF);
  d[s] = 0;
  que.push(P(0, s));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.dis) {
        ans[e.to] = e.cost;
        d[e.to] = d[v] + e.dis;
        que.push(P(d[e.to], e.to));

      } else if (d[e.to] == d[v] + e.dis && ans[e.to] > e.cost) {
        ans[e.to] = e.cost;
      }
    }
  }
  return accumulate(ans + 2, ans + n + 1, res);
}

int main() {
  while (scanf("%d%d", &n, &m) && n != 0) {
    int u, v, d, c;
    for (int i = 0; i <= n; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &d, &c);
      G[u].push_back(edge(v, d, c));
      G[v].push_back(edge(u, d, c));
    }
    sum = dijkstra(1);
    printf("%d\\n", sum);
  }
}
Problem p01366 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define N 10000
#define INF 100000000

struct Edge {
  int to, cost, dist;
  Edge(int t, int c, int d) : to(t), cost(c), dist(d){};
  Edge(){};
};

typedef vector<Edge> Graph;
typedef pair<int, int> P;

Graph g[N];
int cost[N], ucost[N];
int n;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = 0; i < n; ++i) {
    cost[i] = INF;
    ucost[i] = INF;
  }
  q.push(P(0, s));
  cost[s] = 0;
  ucost[s] = 0;
  while (!q.empty()) {
    int v = q.top().second, c = q.top().first;
    q.pop();
    if (cost[v] < c)
      continue;
    for (int i = 0; i < (signed)g[v].size(); ++i) {
      Edge e = g[v][i];
      if (cost[v] + e.cost < cost[e.to]) {
        cost[e.to] = cost[v] + e.cost;
        q.push(P(cost[e.to], e.to));
        ucost[e.to] = e.dist;
      } else if (cost[v] + e.cost == cost[e.to]) {
        if (e.dist < ucost[e.to]) {
          ucost[e.to] = e.dist;
        }
      }
    }
  }
}

int main() {
  int m, ans;
  int u, v, d, c;
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; ++i)
      g[i].clear();
    for (int i = 0; i < m; ++i) {
      cin >> u >> v >> d >> c;
      v--;
      u--;
      g[u].push_back(Edge(v, d, c));
      g[v].push_back(Edge(u, d, c));
    }
    dijkstra(0);
    ans = 0;
    for (int i = 1; i < n; ++i) {
      ans += ucost[i];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

#define N 10000
#define INF 100000000

struct Edge {
  int to, cost;
  Edge(int t, int c) : to(t), cost(c){};
  Edge(){};
};

typedef vector<Edge> Graph;
typedef pair<int, int> P;

Graph g[N];
int from[N];
int co[N][N], cost[N];
int n;

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> q;
  for (int i = 0; i < n; ++i) {
    cost[i] = INF;
    from[i] = -1;
  }
  q.push(P(0, s));
  cost[s] = 0;
  from[s] = s;
  while (!q.empty()) {
    int v = q.top().second;
    q.pop();
    for (int i = 0; i < (signed)g[v].size(); ++i) {
      Edge e = g[v][i];
      if (cost[v] + e.cost < cost[e.to]) {
        cost[e.to] = cost[v] + e.cost;
        q.push(P(cost[e.to], e.to));
        from[e.to] = v;
      } else if (cost[v] + e.cost == cost[e.to]) {
        if (co[v][e.to] < co[from[e.to]][e.to]) {
          from[e.to] = v;
        }
      }
    }
  }
}

int main() {
  int m, ans;
  int u, v, d, c;
  while (1) {
    cin >> n >> m;
    if (!n && !m)
      break;
    for (int i = 0; i < n; ++i) {
      g[i].clear();
      for (int j = 0; j < n; ++j)
        co[i][j] = 0;
    }
    for (int i = 0; i < m; ++i) {
      cin >> u >> v >> d >> c;
      v--;
      u--;
      g[u].push_back(Edge(v, d));
      g[v].push_back(Edge(u, d));
      co[v][u] = co[u][v] = c;
    }
    dijkstra(0);
    ans = 0;
    for (int i = 1; i < n; ++i) {
      ans += co[i][from[i]];
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01366 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
vector<st> E[10000];
int d[10000], c[10000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) E[i].clear();
    rep(i, m) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      u--;
      v--;
      E[u].push_back({v, d, c});
      E[v].push_back({u, d, c});
    }
    memset(d, 0x3f, sizeof(d));
    priority_queue<P> que;
    d[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      for (st &v : E[p.second]) {
        if (d[v.a] > p.first + v.b) {
          d[v.a] = p.first + v.b;
          c[v.a] = v.c;
          que.push(P(d[v.a], v.a));
        } else if (d[v.a] == p.first + v.b && c[v.a] > v.c)
          c[v.a] = v.c;
      }
    }
    printf("%d\\n", accumulate(c, c + n, 0));
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
list<st> E[10000];
int d[10000], c[10000];
int main() {
  int n, m;
  while (scanf("%d%d", &n, &m), n) {
    rep(i, n) E[i].clear();
    rep(i, m) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      u--;
      v--;
      E[u].push_back({v, d, c});
      E[v].push_back({u, d, c});
    }
    memset(d, 0x3f, sizeof(d));
    priority_queue<P> que;
    d[0] = 0;
    que.push(P(0, 0));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      for (st &v : E[p.second]) {
        if (d[v.a] > p.first + v.b) {
          d[v.a] = p.first + v.b;
          c[v.a] = v.c;
          que.push(P(d[v.a], v.a));
        } else if (d[v.a] == p.first + v.b && c[v.a] > v.c)
          c[v.a] = v.c;
      }
    }
    printf("%d\\n", accumulate(c, c + n, 0));
  }
}
Problem p01366 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
const int maxn = 1e4 + 10;
const int INF = 0x3f3f3f3f;
typedef pair<int, int> P;
struct edge {
  int to, dis, cost;
};

int d[maxn];
int value[maxn];

int N, M;

int dijkstra(vector<edge> G[]) {

  memset(d, INF, sizeof(d));
  // memset(value,INF,sizeof(value));
  priority_queue<P, vector<P>, greater<P>> que;

  d[1] = 0;
  value[1] = 0;
  que.push(P(d[1], 1));

  int res = 0;
  while (!que.empty()) {
    P p = que.top();
    que.pop();

    int v = p.second;
    if (d[v] < p.first)
      continue;

    res += value[v];

    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] == d[v] + e.dis)
        value[e.to] = min(value[e.to], e.cost);
      if (d[e.to] > d[v] + e.dis) {
        d[e.to] = d[v] + e.dis;
        que.push(P(d[e.to], e.to));

        value[e.to] = e.cost;
      }
    }
  }
  return res;
}

int main() {

  // freopen("c:\\\\users\\\\administrator\\\\desktop\\\\in.txt","r",stdin);
  while (cin >> N >> M, N || M) {

    vector<edge> G[maxn];
    for (int i = 0; i < M; i++) {
      int u, v, d, c;
      edge e;
      cin >> u >> v >> d >> c;
      e.to = v, e.dis = d, e.cost = c;
      G[u].push_back(e);
      e.to = u;
      G[v].push_back(e);
    }

    int res = dijkstra(G);
    cout << res << endl;
  }
}

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];
int SPFA() {
  memset(dis, 0x3F, sizeof(int) * (N + 1));
  memset(inque, 0, sizeof(int) * (N + 1));
  memset(cost, 0x3F, sizeof(int) * (N + 1));
  dis[1] = 0;
  queue<int> Q;
  Q.push(1);
  inque[1] = 1;
  cost[1] = 0;
  while (!Q.empty()) {
    int t = Q.front();
    Q.pop();
    inque[1] = 0;
    for (Edge *p = V[t]; p; p = p->nxt)
      if (dis[p->id] > dis[t] + p->len ||
          (dis[p->id] == dis[t] + p->len && cost[p->id] > p->cos)) {
        dis[p->id] = dis[t] + p->len;
        cost[p->id] = p->cos;
        if (!inque[p->id]) {
          Q.push(p->id);
          inque[p->id] = 1;
        }
      }
  }
  int ret = 0;
  for (int i = 1; i <= N; i++)
    ret += cost[i];
  return ret;
}
int main() {
  while (scanf("%d%d", &N, &M) != EOF && !(!N && !M)) {
    memset(E, 0, sizeof(int) * (M + 1));
    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;
}
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct node {
  int next, d, c;
};
const int maxn = 10005;
int n, m;
int d[maxn], c[maxn];
bool vis[maxn];
void init() {
  for (int i = 0; i < maxn; i++)
    d[i] = c[i] = 1e9, vis[i] = 0;
}
int main() {
  while (~scanf("%d%d", &n, &m)) {
    if (n == 0 && m == 0)
      return 0;
    init();
    vector<node> v[maxn];
    for (int i = 0; i < m; i++) {
      int x, y, d, c;
      scanf("%d%d%d%d", &x, &y, &d, &c);
      v[x].push_back(node{y, d, c});
      v[y].push_back(node{x, d, c});
    }
    queue<int> q;
    q.push(1);
    d[1] = c[1] = 0;
    vis[1] = 1;
    while (!q.empty()) {
      int u = q.front();
      q.pop();
      vis[u] = 0;
      for (int i = 0; i < v[u].size(); i++) {
        int nx = v[u][i].next;
        if (d[nx] == d[u] + v[u][i].d && c[nx] > v[u][i].c) {
          c[nx] = v[u][i].c;
        }
        if (d[nx] > d[u] + v[u][i].d) {
          d[nx] = d[u] + v[u][i].d;
          c[nx] = v[u][i].c;
          if (vis[nx])
            continue;
          vis[nx] = 1;
          q.push(nx);
        }
      }
    }
    int ans = 0;
    for (int i = 2; i <= n; i++)
      ans += c[i];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define MN 200200
using namespace std;
typedef pair<int, int> pii;
struct Edge {
  int to, nxt, d, c;
} edge[MN];
int n, m, head[MN], cnt = 1, dis[MN], cost[MN], capital, vis[MN], ans = 0;
void AddEdge(int u, int v, int d, int c) {
  edge[cnt].to = v;
  edge[cnt].d = d;
  edge[cnt].c = c;
  edge[cnt].nxt = head[u];
  head[u] = cnt++;
}
void Dijkstra() {
  memset(vis, 0, sizeof(vis));
  memset(dis, 127, sizeof(dis));
  memset(cost, 127, sizeof(cost));
  priority_queue<pii> pq;
  vis[capital] = 1;
  dis[capital] = 0;
  cost[capital] = 0;
  pq.push(make_pair(capital, dis[capital]));
  while (!pq.empty()) {
    int now = pq.top().first;
    pq.pop();
    for (int i = head[now]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      if (dis[v] > dis[now] + edge[i].d ||
          (dis[v] == dis[now] + edge[i].d && cost[v] > edge[i].c)) {
        dis[v] = dis[now] + edge[i].d;
        cost[v] = edge[i].c;
        if (!vis[v])
          pq.push(make_pair(v, dis[v])), vis[v] = 1;
      }
    }
  }
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    if (n == 0 && m == 0)
      break;
    memset(edge, 0, sizeof(edge));
    memset(head, 0, sizeof(head));
    for (int i = 1; i <= m; i++) {
      int u, v, d, c;
      ;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      if (i == 1)
        capital = u;
      AddEdge(u, v, d, c);
      AddEdge(v, u, d, c);
    }
    Dijkstra();
    /*for(int i=1;i<=n;i++)
    {
        printf("%d\\n",dis[i]);
    }*/
    ans = 0;
    // cout<<n<<endl;
    for (int i = 1; i <= n; i++) {
      ans += cost[i];
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

constexpr int maxN = 10005;
constexpr int maxM = 400005;

int N, M;

int adj[maxN], gto[maxM], gnxt[maxM], length[maxM], cost[maxM], e;
void add_edge(int u, int v, int d, int c) {
  gto[e] = v;
  length[e] = d;
  cost[e] = c;
  gnxt[e] = adj[u];
  adj[u] = e++;
}

void print_edge(int s) {
  printf("%d:", s);
  for (int e = adj[s]; ~e; e = gnxt[e])
    printf(" %d", gto[e]);
  putchar('\\n');
}

int dis[maxN], pre[maxN];
int qu[maxN], qh, qt;
bool inq[maxN];
void bellman_ford(int s) {
  memset(dis, 0x3f, sizeof dis);
  dis[s] = 0;
  for (inq[qu[qh = qt = 0] = s] = 1; qh <= qt; inq[qu[qh++ % N]] = 0)
    for (int u = qu[qh % N], e = adj[u], v = gto[e]; ~e; v = gto[e = gnxt[e]])
      if (dis[v] > dis[u] + length[e]) {
        dis[v] = dis[u] + length[e];
        pre[v] = cost[e];
        if (!inq[v])
          inq[qu[++qt % N] = v] = 1;
      } else if (dis[v] == dis[u] + length[e]) {
        pre[v] = min(pre[v], cost[e]);
      }
}

int main() {
  while (~scanf("%d%d", &N, &M) && (N || M)) {
    memset(adj, -1, sizeof adj);
    for (int i = 0; i != M; ++i) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      add_edge(u, v, d, c);
      add_edge(v, u, d, c);
    }
    bellman_ford(1);
    long long ans = 0;
    for (int i = 1; i <= N; ++i)
      ans += pre[i];
    printf("%lld\\n", ans);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;

constexpr int maxN = 10005;
constexpr int maxM = 40005;

int N, M;

int adj[maxN], gto[maxM], gnxt[maxM], length[maxM], cost[maxM], e;
void add_edge(int u, int v, int d, int c) {
  gto[e] = v;
  length[e] = d;
  cost[e] = c;
  gnxt[e] = adj[u];
  adj[u] = e++;
}

void print_edge(int s) {
  printf("%d:", s);
  for (int e = adj[s]; ~e; e = gnxt[e])
    printf(" %d", gto[e]);
  putchar('\\n');
}

int dis[maxN], pre[maxN];
int qu[maxN], qh, qt;
bool inq[maxN];
void bellman_ford(int s) {
  memset(dis, 0x3f, sizeof dis);
  dis[s] = 0;
  for (inq[qu[qh = qt = 0] = s] = 1; qh <= qt; inq[qu[qh++ % N]] = 0)
    for (int u = qu[qh % N], e = adj[u], v = gto[e]; ~e; v = gto[e = gnxt[e]])
      if (dis[v] > dis[u] + length[e]) {
        dis[v] = dis[u] + length[e];
        pre[v] = cost[e];
        if (!inq[v])
          inq[qu[++qt % N] = v] = 1;
      } else if (dis[v] == dis[u] + length[e]) {
        pre[v] = min(pre[v], cost[e]);
      }
}

int main() {
  while (~scanf("%d%d", &N, &M) && (N || M)) {
    memset(adj, -1, sizeof adj);
    for (int i = 0; i != M; ++i) {
      int u, v, d, c;
      scanf("%d%d%d%d", &u, &v, &d, &c);
      add_edge(u, v, d, c);
      add_edge(v, u, d, c);
    }
    bellman_ford(1);
    long long ans = 0;
    for (int i = 1; i <= N; ++i)
      ans += pre[i];
    printf("%lld\\n", ans);
  }
  return 0;
}
Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 10050
#define INF 1e9
struct note {
  int to, next, c, w;
};
int pi, head[maxn], dis[maxn], cost[maxn];
note edge[2005000];
bool vis[maxn];
void add(int u, int v, int w, int c) {
  edge[pi].to = v;
  edge[pi].c = c;
  edge[pi].w = w;
  edge[pi].next = head[u];
  head[u] = pi++;
}
queue<int> q;
int spfa(int s, int n) {
  memset(vis, false, sizeof(vis));
  while (!q.empty())
    q.pop();
  for (int i = 1; i <= n; i++)
    cost[i] = dis[i] = INF;
  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() {
  int n, m;
  while (scanf("%d%d", &n, &m) && n + m) {
    int a = 0, u, v, c, w;
    memset(head, -1, sizeof(head));
    pi = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d%d%d%d", &u, &v, &c, &w);
      add(u, v, c, w);
      add(v, u, c, w);
    }
    spfa(1, n);
    for (int i = 1; i <= n; i++)
      a += cost[i];
    printf("%d\\n", a);
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <queue>
#include <stdio.h>
#include <string.h>
using namespace std;
#define maxn 10050
#define INF 1e9
struct node {
  int to, next, c, w;
};
int pi, head[maxn], dis[maxn], cost[maxn];
node edge[maxn];
bool vis[maxn];
void add(int u, int v, int w, int c) {
  edge[pi].to = v;
  edge[pi].c = c;
  edge[pi].w = w;
  edge[pi].next = head[u];
  head[u] = pi++;
}
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() {
  int n, m;
  while (scanf("%d%d", &n, &m) && n + m) {
    int a = 0, u, v, c, w;
    memset(head, -1, sizeof(head));
    pi = 0;
    for (int i = 1; i <= m; i++) {
      scanf("%d%d%d%d", &u, &v, &c, &w);
      add(u, v, c, w);
      add(v, u, c, w);
    }
    spfa(1, n);
    for (int i = 0; i <= n; i++)
      a += cost[i];
    printf("%d\\n", a);
  }
  return 0;
}
Problem p01366 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f

using namespace std;

int m, n, temp1, temp2, ans, used[10005], mincost[10005], mindis[10005], mark;

struct road {
  int from, to, l, w;
} r[20005];

void Dijkstra() {
  memset(used, 0, sizeof(used));
  memset(mincost, inf, sizeof(mincost));
  memset(mindis, inf, sizeof(mindis));
  for (int i = 1; i <= n; i++) {
    if (r[i].from == 1) {
      mincost[r[i].to] = r[i].w;
      mindis[r[i].to] = r[i].l;
    }
    if (r[i].to == 1) {
      mincost[r[i].from] = r[i].w;
      mindis[r[i].from] = r[i].l;
    }
  }
  used[1] = 1;
  for (int i = 2; i <= m; i++) {
    mark = -1;
    for (int i = 1; i <= m; i++)
      if (!used[i] && (mark == -1 || mindis[i] < mindis[mark]))
        mark = i;
    used[mark] = 1;
    ans += mincost[mark];
    for (int i = 1; i <= n; i++) {
      if (r[i].from == mark) {
        if (!used[r[i].to]) {
          if (mindis[r[i].to] > mindis[r[i].from] + r[i].l) {
            mindis[r[i].to] = mindis[r[i].from] + r[i].l;
            mincost[r[i].to] = r[i].w;
          }
          if (mindis[r[i].to] == mindis[r[i].from] + r[i].l)
            mincost[r[i].to] = min(mincost[r[i].to], r[i].w);
        }
      }
      if (r[i].to == mark) {
        if (!used[r[i].from]) {
          if (mindis[r[i].from] > mindis[r[i].to] + r[i].l) {
            mindis[r[i].from] = mindis[r[i].to] + r[i].l;
            mincost[r[i].from] = r[i].w;
          }
          if (mindis[r[i].from] == mindis[r[i].to] + r[i].l)
            mincost[r[i].from] = min(mincost[r[i].from], r[i].w);
        }
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  while (cin >> m >> n && m) {
    ans = 0;
    for (int i = 1; i <= n; i++)
      cin >> r[i].from >> r[i].to >> r[i].l >> r[i].w;
    Dijkstra();
    cout << ans << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstring>
#include <iostream>
#define inf 0x3f3f3f3f

using namespace std;

int m, n, w[10005][10005], l[10005][10005], temp1, temp2, ans, used[10005],
    mincost[10005], mindis[10005], mark;

void Dijkstra() {
  memset(used, 0, sizeof(used));
  for (int i = 1; i <= m; i++) {
    mincost[i] = w[1][i];
    mindis[i] = l[1][i];
  }
  used[1] = 1;
  for (int i = 2; i <= m; i++) {
    mark = -1;
    for (int i = 1; i <= m; i++)
      if (!used[i] &&
          (mark == -1 || mindis[i] < mindis[mark] ||
           (mindis[i] == mindis[mark] && mincost[i] < mindis[mark])))
        mark = i;
    used[mark] = 1;
    ans += mincost[mark];
    for (int i = 1; i <= m; i++) {
      if (!used[i] && mindis[i] >= mindis[mark] + l[mark][i]) {
        mindis[i] = mindis[mark] + l[mark][i];
        mincost[i] = min(mincost[i], w[mark][i]);
      }
    }
  }
}

int main() {
  while (cin >> m >> n && m) {
    ans = 0;
    memset(l, inf, sizeof(l));
    memset(w, inf, sizeof(w));
    for (int i = 1; i <= n; i++) {
      cin >> temp1 >> temp2;
      cin >> l[temp1][temp2] >> w[temp1][temp2];
      l[temp2][temp1] = l[temp1][temp2];
      w[temp2][temp1] = w[temp1][temp2];
    }
    Dijkstra();
    cout << ans << endl;
  }
  return 0;
}