'Memory problem of long long vector vs int vector in C++
I was doing a homework on data structure which is to use the dijkstra algorithm to find the path.
What puzzled me is that when I use int vectors for storing the adjacency list and all other vectors, I used up all the memory allowed for the test (65565kb), but when I used long long vectors, the memory used is a lot smaller at around 10000kb max.
I know that vectors auto resize themselves so I tried increasing the initial size of them, but I still encountered that memory problem unless I changed it to a long long vector.
Can anyone explain this weird behavior to me? Shouldn't long long take up more space then int?
Also, why should I add 5 to the size of vector when trying to initialize it globally? In this case it is 100000+5.
Thank you very much.
#include "iostream"
#include "vector"
#include "queue"
#include "algorithm"
using namespace std;
vector<vector<pair<long long, long long>>> adjlist(100005);
vector<long long> cost(100005);
vector<long long> previous(100005);
void dijkstra(long long src, long long n){
cost.clear();
cost.resize(100005, LONG_LONG_MAX);
cost[src] = 0;
previous[src] = src;
priority_queue<pair<long long, long long>> squeue;
squeue.push({-cost[src], src});
while(!squeue.empty()){
long curnode = squeue.top().second;
squeue.pop();
for(auto x:adjlist[curnode]){
if(cost[curnode] + x.first < cost[x.second]){
cost[x.second] = cost[curnode] + x.first;
previous[x.second] = curnode;
squeue.push({-cost[x.second], x.second});
}
}
}
}
int main(){
long long n,m;
cin >> n;
cin >> m;
for(long long i=0;i<m;i++){
long long src;
long long dest;
long long weight;
cin >> src;
cin >> dest;
cin >> weight;
adjlist[src].push_back({weight, dest});
adjlist[dest].push_back({weight, src});
}
dijkstra(1, n);
if(previous[n] == 0){
cout << "-1";
adjlist.clear();
previous.clear();
}else{
vector<long long> answer;
long long curnode = n;
while(curnode != 1){
answer.push_back(curnode);
curnode = previous[curnode];
}
answer.push_back(1);
reverse(answer.begin(), answer.end());
for(long long x:answer){
cout << x << " ";
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
