-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathalgorithm.cpp
More file actions
86 lines (72 loc) · 1.94 KB
/
algorithm.cpp
File metadata and controls
86 lines (72 loc) · 1.94 KB
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
#include <limits>
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#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;
void testcase() {
Program lp (CGAL::SMALLER, false, 0, false, 0);
const int B = 0, C = 1, D = 2, R = 3;
long n, m, s; std::cin >> n >> m >> s;
long x_sum = 0, y_sum = 0;
std::vector<std::pair<long, long>> houses(n + m);
for (int i = 0; i < n; i++) {
long x, y; std::cin >> x >> y;
lp.set_a(B, i, -y); lp.set_a(C, i, -1); lp.set_b(i, -x);
houses[i] = {x, y};
x_sum += x;
y_sum += y;
}
for (int i = 0; i < m; i++) {
long x, y; std::cin >> x >> y;
lp.set_a(B, n + i, y); lp.set_a(C, n + i, 1); lp.set_b(n + i, x);
houses[n + i] = {x, y};
x_sum -= x;
y_sum -= y;
}
Solution sol = CGAL::solve_linear_program(lp, ET());
if (sol.is_infeasible()) {
std::cout << "Yuck!" << std::endl;
return;
}
if (s != -1) {
lp.set_a(B, n + m, y_sum);
lp.set_a(C, n + m, n - m);
lp.set_b(n + m, s + x_sum);
Solution sol = CGAL::solve_linear_program(lp, ET());
if (sol.is_infeasible()) {
std::cout << "Bankrupt!" << std::endl;
return;
}
}
int start = n + m + 1;
for (int i = 0; i < n + m; i++) {
long x = houses[i].first, y = houses[i].second;
lp.set_a(B, start + 2*i, -x);
lp.set_a(D, start + 2*i, 1);
lp.set_a(R, start + 2*i, -1);
lp.set_b(start + 2*i, y);
lp.set_a(B, start + 2*i + 1, x);
lp.set_a(D, start + 2*i + 1, -1);
lp.set_a(R, start + 2*i + 1, -1);
lp.set_b(start + 2*i + 1, -y);
}
lp.set_c(R, 1);
sol = CGAL::solve_linear_program(lp, ET());
std::cout << (long)std::ceil(CGAL::to_double(sol.objective_value())) << std::endl;
}
int main() {
std::ios_base::sync_with_stdio(false);
int t;
std::cin >> t;
for (int i = 0; i < t; ++i)
testcase();
}