I wrote a slightly modified Dijkstra algorithm using a priority queue that stores pairs of numbers. When i declare it as
priority_queue < pair<int,int> > Q;
it is painfully slow- 268 ms. On the other hand, if i write it as
priority_queue < pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>> > Q;
it is as fast as it should be- 12ms.
The code for the algorithm is the following
void dijkstra(int inode){
for(int i=1;i<=n;++i)dp[inode][i]=inf;
Q.push({0,v[inode]});
dp[inode][v[inode]]=0;
while(!Q.empty()){
int node=Q.top().second;
int dist=Q.top().first;
Q.pop();
if(dp[inode][node]<dist)continue;
for(auto edge: G[node]){
if(dp[inode][edge.first]>dist+edge.second){
dp[inode][edge.first]=dist+edge.second;
Q.push({dp[inode][edge.first],edge.first});
}
}
}
}
What am i missing ? Where is that difference coming from?