int minRefuelStops(int target, int startFuel, vector<vector<int>>& stations) { if (startFuel >= target) return 0; Heap bfs_q; bfs_q.Insert(Stop{-1, startFuel}); auto n = 0; Heap q; unordered_map<int, bool> is_added; while (!bfs_q.Empty()) { auto s = bfs_q.Pop(); for(auto i = s.index+1; i < stations.size(); ++i) { if (s.fuel >= stations[i][0]) { if (is_added.find(i) != is_added.end()) { //continue break; } is_added[i] = true; auto p = Stop{i, stations[i][1] + s.fuel}; if (p.fuel >= target) return n+1; q.Insert(p); } else { break; } } if (bfs_q.Empty() && !q.Empty()) { bfs_q = q; n++; q.Clear(); is_added.clear(); } } return -1; }
/** flush (clear) the cache */ inline void Flush() { m_map.Clear(); m_heap.Clear(); }
/** flush (clear) the cache */ FORCEINLINE void Flush() { m_map.Clear(); m_heap.Clear(); }