91 template <
typename captype,
typename tcaptype,
typename flowtype>
class Graph 119 Graph(
int node_num_max,
int edge_num_max,
void (*err_function)(
char *) = NULL);
131 void add_edge(node_id i, node_id j, captype cap, captype rev_cap);
138 void add_tweights(node_id i, tcaptype cap_source, tcaptype cap_sink);
217 void set_trcap(node_id i, tcaptype trcap);
395 template <
typename captype,
typename tcaptype,
typename flowtype>
401 if (node_num_max < 16) node_num_max = 16;
402 if (edge_num_max < 16) edge_num_max = 16;
403 arcs = (
arc*) malloc(2*edge_num_max*
sizeof(
arc));
407 throw std::bad_alloc();
418 template <
typename captype,
typename tcaptype,
typename flowtype>
430 template <
typename captype,
typename tcaptype,
typename flowtype>
447 template <
typename captype,
typename tcaptype,
typename flowtype>
453 node_num_max += node_num_max / 2;
455 nodes = (
node*) realloc(nodes_old, node_num_max*
sizeof(
node));
456 if (!
nodes) { exit(1); }
461 if (
nodes != nodes_old)
471 template <
typename captype,
typename tcaptype,
typename flowtype>
479 arc_num_max += arc_num_max / 2;
if (arc_num_max & 1) arc_num_max ++;
480 arcs = (
arc*) realloc(arcs_old, arc_num_max*
sizeof(
arc));
481 if (!
arcs) { exit(1); }
486 if (
arcs != arcs_old)
502 template <
typename captype,
typename tcaptype,
typename flowtype>
530 template <
typename captype,
typename tcaptype,
typename flowtype>
536 if (delta > 0) cap_source += delta;
537 else cap_sink -= delta;
538 flow += (cap_source < cap_sink) ? cap_source : cap_sink;
542 template <
typename captype,
typename tcaptype,
typename flowtype>
549 assert(rev_cap >= 0);
561 a -> next = i -> first;
563 a_rev -> next = j -> first;
568 a_rev -> r_cap = rev_cap;
571 template <
typename captype,
typename tcaptype,
typename flowtype>
577 template <
typename captype,
typename tcaptype,
typename flowtype>
583 template <
typename captype,
typename tcaptype,
typename flowtype>
591 template <
typename captype,
typename tcaptype,
typename flowtype>
598 template <
typename captype,
typename tcaptype,
typename flowtype>
605 template <
typename captype,
typename tcaptype,
typename flowtype>
612 template <
typename captype,
typename tcaptype,
typename flowtype>
620 template <
typename captype,
typename tcaptype,
typename flowtype>
633 template <
typename captype,
typename tcaptype,
typename flowtype>
651 #define TERMINAL ( (arc *) 1 ) 652 #define ORPHAN ( (arc *) 2 ) 655 #define INFINITE_D ((int)(((unsigned)-1)/2)) 673 template <
typename captype,
typename tcaptype,
typename flowtype>
691 template <
typename captype,
typename tcaptype,
typename flowtype>
719 template <
typename captype,
typename tcaptype,
typename flowtype>
730 template <
typename captype,
typename tcaptype,
typename flowtype>
745 template <
typename captype,
typename tcaptype,
typename flowtype>
758 template <
typename captype,
typename tcaptype,
typename flowtype>
773 i -> is_in_changed_list = 0;
798 template <
typename captype,
typename tcaptype,
typename flowtype>
816 if (queue == i) queue = NULL;
883 template <
typename captype,
typename tcaptype,
typename flowtype>
893 bottleneck = middle_arc -> r_cap;
898 if (bottleneck > a->
sister->
r_cap) bottleneck = a -> sister -> r_cap;
900 if (bottleneck > i->
tr_cap) bottleneck = i -> tr_cap;
902 for (i=middle_arc->
head; ; i=a->
head)
906 if (bottleneck > a->
r_cap) bottleneck = a -> r_cap;
908 if (bottleneck > - i->
tr_cap) bottleneck = - i -> tr_cap;
913 middle_arc -> sister -> r_cap += bottleneck;
914 middle_arc -> r_cap -= bottleneck;
919 a -> r_cap += bottleneck;
920 a -> sister -> r_cap -= bottleneck;
926 i -> tr_cap -= bottleneck;
932 for (i=middle_arc->
head; ; i=a->
head)
936 a -> sister -> r_cap += bottleneck;
937 a -> r_cap -= bottleneck;
943 i -> tr_cap += bottleneck;
955 template <
typename captype,
typename tcaptype,
typename flowtype>
959 arc *a0, *a0_min = NULL, *a;
1006 if ((i->
parent = a0_min))
1009 i -> DIST = d_min + 1;
1032 template <
typename captype,
typename tcaptype,
typename flowtype>
1036 arc *a0, *a0_min = NULL, *a;
1083 if ((i->
parent = a0_min))
1086 i -> DIST = d_min + 1;
1111 template <
typename captype,
typename tcaptype,
typename flowtype>
1114 node *i, *j, *current_node = NULL;
1125 if (changed_list && !reuse_trees) { exit(1); }
1135 if ((i=current_node))
1138 if (!i->
parent) i = NULL;
1156 j -> parent = a -> sister;
1158 j -> DIST = i -> DIST + 1;
1163 else if (j->
TS <= i->
TS &&
1167 j -> parent = a -> sister;
1169 j -> DIST = i -> DIST + 1;
1183 j -> parent = a -> sister;
1185 j -> DIST = i -> DIST + 1;
1189 else if (!j->
is_sink) { a = a -> sister;
break; }
1190 else if (j->
TS <= i->
TS &&
1194 j -> parent = a -> sister;
1196 j -> DIST = i -> DIST + 1;
1215 np_next = np -> next;
1232 else current_node = NULL;
1249 template <
typename captype,
typename tcaptype,
typename flowtype>
1255 int num1 = 0, num2 = 0;
1260 if (i->
next || i==current_node) num1 ++;
1266 for ( ; ; i=i->
next)
1272 else assert(i == current_node);
1277 assert(num1 == num2);
1282 if (i->
parent == NULL) {}
1287 else assert(i->
tr_cap < 0);
void process_sink_orphan(node *i)
Block< node_id > * changed_list
void set_orphan_front(node *i)
DBlock< nodeptr > * nodeptr_block
void set_trcap(node_id i, tcaptype trcap)
void mark_node(node_id i)
void augment(arc *middle_arc)
void reallocate_nodes(int num)
void process_source_orphan(node *i)
static const int NODEPTR_BLOCK_SIZE
void get_arc_ends(arc_id a, node_id &i, node_id &j)
void set_orphan_rear(node *i)
void add_edge(node_id i, node_id j, captype cap, captype rev_cap)
void(* error_function)(char *)
flowtype maxflow(bool reuse_trees=false, Block< node_id > *changed_list=NULL)
node_id add_node(int num=1)
void remove_from_changed_list(node_id i)
arc_id get_next_arc(arc_id a)
void maxflow_reuse_trees_init()
void test_consistency(node *current_node=NULL)
void add_to_changed_list(node *i)
Graph(int node_num_max, int edge_num_max, void(*err_function)(char *)=NULL)
void add_tweights(node_id i, tcaptype cap_source, tcaptype cap_sink)
void set_rcap(arc *a, captype rcap)
tcaptype get_trcap(node_id i)
termtype what_segment(node_id i, termtype default_segm=SOURCE)