Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.21 KB | None | 0 0
  1.    
  2. #include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
  3. #include <CGAL/Delaunay_triangulation_2.h>
  4. #include <CGAL/Triangulation_vertex_base_with_info_2.h>
  5. #include <CGAL/Triangulation_face_base_2.h>
  6. #include <boost/graph/adjacency_list.hpp>
  7. #include <boost/graph/prim_minimum_spanning_tree.hpp>
  8.  
  9. typedef boost::adjacency_list<boost::vecS, boost::vecS, boost::undirectedS,
  10.                               boost::property<boost::vertex_distance_t, long>,
  11.                               boost::property<boost::edge_weight_t, long>>
  12.     w_graph;
  13.  
  14. typedef boost::property_map<w_graph, boost::edge_weight_t>::type w_map;
  15. typedef boost::property_map<w_graph, boost::vertex_distance_t>::type d_map;
  16. typedef boost::property_map<w_graph, boost::vertex_index_t>::type i_map;
  17. typedef boost::graph_traits<w_graph>::edge_descriptor edge_desc;
  18. typedef boost::graph_traits<w_graph>::vertex_descriptor vert_desc;
  19.  
  20. typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
  21. typedef CGAL::Triangulation_vertex_base_with_info_2<int, K> Vb;
  22. typedef CGAL::Triangulation_face_base_2<K> Fb;
  23. typedef CGAL::Triangulation_data_structure_2<Vb, Fb> Tds;
  24. typedef CGAL::Delaunay_triangulation_2<K, Tds> Triangulation;
  25. typedef Triangulation::Edge_iterator Edge_iterator;
  26. typedef Triangulation::Vertex_handle Vh;
  27. typedef K::Segment_2 Edge;
  28. typedef K::Point_2 Point;
  29.  
  30. void testcase(){
  31.     int n, m;
  32.     int x, y;
  33.     long p;
  34.  
  35.     cin >> n >> m >> p;
  36.     std::vector<pair<Point, int>> jammers(n);
  37.     std::vector<K::Point_2> missions_start(m);
  38.     std::vector<K::Point_2> missions_end(m);
  39.  
  40.     for (int i = 0; i < n; ++i)
  41.     {
  42.         std::cin >> x >> y;
  43.         jammers[i] = make_pair(Point(x, y), i);
  44.     }
  45.  
  46.     for (int i = 0; i < m; ++i)
  47.     {
  48.         cin >> x >> y;
  49.         missions_start[i] = Point(x, y);
  50.         cin >> x >> y;
  51.         missions_end[i] = Point(x, y);
  52.     }
  53.  
  54.     // construct triangulation
  55.     Triangulation t;
  56.     t.insert(jammers.begin(), jammers.end());
  57.  
  58.     for (Edge_iterator e = t.finite_edges_begin(); e != t.finite_edges_end(); ++e)
  59.     {
  60.         //Grab the 2 vertex handles for the end points
  61.         Vh vi = e->first->vertex((e->second + 1) % 3);
  62.         Vh vj = e->first->vertex((e->second + 2) % 3);
  63.    
  64.         long cur_dist = CGAL::squared_distance(vi->point(), vj->point());
  65.         boost::add_edge(vi->info(), vj->info(), cur_dist, G);
  66.  
  67.         //create lookup for triangulation points and their index in bgl  
  68.         vertex_to_graph_index[vi->point()] = vi->info();
  69.         vertex_to_graph_index[vj->point()] = vj->info();
  70.         //and the other way around
  71.         graph_index_to_vertex[vi->info()] = vi->point();
  72.         graph_index_to_vertex[vj->info()] = vj->point();
  73.     }
  74.     //At the end I should have n vertices in the graph
  75.     long longest_dist_in_dijkstra = 0;
  76.     long longest_dist_uncovered = 0;
  77.     for (int i = 0; i < m; ++i)
  78.     {
  79.  
  80.         Point nearest = t.nearest_vertex(missions_start[i])->point();
  81.         long cur_dist = CGAL::squared_distance(missions_start[i], nearest);
  82.         boost::add_edge(n, vertex_to_graph_index[nearest], cur_dist,
  83.         max_mission_dist[i] = max(max_mission_dist[i], cur_dist);
  84.         //update lookup for mission start
  85.         graph_index_to_vertex[n] = missions_start[i];
  86.         vertex_to_graph_index[missions_start[i]] = n;
  87.  
  88.         nearest = t.nearest_vertex(missions_end[i])->point();
  89.         cur_dist = CGAL::squared_distance(missions_end[i], nearest);
  90.         boost::add_edge(vertex_to_graph_index[nearest], n + 1, cur_dist, G);
  91.         max_mission_dist[i] = max(max_mission_dist[i], cur_dist);
  92.         //update lookup for mission end
  93.         graph_index_to_vertex[n + 1] = missions_end[i];
  94.         vertex_to_graph_index[missions_end[i]] = n + 1;
  95.  
  96.  
  97.         d_map distance = boost::get(boost::vertex_distance, G);
  98.         std::vector<vert_desc> pred_map(n + 2);
  99.  
  100.         boost::dijkstra_shortest_paths(G, n,
  101.                                        boost::distance_map(distance)
  102.                                            .predecessor_map(boost::make_iterator_property_map(
  103.                                                pred_map.begin(), boost::get(boost::vertex_index, G))));
  104.  
  105.         boost::graph_traits<w_graph>::vertex_iterator v, vend;
  106.  
  107.         // cout << "finished dijkstra" << endl;
  108.         longest_dist_uncovered = max(longest_dist_uncovered, max_mission_dist[i]);
  109.        
  110.         //I have n vertices before adding mission start and end. Mission start will have index n, mission end will  
  111.         //have index n+1
  112.         int cur = n + 1;
  113.         while (n != cur)
  114.         {
  115.             // cout << cur << " ";
  116.             Point end = graph_index_to_vertex[cur];
  117.             cur = pred_map[cur];
  118.             Point start = graph_index_to_vertex[cur];
  119.             long dist = CGAL::squared_distance(end, start);
  120.             if (cur != n && cur != n + 1)
  121.                 longest_dist_in_dijkstra = max(longest_dist_in_dijkstra, dist);
  122.         }
  123.         //compute a and b coefficients, check if mission start and end are reachable
  124.         //....
  125.  
  126.         //remove the mission start and end point and associated edges s.t I can reuse the same indices for the next
  127.         //mission
  128.         boost::clear_vertex(n, G);
  129.         boost::clear_vertex(n+1, G);
  130.         boost::remove_vertex(n,G);
  131.         boost::remove_vertex(n+1,G);   
  132.     }  
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement