forked from daniCh8/eth-algolab-2019
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAstérix_and_the_Roman_Lines.cpp
60 lines (51 loc) · 1.96 KB
/
Astérix_and_the_Roman_Lines.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* This problem can be solved using a LP built the following way:
* The variables are just the time that Asterix and Panoramix will have, and the (x, y) coordinates of the point P they'll choose.
* The constraints are all such that the variable time must be <= than the distance(P, legion)/legion_speed.
* That distance has a sign: it's used to save the position of Asterix and Panoramix with respect to the legion (they can't trepass the legions to go to their point).
* The objective function will be the time, negated (we want to maximize it).
**/
#include <iostream>
#include <vector>
#include <algorithm>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
#include <CGAL/Gmpz.h>
typedef long IT;
typedef CGAL::Gmpz ET;
typedef CGAL::Quadratic_program<IT> Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
using namespace std;
IT sign(IT a, IT b, IT c, IT x, IT y) {
if(a*x+b*y+c >= 0) return 1;
return -1;
}
int main(int argc, char const *argv[]) {
ios_base::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while(t--) {
IT x_ao, y_ao, n;
cin >> x_ao >> y_ao >> n;
Program lp (CGAL::SMALLER, false, 0, false, 0);
const int time = 0, x_p = 1, y_p = 2;
for(int i = 0; i < n; i++) {
IT a, b, c, v;
cin >> a >> b >> c >> v;
IT a_l = pow(a, 2), b_l = pow(b, 2), deno = sqrt(a_l + b_l), l_sign = sign(a, b, c, x_ao, y_ao);
lp.set_a(time, i, v*deno);
lp.set_a(x_p, i, -a*l_sign);
lp.set_a(y_p, i, -b*l_sign);
lp.set_b(i, c*l_sign);
}
lp.set_l(time, true, 0);
lp.set_c(time, -1);
lp.set_c(x_p, 0);
lp.set_c(y_p, 0);
lp.set_c0(0);
Solution s = CGAL::solve_linear_program(lp, ET());
ET result = s.objective_value_numerator()/s.objective_value_denominator();
cout << fixed << setprecision(0) << floor(CGAL::to_double(-1*result)) << "\n";
}
}