bool bounded_max_flow(graph_matrix upper, graph_matrix lower, int beg, int end, int& max_flow) {//流网络有g_cnt个节点,下标从0到g_cnt-1,upper为上界容量,lower为下界容量 //返回有源汇容量有上下界的流网络中是否存在可行流,若存在返回最大流max_flow max_flow = 0; //n_upper和n_lower是有源汇网络对应的无源汇网络 graph_matrix n_upper(upper), n_lower(lower); //构造无源汇网络n_upper和n_lower,添加一条从end到beg的无穷大的边 construct_n(n_upper, beg, end); //s_beg为超级源点,s_end为超级汇点 int s_beg, s_end; graph_matrix residue(n_upper); residue.g_cnt += 2; s_beg = residue.g_cnt - 2; s_end = residue.g_cnt - 1; //将residue构造为无源汇网络对应的普通流网络 construct_c(n_upper, n_lower, s_beg, s_end, residue); int lower_max_flow = distance_label(residue, s_beg, s_end); int s_beg_out = beg_out(residue, s_beg, n_upper.g_cnt); //若不相等则不存在可行流,也不存在最大流 if(lower_max_flow != s_beg_out) return(false); //若存在可行流,则计算最大流 //将residue中的超级源点超级汇点和原汇点到源点的边删去 deconstruct_c(residue, s_beg, s_end, beg, end); int free_max_flow = distance_label(residue, beg, end); max_flow = lower_max_flow + free_max_flow; return(true); }
bool bounded_min_flow(graph_matrix upper, graph_matrix lower, int beg, int end, int& min_flow) {//流网络有g_cnt个节点,下标从0到g_cnt-1,upper为上界容量,lower为下界容量 //返回有源汇容量有上下界的流网络中是否存在可行流,若存在返回最小流min_flow min_flow = 0; graph_matrix n_upper(upper), n_lower(lower); int s_beg, s_end; graph_matrix residue(n_upper); residue.g_cnt += 2; s_beg = residue.g_cnt - 2; s_end = residue.g_cnt - 1; construct_c(n_upper, n_lower, s_beg, s_end, residue); //第一次计算最大流 int lower_max_flow = distance_label(residue, s_beg, s_end); int s_beg_out = beg_out(residue, s_beg, n_upper.g_cnt); //若第一次最大流已经等于超级源点所有出弧边容量之和,即已经满负荷 if(lower_max_flow == s_beg_out) return(false); //若lower_max_flow < s_beg_out,即尚未满负荷 //添加从汇点end到源点beg容量无穷大的边 construct_n(residue, beg, end); //再次计算最大流 //由于计算从汇点end到源点beg边上的流需要更改distance_label代码,不方便扩展 //所以本文略去具体的代码(读者只需要对distance_label代码稍作更改即可求出min_flow) int lower_max_flow2 = distance_label(residue, s_beg, s_end); int s_beg_out2 = beg_out(residue, s_beg, n_upper.g_cnt); if(lower_max_flow2 == s_beg_out2) return(true); else return(false); }