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
| #include <algorithm>
#include <iostream>
#include <set>
#include <vector>
///////////////////////////////////////////////////////////////////////////////
class T_official;
///////////////////////////////////////////////////////////////////////////////
typedef T_official * T_off_tree;
typedef std::vector < T_off_tree > T_off_trees;
typedef int T_id;
typedef std::set < T_id > T_ids;
///////////////////////////////////////////////////////////////////////////////
struct T_resolution
{
//---------------------------------------------------------------------
int price_;
T_ids actors_ids_;
int full_price_ = {};
//---------------------------------------------------------------------
T_resolution
(
int price,
T_ids const & actors_ids
)
:
price_ ( price ),
actors_ids_ ( actors_ids )
{}
//---------------------------------------------------------------------
};
///////////////////////////////////////////////////////////////////////////////
typedef std::vector < T_resolution > T_resolutions;
///////////////////////////////////////////////////////////////////////////////
class T_official
{
//-------------------------------------------------------------------------
int id_;
T_resolutions resolutions_;
T_off_trees sub_trees_;
T_resolutions::iterator res_min_it_ = resolutions_.end();
int price_min_ = {};
bool price_min_is_valid_ = {};
//-------------------------------------------------------------------------
public:
//-------------------------------------------------------------------------
T_official
(
int id,
T_resolutions const & resolutions
)
:
id_ ( id ),
resolutions_ ( resolutions )
{}
//-------------------------------------------------------------------------
void print_price_min_and_id_of_actors_of_price_min()
{
std::cout << get_price_min()
<< std::endl;
print_id_and_id_of_price_min_actors();
std::cout << std::endl;
}
//-------------------------------------------------------------------------
void add_sub( T_off_tree const & off_tree )
{
sub_trees_.emplace_back( off_tree );
}
//-------------------------------------------------------------------------
private:
//-------------------------------------------------------------------------
int get_price_min()
{
set_price_min_if_not_valid();
return price_min_;
}
//-------------------------------------------------------------------------
void set_price_min_if_not_valid()
{
if (
price_min_is_valid_
)
{
return;
}
price_min_is_valid_ = true;
for( auto & res : resolutions_ )
{
set_res_full_price( res );
}
set_res_min_it();
price_min_ = res_min_it_->full_price_;
}
//-------------------------------------------------------------------------
void set_res_full_price( T_resolution & res )
{
res.full_price_ = res.price_;
for( auto actor_id : res.actors_ids_ )
{
auto const & sub_tree_it = get_sub_tree_it( actor_id );
if (
sub_tree_it != sub_trees_.end()
)
{
res.full_price_ += ( *sub_tree_it )->get_price_min();
}
}//for
}
//-------------------------------------------------------------------------
void set_res_min_it()
{
res_min_it_ = std::min_element
(
resolutions_.begin (),
resolutions_.end (),
[] (
auto const & L_res,
auto const & R_res
)
{
return L_res.full_price_
< R_res.full_price_;
}
);
}
//-------------------------------------------------------------------------
void print_id_and_id_of_price_min_actors() const
{
std::cout << id_
<< '\t';
if (
res_min_it_
== resolutions_.end()
)
{
return;
}
for( auto actor_id : res_min_it_->actors_ids_ )
{
auto const & sub_tree_it = get_sub_tree_it( actor_id );
if (
sub_tree_it != sub_trees_.end()
)
{
( *sub_tree_it )->print_id_and_id_of_price_min_actors();
}
}//for
}
//-------------------------------------------------------------------------
T_off_trees::const_iterator get_sub_tree_it( T_id id ) const
{
return std::find_if
(
sub_trees_.begin (),
sub_trees_.end (),
[=] (
auto const & sub_tree
)
{
return sub_tree->id_ == id;
}
);
}
//-------------------------------------------------------------------------
};
///////////////////////////////////////////////////////////////////////////////
int main()
{
T_off_tree off_tree_1 = new T_official
(
1,
{
{ 10, {2, 3} },
{ 20, {3, 4} }
}
);
T_off_tree off_tree_2 = new T_official
(
2,
{
{ 30, {} }
}
);
T_off_tree off_tree_3 = new T_official
(
3,
{
{ 40, {5} },
{ 50, {5, 6} }
}
);
T_off_tree off_tree_4 = new T_official
(
4,
{
{ 50, {} }
}
);
T_off_tree off_tree_5 = new T_official
(
5,
{
{ 60, {} }
}
);
T_off_tree off_tree_6 = new T_official
(
6,
{
{ 70, {} }
}
);
off_tree_3->add_sub( off_tree_5 );
off_tree_3->add_sub( off_tree_6 );
off_tree_1->add_sub( off_tree_2 );
off_tree_1->add_sub( off_tree_3 );
off_tree_1->add_sub( off_tree_4 );
off_tree_1->print_price_min_and_id_of_actors_of_price_min();
} |