Problem p01366 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <complex>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef int Weight;
const int inf = 1 << 29;

struct Edge {
  int src, dst;
  Weight weight, cost;
  Edge(int src, int dst, Weight weight, Weight cost)
      : src(src), dst(dst), weight(weight), cost(cost) {
    ;
  }
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.cost != f.cost   ? e.cost > f.cost
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

set<pair<int, int>> usedPath;

void dijkstra(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev,
              vector<int> &cost) {
  const int n = g.size();
  dist.assign(n, inf);
  cost.assign(n, inf);
  dist[s] = cost[s] = 0;
  prev.assign(n, -1);

  priority_queue<Edge> Q;
  for (Q.push(Edge(-2, s, 0, 0)); !Q.empty();) {
    Edge e = Q.top();
    Q.pop();
    // cout<<"now : "<<e.src<<" "<<e.dst<<" "<<e.weight<<" "<<e.cost<<endl;
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;

    if (e.src >= 0 && e.dst >= 0) {
      usedPath.insert(make_pair(min(e.src, e.dst), max(e.src, e.dst)));
    }

    for (Edges::const_iterator it = g[e.dst].begin(); it != g[e.dst].end();
         ++it) {
      if (dist[it->dst] > e.weight + it->weight ||
          (dist[it->dst] == e.weight + it->weight &&
           cost[it->dst] > it->cost)) {
        dist[it->dst] = e.weight + it->weight;
        cost[it->dst] = it->cost;
        // cout<<"add "<<it->src<<" "<<it->dst<<" "<<it->cost<<"
        // "<<e.weight+it->weight<<endl;
        Q.push(Edge(it->src, it->dst, e.weight + it->weight, it->cost));
      }
    }
  }
}

int main() {
  int n, m;
  while (cin >> n >> m, n | m) {
    usedPath.clear();
    int u, v, c, d;
    ll sum = 0;
    Graph g(n);
    REP(i, m) {
      cin >> u >> v >> d >> c;
      u--, v--;
      g[u].push_back(Edge(u, v, d, c));
      g[v].push_back(Edge(v, u, d, c));
    }

    vector<Weight> dist(n);
    vector<int> prev(n);
    vector<int> cost(n);
    dijkstra(g, 0, dist, prev, cost);
    sum = 0;
    for (set<pair<int, int>>::iterator it = usedPath.begin();
         it != usedPath.end(); ++it) {
      pair<int, int> now = *it;
      for (int i = 0; i < g[now.first].size(); ++i)
        if (g[now.first][i].dst == now.second) {
          // cout<<now.first<<" "<<now.second<<endl;
          sum += g[now.first][i].cost;
          break;
        }
    }
    cout << sum << endl;
  }
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  cannot create std::vector larger than max_size()


Fixed submission:

#include <algorithm>
#include <complex>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
typedef long long ll;
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
using namespace std;

typedef int Weight;
const int inf = 1 << 29;

struct Edge {
  int src, dst;
  Weight weight, cost;
  Edge(int src, int dst, Weight weight, Weight cost)
      : src(src), dst(dst), weight(weight), cost(cost) {
    ;
  }
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight
         : e.cost != f.cost   ? e.cost > f.cost
         : e.src != f.src     ? e.src < f.src
                              : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

set<pair<int, int>> usedPath;

void dijkstra(const Graph &g, int s, vector<Weight> &dist, vector<int> &prev,
              vector<int> &cost) {
  const int n = g.size();
  dist.assign(n, inf);
  cost.assign(n, inf);
  dist[s] = cost[s] = 0;
  prev.assign(n, -1);

  priority_queue<Edge> Q;
  for (Q.push(Edge(-2, s, 0, 0)); !Q.empty();) {
    Edge e = Q.top();
    Q.pop();
    // cout<<"now : "<<e.src<<" "<<e.dst<<" "<<e.weight<<" "<<e.cost<<endl;
    if (prev[e.dst] != -1)
      continue;
    prev[e.dst] = e.src;

    if (e.src >= 0 && e.dst >= 0) {
      usedPath.insert(make_pair(min(e.src, e.dst), max(e.src, e.dst)));
    }

    for (Edges::const_iterator it = g[e.dst].begin(); it != g[e.dst].end();
         ++it) {
      if (dist[it->dst] > e.weight + it->weight ||
          (dist[it->dst] == e.weight + it->weight &&
           cost[it->dst] > it->cost)) {
        dist[it->dst] = e.weight + it->weight;
        cost[it->dst] = it->cost;
        // cout<<"add "<<it->src<<" "<<it->dst<<" "<<it->cost<<"
        // "<<e.weight+it->weight<<endl;
        Q.push(Edge(it->src, it->dst, e.weight + it->weight, it->cost));
      }
    }
  }
}

int main() {
  ifstream fin("H.txt");
  int n, m;
  while (fin >> n >> m, n | m) {
    usedPath.clear();
    int u, v, c, d;
    ll sum = 0;
    Graph g(n);
    REP(i, m) {
      fin >> u >> v >> d >> c;
      u--, v--;
      g[u].push_back(Edge(u, v, d, c));
      g[v].push_back(Edge(v, u, d, c));
    }

    vector<Weight> dist(n);
    vector<int> prev(n);
    vector<int> cost(n);
    dijkstra(g, 0, dist, prev, cost);
    sum = 0;
    for (set<pair<int, int>>::iterator it = usedPath.begin();
         it != usedPath.end(); ++it) {
      pair<int, int> now = *it;
      for (int i = 0; i < g[now.first].size(); ++i)
        if (g[now.first][i].dst == now.second) {
          // cout<<now.first<<" "<<now.second<<endl;
          sum += g[now.first][i].cost;
          break;
        }
    }
    cout << sum << endl;
  }
}