-
Notifications
You must be signed in to change notification settings - Fork 9
/
diet.cpp
63 lines (51 loc) · 1.39 KB
/
diet.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
#include <iostream>
#include <CGAL/QP_models.h>
#include <CGAL/QP_functions.h>
#include <CGAL/Gmpz.h>
typedef int 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;
void solve(int n, int m) {
vector<int> a(n);
vector<int> b(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
cin >> b[i];
}
vector<int> p(m);
vector<vector<int>> c(m, vector<int>(n));
for (int j = 0; j < m; ++j) {
cin >> p[j];
for (int i = 0; i < n; ++i) {
cin >> c[j][i];
}
}
Program lp = Program(CGAL::SMALLER, true, 0, false, 0);
for (int i = 0; i < n; ++i) {
lp.set_b(i, b[i]); // xi <= b
lp.set_b(i + n, -a[i]); // a <= xi <=> -xi <= -a
for (int j = 0; j < m; ++j) {
lp.set_a(j, i, c[j][i]);
lp.set_a(j, i + n, -c[j][i]);
}
}
for (int j = 0; j < m; ++j) {
lp.set_c(j, p[j]);
}
Solution s = CGAL::solve_linear_program(lp, ET());
if (s.is_infeasible()) {
cout << "No such diet." << endl;
} else {
cout << setprecision(0) << fixed << floor(CGAL::to_double(s.objective_value())) << endl;
}
}
int main() {
int n, m;
while (cin >> n && n > 0) {
cin >> m;
solve(n, m);
}
return 0;
}