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

#include <cstring>
#include <functional>
#include <iostream>
#include <queue>

using namespace std;

#define maxn 10240
#define INF 0x3f3f3f3f;

typedef struct edge {
  int to, dis, cos;
  edge() {}
  edge(int to, int dis, int cos) : to(to), dis(dis), cos(cos) {}
};
typedef pair<int, int> P;

vector<edge> G[maxn];

int d[maxn];
int v; // 定点

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> que; // 优先输出小数据；
  memset(d, 0x3f, sizeof(d));
  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) {
        d[e.to] = d[v] + e.dis;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int main() {
  int m;
  while (cin >> v >> m) {
    if (v == 0 && m == 0)
      break;
    for (int i = 0; i < v; i++) {
      G[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int u, v, d, c;
      cin >> u >> v >> d >> c;
      u--, v--;
      G[u].push_back(edge(v, d, c));
      G[v].push_back(edge(u, d, c));
    }
    dijkstra(0);
    int ans = 0;
    for (int i = 1; i < v; i++) {
      int min = INF;
      for (int j = 0; j < G[i].size(); j++) {
        if (d[G[i][j].to] + G[i][j].dis == d[i] && G[i][j].cos < min) {
          min = G[i][j].cos;
        }
      }
      ans += min;
    }
    cout << ans << endl;
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10010;
int mp[maxn][maxn], d[maxn], cos[maxn], cs[maxn][maxn], n, m;
bool vis[maxn];
void dij(int x) {
  int min, u, cnt = 0;
  d[x] = 0;
  cos[x] = 0;
  for (int i = 1; i <= n; i++) {
    min = INF;
    u = -1;
    for (int j = 1; j <= n; j++) {
      if (vis[j] == 0 && d[j] < min) {
        min = d[j];
        u = j;
      }
    }
    if (u == -1)
      return;
    vis[u] = 1;
    for (int k = 1; k <= n; k++) {
      if (vis[k] == 0 && mp[u][k] + d[u] < d[k]) {
        d[k] = mp[u][k] + d[u];
        cos[k] = cs[u][k];
      } else if (vis[k] == 0 && mp[u][k] + d[u] == d[k]) {
        if (cos[k] > cs[u][k])
          cos[k] = cs[u][k];
      }
    }
  }
}
int main() {
  while (cin >> n >> m && n + m) {

    memset(vis, 0, sizeof(vis));
    memset(d, INF, sizeof(d));
    memset(cos, INF, sizeof(cos));
    memset(mp, INF, sizeof(mp)); // 转化邻接矩阵时
    for (int i = 1; i <= n; i++)
      mp[i][i] = 0;
    int x, y, dis, c;
    for (int i = 1; i <= m; i++) {
      cin >> x >> y >> dis >> c;
      if (mp[x][y] > dis) {
        mp[y][x] = mp[x][y] = dis;
        cs[x][y] = cs[y][x] = c;
      } // 确保多条边时，取最小。
    }
    /*for(int i=1;i<=n;i++){
                    for(int j=1;j<=n;j++){
                    cout<<mp[i][j]<<' ';
            }
            cout<<endl;
    }

    for(int i=1;i<=n;i++){
                    for(int j=1;j<=n;j++){
                    cout<<cs[i][j]<<' ';
            }
            cout<<endl;
    }
    */
    dij(1);
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += cos[i];
    cout << ans << endl;
  }
  return 0;
}