232 template <
class Type>
class Block
239 Block(
int size,
void (*err_function)(
char *) = NULL) { first = last = NULL; block_size = size; error_function = err_function; }
242 ~Block() {
while (first) { block *next = first -> next;
delete[] ((
char*)first); first = next; } }
247 Type *New(
int num = 1)
251 if (!last || last->current + num > last->last)
253 if (last && last->next) last = last -> next;
256 block *next = (block *)
new char [
sizeof(block) + (block_size-1)*
sizeof(Type)];
257 if (!next) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
258 if (last) last -> next = next;
261 last -> current = & ( last -> data[0] );
262 last -> last = last -> current + block_size;
268 last -> current += num;
275 for (scan_current_block=first; scan_current_block; scan_current_block = scan_current_block->next)
277 scan_current_data = & ( scan_current_block -> data[0] );
278 if (scan_current_data < scan_current_block -> current)
return scan_current_data ++;
288 while (scan_current_data >= scan_current_block -> current)
290 scan_current_block = scan_current_block -> next;
291 if (!scan_current_block)
return NULL;
292 scan_current_data = & ( scan_current_block -> data[0] );
294 return scan_current_data ++;
302 for (b=first; ; b=b->next)
304 b -> current = & ( b -> data[0] );
305 if (b == last)
break;
314 typedef struct block_st
316 Type *current, *last;
317 struct block_st *next;
325 block *scan_current_block;
326 Type *scan_current_data;
328 void (*error_function)(
char *);
335 template <
class Type>
class DBlock
342 DBlock(
int size,
void (*err_function)(
char *) = NULL) { first = NULL; first_free = NULL; block_size = size; error_function = err_function; }
345 ~DBlock() {
while (first) { block *next = first -> next;
delete[] ((
char*)first); first = next; } }
355 first = (block *)
new char [
sizeof(block) + (block_size-1)*
sizeof(block_item)];
356 if (!first) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
357 first_free = & (first -> data[0] );
358 for (item=first_free; item<first_free+block_size-1; item++)
359 item -> next_free = item + 1;
360 item -> next_free = NULL;
361 first -> next = next;
365 first_free = item -> next_free;
366 return (Type *) item;
372 ((block_item *) t) -> next_free = first_free;
373 first_free = (block_item *) t;
380 typedef union block_item_st
383 block_item_st *next_free;
386 typedef struct block_st
388 struct block_st *next;
394 block_item *first_free;
396 void (*error_function)(
char *);
437 #define NODE_BLOCK_SIZE 512
438 #define ARC_BLOCK_SIZE 1024
439 #define NODEPTR_BLOCK_SIZE 128
452 typedef double captype;
454 typedef double flowtype;
456 typedef void * node_id;
464 Graph(
void (*err_function)(
char *) = NULL);
474 void add_edge(node_id from, node_id to, captype cap, captype rev_cap);
479 void set_tweights(node_id i, captype cap_source, captype cap_sink);
484 void add_tweights(node_id i, captype cap_source, captype cap_sink);
488 termtype what_segment(node_id i);
500 struct arc_forward_st;
501 struct arc_reverse_st;
503 #define IS_ODD(a) ((int)(a) & 1)
504 #define MAKE_ODD(a) ((arc_forward *) ((int)(a) | 1))
505 #define MAKE_EVEN(a) ((arc_forward *) ((int)(a) & (~1)))
506 #define MAKE_ODD_REV(a) ((arc_reverse *) ((int)(a) | 1))
507 #define MAKE_EVEN_REV(a) ((arc_reverse *) ((int)(a) & (~1)))
510 typedef struct node_st
528 arc_forward_st *first_out;
529 arc_reverse_st *first_in;
531 arc_forward_st *parent;
547 #define NEIGHBOR_NODE(i, shift) ((node *) ((char *)(i) + (shift)))
548 #define NEIGHBOR_NODE_REV(i, shift) ((node *) ((char *)(i) - (shift)))
549 typedef struct arc_forward_st
556 typedef struct arc_reverse_st
562 typedef struct nodeptr_st
568 typedef struct node_block_st
571 struct node_block_st *next;
572 node nodes[NODE_BLOCK_SIZE];
575 #define last_node LAST_NODE.LAST_NODE
577 typedef struct arc_for_block_st
582 arc_forward *current;
583 struct arc_for_block_st *next;
584 arc_forward arcs_for[ARC_BLOCK_SIZE];
592 typedef struct arc_rev_block_st
597 arc_reverse *current;
598 struct arc_rev_block_st *next;
599 arc_reverse arcs_rev[ARC_BLOCK_SIZE];
607 node_block *node_block_first;
608 arc_for_block *arc_for_block_first;
609 arc_rev_block *arc_rev_block_first;
610 DBlock<nodeptr> *nodeptr_block;
612 void (*error_function)(
char *);
620 node *queue_first[2], *queue_last[2];
621 nodeptr *orphan_first, *orphan_last;
627 void set_active(node *i);
630 void prepare_graph();
632 void augment(node *s_start, node *t_start, captype *cap_middle, captype *rev_cap_middle);
633 void process_source_orphan(node *i);
634 void process_sink_orphan(node *i);
642 inline Graph::Graph(
void (*err_function)(
char *))
644 error_function = err_function;
645 node_block_first = NULL;
646 arc_for_block_first = NULL;
647 arc_rev_block_first = NULL;
651 inline Graph::~Graph()
653 while (node_block_first)
655 node_block *next = node_block_first -> next;
656 delete node_block_first;
657 node_block_first = next;
660 while (arc_for_block_first)
662 arc_for_block *next = arc_for_block_first -> next;
663 delete arc_for_block_first -> start;
664 arc_for_block_first = next;
667 while (arc_rev_block_first)
669 arc_rev_block *next = arc_rev_block_first -> next;
670 delete arc_rev_block_first -> start;
671 arc_rev_block_first = next;
675 inline Graph::node_id Graph::add_node()
679 if (!node_block_first || node_block_first->current+1 > &node_block_first->nodes[NODE_BLOCK_SIZE-1])
681 node_block *next = node_block_first;
682 node_block_first = (node_block *)
new node_block;
683 if (!node_block_first) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
684 node_block_first -> current = & ( node_block_first -> nodes[0] );
685 node_block_first -> next = next;
688 i = node_block_first -> current ++;
689 i -> first_out = (arc_forward *) 0;
690 i -> first_in = (arc_reverse *) 0;
697 inline void Graph::add_edge(node_id from, node_id to, captype cap, captype rev_cap)
702 if (!arc_for_block_first || arc_for_block_first->current+1 > &arc_for_block_first->arcs_for[ARC_BLOCK_SIZE])
704 arc_for_block *next = arc_for_block_first;
705 char *ptr =
new char[
sizeof(arc_for_block)+1];
706 if (!ptr) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
707 if ((
int)ptr & 1) arc_for_block_first = (arc_for_block *) (ptr + 1);
708 else arc_for_block_first = (arc_for_block *) ptr;
709 arc_for_block_first -> start = ptr;
710 arc_for_block_first -> current = & ( arc_for_block_first -> arcs_for[0] );
711 arc_for_block_first -> next = next;
714 if (!arc_rev_block_first || arc_rev_block_first->current+1 > &arc_rev_block_first->arcs_rev[ARC_BLOCK_SIZE])
716 arc_rev_block *next = arc_rev_block_first;
717 char *ptr =
new char[
sizeof(arc_rev_block)+1];
718 if (!ptr) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
719 if ((
int)ptr & 1) arc_rev_block_first = (arc_rev_block *) (ptr + 1);
720 else arc_rev_block_first = (arc_rev_block *) ptr;
721 arc_rev_block_first -> start = ptr;
722 arc_rev_block_first -> current = & ( arc_rev_block_first -> arcs_rev[0] );
723 arc_rev_block_first -> next = next;
726 a_for = arc_for_block_first -> current ++;
727 a_rev = arc_rev_block_first -> current ++;
729 a_rev -> sister = (arc_forward *) from;
730 a_for -> shift = (int) to;
731 a_for -> r_cap = cap;
732 a_for -> r_rev_cap = rev_cap;
734 ((node *)from) -> first_out =
735 (arc_forward *) ((
int)(((node *)from) -> first_out) + 1);
736 ((node *)to) -> first_in =
737 (arc_reverse *) ((
int)(((node *)to) -> first_in) + 1);
740 inline void Graph::set_tweights(node_id i, captype cap_source, captype cap_sink)
742 flow += (cap_source < cap_sink) ? cap_source : cap_sink;
743 ((node*)i) -> tr_cap = cap_source - cap_sink;
746 inline void Graph::add_tweights(node_id i, captype cap_source, captype cap_sink)
748 register captype delta = ((node*)i) -> tr_cap;
749 if (delta > 0) cap_source += delta;
750 else cap_sink -= delta;
751 flow += (cap_source < cap_sink) ? cap_source : cap_sink;
752 ((node*)i) -> tr_cap = cap_source - cap_sink;
767 inline void Graph::prepare_graph()
770 arc_for_block *ab_for, *ab_for_first;
771 arc_rev_block *ab_rev, *ab_rev_first, *ab_rev_scan;
773 arc_reverse *a_rev, *a_rev_scan, a_rev_tmp;
775 bool for_flag =
false, rev_flag =
false;
778 if (!arc_rev_block_first)
780 node_id from = add_node(), to = add_node();
781 add_edge(from, to, 1, 0);
785 a_rev_tmp.sister = NULL;
786 for (a_rev=arc_rev_block_first->current; a_rev<&arc_rev_block_first->arcs_rev[ARC_BLOCK_SIZE]; a_rev++)
788 a_rev -> sister = NULL;
791 ab_for = ab_for_first = arc_for_block_first;
792 ab_rev = ab_rev_first = ab_rev_scan = arc_rev_block_first;
793 a_for = &ab_for->arcs_for[0];
794 a_rev = a_rev_scan = &ab_rev->arcs_rev[0];
796 for (nb=node_block_first; nb; nb=nb->next)
798 for (i=&nb->nodes[0]; i<nb->current; i++)
801 k = (int) i -> first_out;
802 if (a_for + k > &ab_for->arcs_for[ARC_BLOCK_SIZE])
804 if (k > ARC_BLOCK_SIZE) {
if (error_function) (*error_function)(
"# of arcs per node exceeds block size!"); exit(1); }
805 if (for_flag) ab_for = NULL;
806 else { ab_for = ab_for -> next; ab_rev_scan = ab_rev_scan -> next; }
809 arc_for_block *next = arc_for_block_first;
810 char *ptr =
new char[
sizeof(arc_for_block)+1];
811 if (!ptr) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
812 if ((
int)ptr & 1) arc_for_block_first = (arc_for_block *) (ptr + 1);
813 else arc_for_block_first = (arc_for_block *) ptr;
814 arc_for_block_first -> start = ptr;
815 arc_for_block_first -> current = & ( arc_for_block_first -> arcs_for[0] );
816 arc_for_block_first -> next = next;
817 ab_for = arc_for_block_first;
820 else a_rev_scan = &ab_rev_scan->arcs_rev[0];
821 a_for = &ab_for->arcs_for[0];
826 i -> parent = (arc_forward *) a_rev_scan;
828 else i -> parent = (arc_forward *) &a_rev_tmp;
830 i -> first_out = a_for;
831 ab_for -> last_node = i;
834 k = (int) i -> first_in;
835 if (a_rev + k > &ab_rev->arcs_rev[ARC_BLOCK_SIZE])
837 if (k > ARC_BLOCK_SIZE) {
if (error_function) (*error_function)(
"# of arcs per node exceeds block size!"); exit(1); }
838 if (rev_flag) ab_rev = NULL;
839 else ab_rev = ab_rev -> next;
842 arc_rev_block *next = arc_rev_block_first;
843 char *ptr =
new char[
sizeof(arc_rev_block)+1];
844 if (!ptr) {
if (error_function) (*error_function)(
"Not enough memory!"); exit(1); }
845 if ((
int)ptr & 1) arc_rev_block_first = (arc_rev_block *) (ptr + 1);
846 else arc_rev_block_first = (arc_rev_block *) ptr;
847 arc_rev_block_first -> start = ptr;
848 arc_rev_block_first -> current = & ( arc_rev_block_first -> arcs_rev[0] );
849 arc_rev_block_first -> next = next;
850 ab_rev = arc_rev_block_first;
853 a_rev = &ab_rev->arcs_rev[0];
856 i -> first_in = a_rev;
857 ab_rev -> last_node = i;
860 i -> first_out = a_for;
861 i -> first_in = a_rev;
865 for (ab_for=arc_for_block_first; ab_for; ab_for=ab_for->next)
867 ab_for -> current = ab_for -> last_node -> first_out;
870 for ( ab_for=ab_for_first, ab_rev=ab_rev_first;
872 ab_for=ab_for->next, ab_rev=ab_rev->next )
873 for ( a_for=&ab_for->arcs_for[0], a_rev=&ab_rev->arcs_rev[0];
874 a_for<&ab_for->arcs_for[ARC_BLOCK_SIZE];
880 int shift = 0, shift_new;
881 captype r_cap, r_rev_cap, r_cap_new, r_rev_cap_new;
883 if (!(from=(node *)(a_rev->sister)))
continue;
891 shift_new = ((
char *)(af->shift)) - (
char *)from;
892 r_cap_new = af -> r_cap;
893 r_rev_cap_new = af -> r_rev_cap;
898 af -> r_rev_cap = r_rev_cap;
902 r_rev_cap = r_rev_cap_new;
904 af = -- from -> first_out;
905 if ((arc_reverse *)(from->parent) != &a_rev_tmp)
907 from -> parent = (arc_forward *)(((arc_reverse *)(from -> parent)) - 1);
908 ar = (arc_reverse *)(from -> parent);
910 }
while (from=(node *)(ar->sister));
914 af -> r_rev_cap = r_rev_cap;
917 for (ab_for=arc_for_block_first; ab_for; ab_for=ab_for->next)
919 i = ab_for -> last_node;
920 a_for = i -> first_out;
921 ab_for -> current -> shift = a_for -> shift;
922 ab_for -> current -> r_cap = a_for -> r_cap;
923 ab_for -> current -> r_rev_cap = a_for -> r_rev_cap;
924 a_for -> shift = (int) (ab_for -> current + 1);
925 i -> first_out = (arc_forward *) (((
char *)a_for) - 1);
929 for (ab_rev=arc_rev_block_first; ab_rev; ab_rev=ab_rev->next)
931 ab_rev -> current = ab_rev -> last_node -> first_in;
934 for (nb=node_block_first; nb; nb=nb->next)
935 for (i=&nb->nodes[0]; i<nb->current; i++)
937 arc_forward *a_for_first, *a_for_last;
939 a_for_first = i -> first_out;
940 if (IS_ODD(a_for_first))
942 a_for_first = (arc_forward *) (((
char *)a_for_first) + 1);
943 a_for_last = (arc_forward *) ((a_for_first ++) -> shift);
945 else a_for_last = (i + 1) -> first_out;
947 for (a_for=a_for_first; a_for<a_for_last; a_for++)
949 node *to = NEIGHBOR_NODE(i, a_for -> shift);
950 a_rev = -- to -> first_in;
951 a_rev -> sister = a_for;
955 for (ab_rev=arc_rev_block_first; ab_rev; ab_rev=ab_rev->next)
957 i = ab_rev -> last_node;
958 a_rev = i -> first_in;
959 ab_rev -> current -> sister = a_rev -> sister;
960 a_rev -> sister = (arc_forward *) (ab_rev -> current + 1);
961 i -> first_in = (arc_reverse *) (((
char *)a_rev) - 1);
973 #define TERMINAL ( (arc_forward *) 1 )
974 #define ORPHAN ( (arc_forward *) 2 )
976 #define INFINITE_D 1000000000
993 inline void Graph::set_active(node *i)
998 if (queue_last[1]) queue_last[1] -> next = i;
999 else queue_first[1] = i;
1010 inline Graph::node * Graph::next_active()
1016 if (!(i=queue_first[0]))
1018 queue_first[0] = i = queue_first[1];
1019 queue_last[0] = queue_last[1];
1020 queue_first[1] = NULL;
1021 queue_last[1] = NULL;
1022 if (!i)
return NULL;
1026 if (i->next == i) queue_first[0] = queue_last[0] = NULL;
1027 else queue_first[0] = i -> next;
1031 if (i->parent)
return i;
1037 inline void Graph::maxflow_init()
1042 queue_first[0] = queue_last[0] = NULL;
1043 queue_first[1] = queue_last[1] = NULL;
1044 orphan_first = NULL;
1046 for (nb=node_block_first; nb; nb=nb->next)
1047 for (i=&nb->nodes[0]; i<nb->current; i++)
1055 i -> parent = TERMINAL;
1060 else if (i->tr_cap < 0)
1064 i -> parent = TERMINAL;
1079 inline void Graph::augment(node *s_start, node *t_start, captype *cap_middle, captype *rev_cap_middle)
1089 bottleneck = *cap_middle;
1093 if (a == TERMINAL)
break;
1097 if (bottleneck > a->r_cap) bottleneck = a -> r_cap;
1098 i = NEIGHBOR_NODE_REV(i, a -> shift);
1102 if (bottleneck > a->r_rev_cap) bottleneck = a -> r_rev_cap;
1103 i = NEIGHBOR_NODE(i, a -> shift);
1106 if (bottleneck > i->tr_cap) bottleneck = i -> tr_cap;
1111 if (a == TERMINAL)
break;
1115 if (bottleneck > a->r_rev_cap) bottleneck = a -> r_rev_cap;
1116 i = NEIGHBOR_NODE_REV(i, a -> shift);
1120 if (bottleneck > a->r_cap) bottleneck = a -> r_cap;
1121 i = NEIGHBOR_NODE(i, a -> shift);
1124 if (bottleneck > - i->tr_cap) bottleneck = - i -> tr_cap;
1129 *rev_cap_middle += bottleneck;
1130 *cap_middle -= bottleneck;
1134 if (a == TERMINAL)
break;
1138 a -> r_rev_cap += bottleneck;
1139 a -> r_cap -= bottleneck;
1143 i -> parent = ORPHAN;
1144 np = nodeptr_block -> New();
1146 np -> next = orphan_first;
1149 i = NEIGHBOR_NODE_REV(i, a -> shift);
1153 a -> r_cap += bottleneck;
1154 a -> r_rev_cap -= bottleneck;
1158 i -> parent = ORPHAN;
1159 np = nodeptr_block -> New();
1161 np -> next = orphan_first;
1164 i = NEIGHBOR_NODE(i, a -> shift);
1167 i -> tr_cap -= bottleneck;
1171 i -> parent = ORPHAN;
1172 np = nodeptr_block -> New();
1174 np -> next = orphan_first;
1181 if (a == TERMINAL)
break;
1185 a -> r_cap += bottleneck;
1186 a -> r_rev_cap -= bottleneck;
1190 i -> parent = ORPHAN;
1191 np = nodeptr_block -> New();
1193 np -> next = orphan_first;
1196 i = NEIGHBOR_NODE_REV(i, a -> shift);
1200 a -> r_rev_cap += bottleneck;
1201 a -> r_cap -= bottleneck;
1205 i -> parent = ORPHAN;
1206 np = nodeptr_block -> New();
1208 np -> next = orphan_first;
1211 i = NEIGHBOR_NODE(i, a -> shift);
1214 i -> tr_cap += bottleneck;
1218 i -> parent = ORPHAN;
1219 np = nodeptr_block -> New();
1221 np -> next = orphan_first;
1231 inline void Graph::process_source_orphan(node *i)
1234 arc_forward *a0_for, *a0_for_first, *a0_for_last;
1235 arc_reverse *a0_rev, *a0_rev_first, *a0_rev_last;
1236 arc_forward *a0_min = NULL, *a;
1238 int d, d_min = INFINITE_D;
1241 a0_for_first = i -> first_out;
1242 if (IS_ODD(a0_for_first))
1244 a0_for_first = (arc_forward *) (((
char *)a0_for_first) + 1);
1245 a0_for_last = (arc_forward *) ((a0_for_first ++) -> shift);
1247 else a0_for_last = (i + 1) -> first_out;
1248 a0_rev_first = i -> first_in;
1249 if (IS_ODD(a0_rev_first))
1251 a0_rev_first = (arc_reverse *) (((
char *)a0_rev_first) + 1);
1252 a0_rev_last = (arc_reverse *) ((a0_rev_first ++) -> sister);
1254 else a0_rev_last = (i + 1) -> first_in;
1257 for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++)
1258 if (a0_for->r_rev_cap)
1260 j = NEIGHBOR_NODE(i, a0_for -> shift);
1261 if (!j->is_sink && (a=j->parent))
1280 if (a==ORPHAN) { d = INFINITE_D;
break; }
1282 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1284 j = NEIGHBOR_NODE(j, a -> shift);
1294 for (j=NEIGHBOR_NODE(i, a0_for->shift); j->TS!=TIME; )
1300 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1302 j = NEIGHBOR_NODE(j, a -> shift);
1307 for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++)
1309 a0_for = a0_rev -> sister;
1312 j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
1313 if (!j->is_sink && (a=j->parent))
1332 if (a==ORPHAN) { d = INFINITE_D;
break; }
1334 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1336 j = NEIGHBOR_NODE(j, a -> shift);
1342 a0_min = MAKE_ODD(a0_for);
1346 for (j=NEIGHBOR_NODE_REV(i,a0_for->shift); j->TS!=TIME; )
1352 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1354 j = NEIGHBOR_NODE(j, a -> shift);
1361 if (i->parent = a0_min)
1364 i -> DIST = d_min + 1;
1372 for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++)
1374 j = NEIGHBOR_NODE(i, a0_for -> shift);
1375 if (!j->is_sink && (a=j->parent))
1377 if (a0_for->r_rev_cap) set_active(j);
1378 if (a!=TERMINAL && a!=ORPHAN && IS_ODD(a) && NEIGHBOR_NODE_REV(j, MAKE_EVEN(a)->shift)==i)
1381 j -> parent = ORPHAN;
1382 np = nodeptr_block -> New();
1384 if (orphan_last) orphan_last -> next = np;
1385 else orphan_first = np;
1391 for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++)
1393 a0_for = a0_rev -> sister;
1394 j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
1395 if (!j->is_sink && (a=j->parent))
1397 if (a0_for->r_cap) set_active(j);
1398 if (a!=TERMINAL && a!=ORPHAN && !IS_ODD(a) && NEIGHBOR_NODE(j, a->shift)==i)
1401 j -> parent = ORPHAN;
1402 np = nodeptr_block -> New();
1404 if (orphan_last) orphan_last -> next = np;
1405 else orphan_first = np;
1414 inline void Graph::process_sink_orphan(node *i)
1417 arc_forward *a0_for, *a0_for_first, *a0_for_last;
1418 arc_reverse *a0_rev, *a0_rev_first, *a0_rev_last;
1419 arc_forward *a0_min = NULL, *a;
1421 int d, d_min = INFINITE_D;
1424 a0_for_first = i -> first_out;
1425 if (IS_ODD(a0_for_first))
1427 a0_for_first = (arc_forward *) (((
char *)a0_for_first) + 1);
1428 a0_for_last = (arc_forward *) ((a0_for_first ++) -> shift);
1430 else a0_for_last = (i + 1) -> first_out;
1431 a0_rev_first = i -> first_in;
1432 if (IS_ODD(a0_rev_first))
1434 a0_rev_first = (arc_reverse *) (((
char *)a0_rev_first) + 1);
1435 a0_rev_last = (arc_reverse *) ((a0_rev_first ++) -> sister);
1437 else a0_rev_last = (i + 1) -> first_in;
1440 for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++)
1443 j = NEIGHBOR_NODE(i, a0_for -> shift);
1444 if (j->is_sink && (a=j->parent))
1463 if (a==ORPHAN) { d = INFINITE_D;
break; }
1465 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1467 j = NEIGHBOR_NODE(j, a -> shift);
1477 for (j=NEIGHBOR_NODE(i, a0_for->shift); j->TS!=TIME; )
1483 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1485 j = NEIGHBOR_NODE(j, a -> shift);
1490 for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++)
1492 a0_for = a0_rev -> sister;
1493 if (a0_for->r_rev_cap)
1495 j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
1496 if (j->is_sink && (a=j->parent))
1515 if (a==ORPHAN) { d = INFINITE_D;
break; }
1517 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1519 j = NEIGHBOR_NODE(j, a -> shift);
1525 a0_min = MAKE_ODD(a0_for);
1529 for (j=NEIGHBOR_NODE_REV(i,a0_for->shift); j->TS!=TIME; )
1535 j = NEIGHBOR_NODE_REV(j, MAKE_EVEN(a) -> shift);
1537 j = NEIGHBOR_NODE(j, a -> shift);
1544 if (i->parent = a0_min)
1547 i -> DIST = d_min + 1;
1555 for (a0_for=a0_for_first; a0_for<a0_for_last; a0_for++)
1557 j = NEIGHBOR_NODE(i, a0_for -> shift);
1558 if (j->is_sink && (a=j->parent))
1560 if (a0_for->r_cap) set_active(j);
1561 if (a!=TERMINAL && a!=ORPHAN && IS_ODD(a) && NEIGHBOR_NODE_REV(j, MAKE_EVEN(a)->shift)==i)
1564 j -> parent = ORPHAN;
1565 np = nodeptr_block -> New();
1567 if (orphan_last) orphan_last -> next = np;
1568 else orphan_first = np;
1574 for (a0_rev=a0_rev_first; a0_rev<a0_rev_last; a0_rev++)
1576 a0_for = a0_rev -> sister;
1577 j = NEIGHBOR_NODE_REV(i, a0_for -> shift);
1578 if (j->is_sink && (a=j->parent))
1580 if (a0_for->r_rev_cap) set_active(j);
1581 if (a!=TERMINAL && a!=ORPHAN && !IS_ODD(a) && NEIGHBOR_NODE(j, a->shift)==i)
1584 j -> parent = ORPHAN;
1585 np = nodeptr_block -> New();
1587 if (orphan_last) orphan_last -> next = np;
1588 else orphan_first = np;
1599 inline Graph::flowtype Graph::maxflow()
1601 node *i, *j, *current_node = NULL, *s_start, *t_start;
1602 captype *cap_middle, *rev_cap_middle;
1603 arc_forward *a_for, *a_for_first, *a_for_last;
1604 arc_reverse *a_rev, *a_rev_first, *a_rev_last;
1605 nodeptr *np, *np_next;
1609 nodeptr_block =
new DBlock<nodeptr>(NODEPTR_BLOCK_SIZE, error_function);
1616 if (!i->parent) i = NULL;
1620 if (!(i = next_active()))
break;
1626 a_for_first = i -> first_out;
1627 if (IS_ODD(a_for_first))
1629 a_for_first = (arc_forward *) (((
char *)a_for_first) + 1);
1630 a_for_last = (arc_forward *) ((a_for_first ++) -> shift);
1632 else a_for_last = (i + 1) -> first_out;
1633 a_rev_first = i -> first_in;
1634 if (IS_ODD(a_rev_first))
1636 a_rev_first = (arc_reverse *) (((
char *)a_rev_first) + 1);
1637 a_rev_last = (arc_reverse *) ((a_rev_first ++) -> sister);
1639 else a_rev_last = (i + 1) -> first_in;
1644 for (a_for=a_for_first; a_for<a_for_last; a_for++)
1647 j = NEIGHBOR_NODE(i, a_for -> shift);
1651 j -> parent = MAKE_ODD(a_for);
1653 j -> DIST = i -> DIST + 1;
1656 else if (j->is_sink)
1660 cap_middle = & ( a_for -> r_cap );
1661 rev_cap_middle = & ( a_for -> r_rev_cap );
1664 else if (j->TS <= i->TS &&
1668 j -> parent = MAKE_ODD(a_for);
1670 j -> DIST = i -> DIST + 1;
1674 for (a_rev=a_rev_first; a_rev<a_rev_last; a_rev++)
1676 a_for = a_rev -> sister;
1677 if (a_for->r_rev_cap)
1679 j = NEIGHBOR_NODE_REV(i, a_for -> shift);
1683 j -> parent = a_for;
1685 j -> DIST = i -> DIST + 1;
1688 else if (j->is_sink)
1692 cap_middle = & ( a_for -> r_rev_cap );
1693 rev_cap_middle = & ( a_for -> r_cap );
1696 else if (j->TS <= i->TS &&
1700 j -> parent = a_for;
1702 j -> DIST = i -> DIST + 1;
1710 for (a_for=a_for_first; a_for<a_for_last; a_for++)
1711 if (a_for->r_rev_cap)
1713 j = NEIGHBOR_NODE(i, a_for -> shift);
1717 j -> parent = MAKE_ODD(a_for);
1719 j -> DIST = i -> DIST + 1;
1722 else if (!j->is_sink)
1726 cap_middle = & ( a_for -> r_rev_cap );
1727 rev_cap_middle = & ( a_for -> r_cap );
1730 else if (j->TS <= i->TS &&
1734 j -> parent = MAKE_ODD(a_for);
1736 j -> DIST = i -> DIST + 1;
1739 for (a_rev=a_rev_first; a_rev<a_rev_last; a_rev++)
1741 a_for = a_rev -> sister;
1744 j = NEIGHBOR_NODE_REV(i, a_for -> shift);
1748 j -> parent = a_for;
1750 j -> DIST = i -> DIST + 1;
1753 else if (!j->is_sink)
1757 cap_middle = & ( a_for -> r_cap );
1758 rev_cap_middle = & ( a_for -> r_rev_cap );
1761 else if (j->TS <= i->TS &&
1765 j -> parent = a_for;
1767 j -> DIST = i -> DIST + 1;
1781 augment(s_start, t_start, cap_middle, rev_cap_middle);
1785 while (np=orphan_first)
1787 np_next = np -> next;
1790 while (np=orphan_first)
1792 orphan_first = np -> next;
1794 nodeptr_block -> Delete(np);
1795 if (!orphan_first) orphan_last = NULL;
1796 if (i->is_sink) process_sink_orphan(i);
1797 else process_source_orphan(i);
1800 orphan_first = np_next;
1804 else current_node = NULL;
1807 delete nodeptr_block;
1814 inline Graph::termtype Graph::what_segment(node_id i)
1816 if (((node*)i)->parent && !((node*)i)->is_sink)
return SOURCE;