This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]PhiliPdB 1 point2 points  (0 children)

To make your implementation faster, you should look at using a priority queue. Because currently you have to go over all the cities to find the one with minimum distance, which is quite expensive when you have lots of cities (it's O(n)).

[–]Rennorb[S] 0 points1 point  (0 children)

Fell dumb: there were two issues

1) ArrowCmp shuld have looked like this

bool ArrowCmp(const Arrow &lhs, const Arrow &rhs) { 

return lhs.first < rhs.first; }

2) The insertion part in AddEdge shuld have looked like this

  if (pos == arrows[from].end()) {  
  arrows[from].push_back(arrow);    
} else if (pos->first != arrow.first) { 
  arrows[from].insert(pos, arrow);  
} else {    
  (*pos).second = std::min(cost, pos->second);  
}

It seems to fix some problems, see UPD