1 # ifndef TPL_MAXFLOW_MINCOST_H
2 # define TPL_MAXFLOW_MINCOST_H
4 # include <Bellman_Ford.H>
5 # include <tpl_netgraph.H>
24 template <
typename Arc_Info,
typename F_Type =
double>
62 template <
class NodeT,
class ArcT>
138 Arc * a = it.get_current();
139 if (not a->is_residual)
140 total += a->flow_cost();
152 typedef typename Net::Flow_Type Distance_Type;
154 static const Distance_Type Max_Distance;
156 static const Distance_Type Zero_Distance;
158 typename Net::Flow_Type operator () (
typename Net::Arc * a)
const
160 return a->is_residual ? -((
typename Net::Arc *) a->img_arc)->cost : a->cost;
165 const typename Access_Cost<Net>::Distance_Type
170 const typename Access_Cost<Net>::Distance_Type
172 numeric_limits<typename Access_Cost::Distance_Type>::max();
201 template <
class Net,
template <
class>
class Max_Flow_Algo>
203 (Net & net,
bool leave_residual =
false)
209 Max_Flow_Algo <Net> () (net,
true);
214 if (increase_flow <Net> (net, cycle) == 0)
217 if (not leave_residual)
218 net.unmake_residual_net();
221 template <
class Net,
template <
class>
class Cost>
static
222 typename Net::Flow_Type search_max_arc_cost(Net & net)
225 typename Net::Flow_Type max_cost = Cost <Net> () (it.get_current());
226 for (it.next(); it.has_current(); it.next())
228 typename Net::Flow_Type curr_cost = Cost <Net> () (it.get_current());
229 if (curr_cost > max_cost)
230 max_cost = curr_cost;
236 template <
class Net>
static
237 typename Net::Flow_Type compute_max_possible_flow(Net & net)
239 typename Net::Node * source = net.get_source();
240 typename Net::Node * sink = net.get_sink();
242 return std::min(net.get_out_cap(source), net.get_in_cap(sink));
245 template <
class Net,
template <
class>
class Cost>
static
246 typename Net::Arc * create_dummy_arc(Net & net)
248 const typename Net::Flow_Type max_cost =
249 net.get_num_nodes() * search_max_arc_cost <Net, Cost> (net);
251 net.make_residual_net();
253 typename Net::Node * src = net.get_source();
254 typename Net::Node * tgt = net.get_sink();
255 const typename Net::Flow_Type max_flow =
256 std::min(net.get_out_cap(src), net.get_in_cap(tgt));
257 typename Net::Net::Digraph * digraph = &net;
258 typename Net::Arc * a = digraph->insert_arc(src, tgt);
259 net.get_cookie() = a;
263 typename Net::Arc * img = digraph->insert_arc(tgt, src);
265 digraph->disconnect_arc(a);
267 a->is_residual =
false;
273 img->is_residual =
true;
276 img->cost = max_cost;
284 digraph->remove_arc(a);
289 template <
class Net>
static
290 void destroy_dummy_arc(Net & net)
292 typename Net::Arc * a = (
typename Net::Arc *) net.get_cookie();
293 typename Net::Arc * img = (
typename Net::Arc *) a->img_arc;
294 typename Net::Net::Digraph * digraph = &net;
296 digraph->connect_arc(a);
297 digraph->remove_arc(img);
298 digraph->remove_arc(a);
300 net.unmake_residual_net();
333 create_dummy_arc <Net, Access_Cost> (net);
338 if (increase_flow <Net> (net, cycle) == 0)
341 ((
typename Net::Arc *) net.get_cookie())->cost = 0;
345 destroy_dummy_arc(net);
411 template <
template <
class>
class Max_Flow_Algo>
414 max_flow_min_cost_by_cycle_canceling<Net, Max_Flow_Algo>
415 (net, leave_residual);
422 # endif // TPL_MAXFLOW_MINCOST_H
void by_cycle_canceling_and_dummy_arc(Net &net) const
Definition: tpl_maxflow_mincost.H:382
Definition: tpl_maxflow_mincost.H:25
Definition: Bellman_Ford.H:1359
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const typename Arc::Arc_Type &arc_info, const Flow_Type &cap, const Flow_Type &flow)
Definition: tpl_netgraph.H:517
NodeT Node
Tipo de node.
Definition: tpl_maxflow_mincost.H:80
Definition: tpl_maxflow_mincost.H:357
Definition: tpl_netgraph.H:66
F_Type Flow_Type
Tipo que representa el flujo.
Definition: tpl_maxflow_mincost.H:30
Definition: ahFunction.H:145
Definition: tpl_maxflow_mincost.H:148
Definition: tpl_graph.H:751
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &cost)
Definition: tpl_maxflow_mincost.H:107
const Flow_Type & flow_cost(Arc *a) const
Retorna una referencia al coste de un arco:
Definition: tpl_maxflow_mincost.H:95
Definition: tpl_netgraph.H:49
ArcT Arc
Tipo de arco.
Definition: tpl_maxflow_mincost.H:77
Definition: tpl_graph.H:26
virtual Arc * insert_arc(Node *src_node, Node *tgt_node)
Usado por algorítmica interna. NO UTILIZAR.
Definition: tpl_maxflow_mincost.H:116
Definition: Bellman_Ford.H:1439
Arc::Flow_Type Flow_Type
Tipo que representa la capacidad y el flujo.
Definition: tpl_maxflow_mincost.H:83
void next()
Adelanta el iterador una posición.
Definition: filter_iterator.H:143
Net::Digraph Digraph
El tipo de grafo dirigido.
Definition: tpl_maxflow_mincost.H:72
void by_cycle_canceling(Net &net, bool leave_residual=false)
Definition: tpl_maxflow_mincost.H:412
Flow_Type cost
coste por unidad de flujo
Definition: tpl_maxflow_mincost.H:32
Node::Node_Type Node_Type
Tipo de atributo que almacena un nodo.
Definition: tpl_maxflow_mincost.H:86
Definition: tpl_netgraph.H:205
Flow_Type & get_cost(Arc *a)
Retorna una referencia modificable al coste de un arco.
Definition: tpl_maxflow_mincost.H:92
virtual Arc * insert_arc(Node *src_node, Node *tgt_node, const Arc_Type &arc_info)
Usado por algorítmica interna. NO UTILIZAR.
Definition: tpl_maxflow_mincost.H:124
Definition: tpl_maxflow_mincost.H:63
Net_Graph< NodeT, ArcT > Net
Definition: tpl_maxflow_mincost.H:69
Definition: ahFunction.H:54
Flow_Type compute_flow_cost()
Calcula el coste total del flujo circulante por la red.
Definition: tpl_maxflow_mincost.H:133
Arc::Arc_Type Arc_Type
Tipo de atributo que almacena un arco.
Definition: tpl_maxflow_mincost.H:89
Flow_Type flow_cost() const
Retorna el coste del flujo circulante.
Definition: tpl_maxflow_mincost.H:35
Flow_Type flow
valor de flujo
Definition: tpl_netgraph.H:77
void max_flow_min_cost_by_cycle_canceling(Net &net)
Definition: tpl_maxflow_mincost.H:327