最小成本最大流量,带增压::successive_shortest_path_nonnegative_weights

Min-cost-max-flow with boost::successive_shortest_path_nonnegative_weights

本文关键字:successive shortest weights nonnegative path 流量      更新时间:2023-10-16

我需要使用

boost::successive_shortest_path_nonnegative_weights()

在 BGL (v 1_60_0) 中可用的函数。如文档中所述,

表示网络的有向图 G=(V,E) 必须增强以包含 E 中每条边的反向边。也就是说,输入图应该是 Gin = (V,{E U ET})。[...]容量边缘映射参数上限必须将 E 中的每个边映射到正数,并将 ET 中的每个边映射到 0。WeightMap 必须映射从 E 到非负数的每个边,以及从 ET 到其反转边的 -weight

我有一个简单的函数,对于添加到图形中的每个边缘,都会添加一个具有上述容量和重量的反向边缘:

void add_edge_and_reverse(vertex_desc& source, vertex_desc& target, Edge& edge, flow_network_t& fn, boost::associative_property_map<std::map<edge_desc, edge_desc>>& rev_map)
{
    std::pair<edge_desc, bool> e = boost::add_edge(source, target, edge, fn);
    Edge reverse_edge(-edge.cost, 0);
    std::pair<edge_desc, bool> e_rev = boost::add_edge(target, source, reverse_edge, fn);
    rev_map[e.first] = e_rev.first;
    rev_map[e_rev.first] = e.first;
}

现在,图形包含反向边缘,并且它们具有负权重,这与我正在使用的算法名称明显形成鲜明对比。结果,当我执行算法时,我收到此错误:

ValueError: The graph may not contain an edge with negative weight.

我做错了什么?

刚刚遇到了同样的问题。调试几分钟后,我发现了问题。使用浮子类型作为砝码。因此,修改后的边权重(负权重的 dijkstra 版本)可能会因数值误差而略低于 0。可能的解决方案可能是重写"successive_shortest_path_nonnegative_weights.hpp",以便它四舍五入小的负值