@@ -46,14 +46,14 @@ class Pgr_dijkstraTRSP {
4646 private:
4747 void executeDijkstraTRSP (G& graph);
4848 void getFirstSolution (G& graph);
49- void checkFirstSolution (G& graph );
49+ bool checkFirstSolution ();
5050 private:
5151 typedef typename G::V V;
5252 V v_source;
5353 V v_target;
5454 int64_t m_start;
5555 int64_t m_end;
56- std::vector<Restrict_t> m_restrictions;
56+ std::vector< std::pair< int64_t , Restrict_t> > m_restrictions;
5757 bool m_only_cost;
5858 bool m_strict;
5959
@@ -82,7 +82,7 @@ int64_t start_vertex, int64_t end_vertex, bool only_cost, bool strict) {
8282 m_start = start_vertex;
8383 m_end = end_vertex;
8484 for (auto &restriction: restrictions)
85- m_restrictions.push_back (restriction);
85+ m_restrictions.push_back ( { restriction. restricted_edges [ 0 ], restriction} );
8686 m_strict = strict;
8787 executeDijkstraTRSP (graph);
8888 return curr_result_path;
@@ -100,16 +100,50 @@ void Pgr_dijkstraTRSP< G >::getFirstSolution(G& graph) {
100100}
101101
102102template < class G >
103- void Pgr_dijkstraTRSP< G >::checkFirstSolution(G& graph) {
104- auto pathlength = curr_result_path.size ();
105- log << curr_result_path;
103+ bool Pgr_dijkstraTRSP< G >::checkFirstSolution() {
104+ auto sort_cmp = [](const std::pair<int64_t , Restrict_t>& left,
105+ const std::pair<int64_t , Restrict_t>& right) -> bool {
106+ return left.first <= right.first ;
107+ };
108+ auto lower_bound_cmp = [](const std::pair<int64_t , Restrict_t>& p,
109+ int64_t target) -> bool {
110+ return p.first < target;
111+ };
112+ std::stable_sort (m_restrictions.begin (), m_restrictions.end (),
113+ sort_cmp);
114+ std::vector< int64_t > edges_in_path;
115+ for (auto &path: curr_result_path) edges_in_path.push_back (path.edge );
116+ while (edges_in_path.size () and edges_in_path.back () == -1 ) edges_in_path.pop_back ();
117+ size_t index = 0 ;
118+ for (auto &edge: edges_in_path) {
119+ auto edge_index = std::lower_bound (m_restrictions.begin (),
120+ m_restrictions.end (), edge, lower_bound_cmp) - m_restrictions.begin ();
121+ while (edge_index < m_restrictions.size () and
122+ m_restrictions[edge_index].first == edge) {
123+ size_t temp_edge_index = index;
124+ bool okay = true ;
125+ for (auto &edge_id: m_restrictions[edge_index].second .restricted_edges ) {
126+ if (edge_id == -1 ) break ;
127+ if (edges_in_path[temp_edge_index] != edge_id) okay = false ;
128+ temp_edge_index++;
129+ }
130+ if (okay)
131+ return false ;
132+ edge_index++;
133+ }
134+ index++;
135+ }
136+ return true ;
106137}
107138
108139template < class G >
109140void Pgr_dijkstraTRSP< G >::executeDijkstraTRSP(G& graph) {
110141 clear ();
111142 getFirstSolution (graph);
112- checkFirstSolution (graph);
143+ bool sol = checkFirstSolution ();
144+ std::cout<<sol<<" \n " ;
145+ if (sol)
146+ curr_result_path = Path ();
113147}
114148
115149#endif // INCLUDE_DIJKSTRATRSP_PGR_DIJKSTRATRSP_HPP_
0 commit comments