-
Notifications
You must be signed in to change notification settings - Fork 3
/
a_cheese.cpp
250 lines (201 loc) · 7.19 KB
/
a_cheese.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <fstream>
#include <vector>
#include <limits>
#include <deque>
#include <optional>
#include <algorithm>
#include <iomanip>
static constexpr long long INF = std::numeric_limits<long long>::max();
static constexpr double EPSILON = 1e-10;
class edge {
public:
edge(size_t from, size_t to, double capacity, long long cost, size_t reverse)
: from(from), to(to), capacity(capacity), cost(cost), flow(0.0), reverse(reverse) {}
double potential() const noexcept {
return capacity - flow;
}
const size_t from, to;
const double capacity;
const long long cost;
double flow;
const size_t reverse;
};
class network {
public:
network(size_t size, size_t start, size_t finish)
: size(size), start(start), finish(finish), edges(std::make_unique<std::vector<edge>[]>(size)) {}
network(size_t size) : network(size, 0, size - 1) {}
void emplace_edge(size_t from, size_t to, double capacity, long long cost) {
edges[from].emplace_back(from, to, capacity, cost, edges[to].size());
edges[to].emplace_back(to, from, 0, -cost, edges[from].size() - 1);
}
double max_flow_min_cost() {
double flow = 0;
for (auto lev = levit(); lev; lev = levit()) {
auto parent = std::move(*lev);
double add_flow = std::numeric_limits<double>::max();
for (size_t i = finish; i != start; i = parent[i]->from) {
add_flow = std::min(add_flow, parent[i]->potential());
}
flow += add_flow;
for (size_t i = finish; i != start; i = parent[i]->from) {
edge* e = parent[i];
e->flow += add_flow;
edges[e->to][e->reverse].flow -= add_flow;
}
}
double added = 0;
for (auto& e: edges[1]) {
added -= e.flow;
}
return flow;
}
private:
size_t size, start, finish;
std::unique_ptr<std::vector<edge>[]> edges;
std::optional<std::unique_ptr<edge*[]>> levit() const {
enum vertex_type {
FAR_AWAY,
WAITING,
NEARBY
};
auto parent = std::make_unique<edge*[]>(size);
std::vector<vertex_type> vtype(size, FAR_AWAY);
std::vector<long long> distance(size, INF);
distance[0] = 0;
std::deque<size_t> deq;
deq.push_back(start);
while (!deq.empty()) {
size_t from = deq.front();
deq.pop_front();
vtype[from] = NEARBY;
for (auto& e: edges[from]) {
if (std::abs(e.potential()) >= EPSILON && distance[e.to] > distance[e.from] + e.cost) {
distance[e.to] = distance[e.from] + e.cost;
switch (vtype[e.to]) {
case FAR_AWAY:
deq.push_back(e.to);
break;
case NEARBY:
deq.push_front(e.to);
case WAITING:
break;
}
vtype[e.to] = WAITING;
parent[e.to] = &e;
}
}
}
return distance[finish] >= INF ? std::nullopt : std::make_optional(std::move(parent));
}
};
bool is_schedulable(
std::vector<int> const& times,
std::vector<int> const& releases,
std::vector<int> const& deadlines,
int total_time,
std::vector<int> const& machines,
double middle
) {
std::vector<std::pair<double, double>> boundaries;
for (size_t i = 0; i < times.size(); ++i) {
boundaries.emplace_back(releases[i], deadlines[i] + middle);
}
std::vector<double> tmp(boundaries.size() * 2);
for (size_t i = 0; i < boundaries.size(); ++i) {
tmp[i] = boundaries[i].first;
tmp[i + boundaries.size()] = boundaries[i].second;
}
std::sort(tmp.begin(), tmp.end());
std::vector<double> intervals;
intervals.push_back(tmp[0]);
for (size_t i = 1; i < tmp.size(); ++i) {
if (i > 0 && tmp[i] != tmp[i - 1]) {
intervals.push_back(tmp[i]);
}
}
network net(2 + intervals.size() - 1 + (intervals.size() - 1) * machines.size() + times.size(), 0, 1);
int machine_sum = 0;
for (size_t i = 0; i < machines.size(); ++i) {
machine_sum += machines[i];
}
for (size_t i = 2; i < 1 + intervals.size(); ++i) {
net.emplace_edge(i, 1, machine_sum * intervals[i - 1] - intervals[i - 2], 0);
}
for (size_t i = 1; i < intervals.size(); ++i) {
for (size_t j = 0; j < machines.size(); ++j) {
double capacity = (intervals[i] - intervals[i - 1]) * (j + 1);
if (j == machines.size() - 1) {
capacity *= machines[j];
} else {
capacity *= machines[j] - machines[j + 1];
}
net.emplace_edge((intervals.size() + 1) + j + (i - 1) * machines.size(), i + 1, capacity, 0);
}
}
for (size_t i = 0; i < times.size(); ++i) {
int from = (2 + intervals.size() - 1 + machines.size() * (intervals.size() - 1)) + i;
net.emplace_edge(0, from, times[i], 0);
int time_index = 0;
while (boundaries[i].first > intervals[time_index]) {
time_index++;
}
time_index++;
while (time_index < intervals.size() && boundaries[i].second >= intervals[time_index]) {
for (size_t j = 0; j < machines.size(); ++j) {
double capacity = (intervals[time_index] - intervals[time_index - 1]);
if (j == machines.size() - 1) {
capacity *= machines[j];
} else {
capacity *= machines[j] - machines[j + 1];
}
net.emplace_edge(
from,
(2 + intervals.size() - 1) + (time_index - 1) * machines.size() + j,
capacity,
0
);
}
time_index++;
}
}
return std::abs(net.max_flow_min_cost() - total_time) <= EPSILON;
}
int main() {
const std::string file_name = "cheese";
std::ifstream fin(file_name + ".in");
size_t job_size, machine_size;
fin >> job_size >> machine_size;
std::vector<int> times;
std::vector<int> releases;
std::vector<int> deadlines;
int total_time = 0;
for (size_t i = 0; i < job_size; ++i) {
int t, r, d;
fin >> t >> r >> d;
times.push_back(t);
releases.push_back(r);
deadlines.push_back(d);
total_time += t;
}
std::vector<int> machines;
for (size_t i = 0; i < machine_size; ++i) {
int s;
fin >> s;
machines.push_back(s);
}
std::sort(machines.begin(), machines.end(), std::greater<int>());
double left = 0;
double right = total_time;
for (size_t i = 0; i < 100; ++i) {
double middle = (left + right) / 2.0;
if (is_schedulable(times, releases, deadlines, total_time, machines, middle)) {
right = middle;
} else {
left = middle;
}
}
std::ofstream fout(file_name + ".out");
fout << std::setprecision(10) << std::fixed << (left + right) / 2.0 << '\n';
return 0;
}