Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <numeric>
  5. #include <limits>
  6. #include <stdexcept>
  7.  
  8. // example: how to solve a simple explicit LP
  9. #include <CGAL/QP_models.h>
  10. #include <CGAL/QP_functions.h>
  11. #include <CGAL/Gmpq.h>
  12.  
  13. // choose input type (input coefficients must fit)
  14. typedef CGAL::Gmpq IT;
  15. // choose exact type for solver (CGAL::Gmpz or CGAL::Gmpq)
  16. typedef CGAL::Gmpq ET;
  17.  
  18. // program and solution types
  19. typedef CGAL::Quadratic_program<IT> Program;
  20. typedef CGAL::Quadratic_program_solution<ET> Solution;
  21.  
  22. using namespace std;
  23.  
  24. void testcase()
  25. {
  26.     int n, m, h, w;
  27.     cin >> n >> m >> h >> w;
  28.  
  29.     vector<pair<int, int>> variables;
  30.     vector<pair<int, int>> constants;
  31.  
  32.     for (int i = 0; i < n; i++)
  33.     {
  34.         int x, y;
  35.         cin >> x >> y;
  36.  
  37.         variables.push_back(
  38.             std::make_pair(x, y));
  39.     }
  40.  
  41.     for (int i = 0; i < m; i++)
  42.     {
  43.         int x, y;
  44.         cin >> x >> y;
  45.         constants.push_back(
  46.             std::make_pair(x, y));
  47.     }
  48.  
  49.     Program lp(CGAL::SMALLER, true, 1, false, 0);
  50.  
  51.     int peri = 2 * h + 2 * w;
  52.  
  53.     for (int i = 0; i < n; i++)
  54.     {
  55.         // objective function
  56.         lp.set_c(i, -peri);
  57.     }
  58.  
  59.     int n_1 = n - 1;
  60.  
  61.     // Number of constraints
  62.     int k = 0;
  63.  
  64.     // Variable to variable comparison
  65.     for (int i = 0; i < n_1; i++)
  66.     {
  67.         for (int j = i + 1; j < n; j++)
  68.         {
  69.             // a_1 + a_2 <= 2 (p2x - p1x) / w
  70.             lp.set_a(i, k, 1);
  71.             lp.set_a(j, k, 1);
  72.             lp.set_b(k,
  73.                      std::max(
  74.                          CGAL::Gmpq(
  75.                              std::abs(
  76.                                  2 * (variables[i].first - variables[j].first)),
  77.                              w),
  78.                          CGAL::Gmpq(
  79.                              std::abs(
  80.                                  2 * (variables[i].second - variables[j].second)),
  81.                              w)));
  82.  
  83.             k++;
  84.         }
  85.     }
  86.  
  87.     // Variable to constant comparison
  88.     for (int i = 0; i < n; i++)
  89.     {
  90.         CGAL::Gmpq big(2000000000, 1);
  91.         for (int j = 0; j < m; j++)
  92.         {
  93.             CGAL::Gmpq tmp = std::max(
  94.                 CGAL::Gmpq(
  95.                     std::abs((2) * (variables[i].first - constants[j].first)), w),
  96.                 CGAL::Gmpq(
  97.                     std::abs((2) * (variables[i].second - constants[j].second)), w));
  98.  
  99.             big = std::min(big, tmp);
  100.         }
  101.  
  102.         lp.set_a(i, k, 1);
  103.         lp.set_b(k, big - CGAL::Gmpq(1, 1));
  104.         k++;
  105.     }
  106.  
  107.     // solve the program, using ET as the exact type
  108.     Solution s = CGAL::solve_linear_program(lp, ET());
  109.     assert(s.solves_linear_program(lp));
  110.  
  111.     // cout << s << endl;
  112.     cout << -(int)ceil(CGAL::to_double(s.objective_value())) << endl;
  113. }
  114.  
  115. int main(int argc, char const *argv[])
  116. {
  117.     ios_base::sync_with_stdio(false);
  118.  
  119.     int t;
  120.     cin >> t;
  121.  
  122.     while (t--)
  123.         testcase();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement