-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathradiation.cpp
123 lines (100 loc) · 2.6 KB
/
radiation.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
#include <CGAL/Gmpz.h>
#include <CGAL/Gmpzf.h>
#include <CGAL/Gmpq.h>
typedef CGAL::Gmpz IT; // input type
typedef CGAL::Gmpz ET; // exact type for solver
typedef CGAL::Quadratic_program<IT> Program;
typedef CGAL::Quadratic_program_solution<ET> Solution;
using namespace std;
struct point {
int x;
int y;
int z;
};
vector<IT> coefficients(point &p, int d) {
auto x = p.x;
auto y = p.y;
auto z = p.z;
vector<IT> coeff;
IT powerX = 1;
for (int i = 0; i <= d; ++i) {
IT powerXY = powerX;
for (int j = 0; j <= d - i; ++j) {
IT powerXYZ = powerXY;
for (int k = 0; k <= d - i - j; ++k) {
coeff.push_back(powerXYZ);
powerXYZ *= z;
}
powerXY *= y;
}
powerX *= x;
}
return coeff;
}
bool isFeasible( vector<point> &A, vector<point> &B, int d) {
Program lp = Program(CGAL::SMALLER, false, 0, false, 0);
int h = A.size();
int t = B.size();
const int epsilon = 0;
for (int i = 0; i < h; ++i) {
auto coeff = coefficients(A[i], d);
lp.set_a(epsilon, i, 1);
for (int j = 0; j < coeff.size(); ++j) {
lp.set_a(j + 1, i, coeff[j]);
}
}
for (int i = 0; i < t; ++i) {
auto coeff = coefficients(B[i], d);
lp.set_a(epsilon, h + i, 1);
for (int j = 0; j < coeff.size(); ++j) {
lp.set_a(j + 1, h + i, -coeff[j]);
}
}
// Epsilon needs to be greater or equal 0
lp.set_l(epsilon, true, 0);
// maximize epsilon <=> minimize -epsilon
lp.set_c(epsilon, -1);
CGAL::Quadratic_program_options options;
options.set_pricing_strategy(CGAL::QP_BLAND);
Solution s = CGAL::solve_linear_program(lp, ET(), options);
if (s.is_infeasible()) {
return false;
}
if (s.is_unbounded()) {
return true;
}
return s.objective_value() < 0;
}
void solve() {
int h; cin >> h;
int t; cin >> t;
vector<point> A(h);
vector<point> B(t);
int x, y, z;
for (int i = 0; i < h; ++i) {
cin >> x >> y >> z;
A[i] = {x, y, z};
}
for (int i = 0; i < t; ++i) {
cin >> x >> y >> z;
B[i] = {x, y, z};
}
for (int d = 0; d <= 30; ++d) {
if (isFeasible(A, B, d)) {
cout << d << endl;
return;
}
}
cout << "Impossible!" << endl;
}
int main() {
ios_base::sync_with_stdio(false);
int t; cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
return 0;
}