880 _cost.resize(all_edge_num); |
885 _cost.resize(all_edge_num); |
881 _supply.resize(all_node_num); |
886 _supply.resize(all_node_num); |
882 _flow.resize(all_edge_num, 0); |
887 _flow.resize(all_edge_num, 0); |
883 _pi.resize(all_node_num, 0); |
888 _pi.resize(all_node_num, 0); |
884 |
889 |
885 _depth.resize(all_node_num); |
|
886 _parent.resize(all_node_num); |
890 _parent.resize(all_node_num); |
887 _pred.resize(all_node_num); |
891 _pred.resize(all_node_num); |
|
892 _forward.resize(all_node_num); |
888 _thread.resize(all_node_num); |
893 _thread.resize(all_node_num); |
889 _forward.resize(all_node_num); |
894 _rev_thread.resize(all_node_num); |
|
895 _succ_num.resize(all_node_num); |
|
896 _last_succ.resize(all_node_num); |
890 _state.resize(all_edge_num, STATE_LOWER); |
897 _state.resize(all_edge_num, STATE_LOWER); |
891 |
898 |
892 // Initialize node related data |
899 // Initialize node related data |
893 bool valid_supply = true; |
900 bool valid_supply = true; |
894 if (_orig_supply) { |
901 if (_orig_supply) { |
953 |
962 |
954 // Add artificial edges and initialize the spanning tree data structure |
963 // Add artificial edges and initialize the spanning tree data structure |
955 Cost max_cost = std::numeric_limits<Cost>::max() / 4; |
964 Cost max_cost = std::numeric_limits<Cost>::max() / 4; |
956 Capacity max_cap = std::numeric_limits<Capacity>::max(); |
965 Capacity max_cap = std::numeric_limits<Capacity>::max(); |
957 for (int u = 0, e = _edge_num; u != _node_num; ++u, ++e) { |
966 for (int u = 0, e = _edge_num; u != _node_num; ++u, ++e) { |
958 _thread[u] = u + 1; |
|
959 _depth[u] = 1; |
|
960 _parent[u] = _root; |
967 _parent[u] = _root; |
961 _pred[u] = e; |
968 _pred[u] = e; |
|
969 _thread[u] = u + 1; |
|
970 _rev_thread[u + 1] = u; |
|
971 _succ_num[u] = 1; |
|
972 _last_succ[u] = u; |
|
973 _cap[e] = max_cap; |
|
974 _state[e] = STATE_TREE; |
962 if (_supply[u] >= 0) { |
975 if (_supply[u] >= 0) { |
|
976 _forward[u] = true; |
|
977 _pi[u] = 0; |
|
978 _source[e] = u; |
|
979 _target[e] = _root; |
963 _flow[e] = _supply[u]; |
980 _flow[e] = _supply[u]; |
964 _forward[u] = true; |
981 _cost[e] = 0; |
965 _pi[u] = -max_cost; |
982 } |
966 } else { |
983 else { |
967 _flow[e] = -_supply[u]; |
|
968 _forward[u] = false; |
984 _forward[u] = false; |
969 _pi[u] = max_cost; |
985 _pi[u] = max_cost; |
970 } |
986 _source[e] = _root; |
971 _cost[e] = max_cost; |
987 _target[e] = u; |
972 _cap[e] = max_cap; |
988 _flow[e] = -_supply[u]; |
973 _state[e] = STATE_TREE; |
989 _cost[e] = max_cost; |
|
990 } |
974 } |
991 } |
975 |
992 |
976 return true; |
993 return true; |
977 } |
994 } |
978 |
995 |
979 // Find the join node |
996 // Find the join node |
980 void findJoinNode() { |
997 void findJoinNode() { |
981 int u = _source[_in_edge]; |
998 int u = _source[_in_edge]; |
982 int v = _target[_in_edge]; |
999 int v = _target[_in_edge]; |
983 while (_depth[u] > _depth[v]) u = _parent[u]; |
|
984 while (_depth[v] > _depth[u]) v = _parent[v]; |
|
985 while (u != v) { |
1000 while (u != v) { |
986 u = _parent[u]; |
1001 if (_succ_num[u] < _succ_num[v]) { |
987 v = _parent[v]; |
1002 u = _parent[u]; |
|
1003 } else { |
|
1004 v = _parent[v]; |
|
1005 } |
988 } |
1006 } |
989 join = u; |
1007 join = u; |
990 } |
1008 } |
991 |
1009 |
992 // Find the leaving edge of the cycle and returns true if the |
1010 // Find the leaving edge of the cycle and returns true if the |
1057 (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; |
1075 (_flow[_pred[u_out]] == 0) ? STATE_LOWER : STATE_UPPER; |
1058 } else { |
1076 } else { |
1059 _state[_in_edge] = -_state[_in_edge]; |
1077 _state[_in_edge] = -_state[_in_edge]; |
1060 } |
1078 } |
1061 } |
1079 } |
1062 |
1080 |
1063 // Update _thread and _parent vectors |
1081 // Update the tree structure |
1064 void updateThreadParent() { |
1082 void updateTreeStructure() { |
1065 int u; |
1083 int u, w; |
|
1084 int old_rev_thread = _rev_thread[u_out]; |
|
1085 int old_succ_num = _succ_num[u_out]; |
|
1086 int old_last_succ = _last_succ[u_out]; |
1066 v_out = _parent[u_out]; |
1087 v_out = _parent[u_out]; |
1067 |
1088 |
1068 // Handle the case when join and v_out coincide |
1089 u = _last_succ[u_in]; // the last successor of u_in |
1069 bool par_first = false; |
1090 right = _thread[u]; // the node after it |
1070 if (join == v_out) { |
1091 |
1071 for (u = join; u != u_in && u != v_in; u = _thread[u]) ; |
1092 // Handle the case when old_rev_thread equals to v_in |
1072 if (u == v_in) { |
1093 // (it also means that join and v_out coincide) |
1073 par_first = true; |
1094 if (old_rev_thread == v_in) { |
1074 while (_thread[u] != u_out) u = _thread[u]; |
1095 last = _thread[_last_succ[u_out]]; |
1075 first = u; |
1096 } else { |
1076 } |
1097 last = _thread[v_in]; |
1077 } |
1098 } |
1078 |
1099 |
1079 // Find the last successor of u_in (u) and the node after it (right) |
1100 // Update _thread and _parent along the stem nodes (i.e. the nodes |
1080 // according to the thread index |
1101 // between u_in and u_out, whose parent have to be changed) |
1081 for (u = u_in; _depth[_thread[u]] > _depth[u_in]; u = _thread[u]) ; |
|
1082 right = _thread[u]; |
|
1083 if (_thread[v_in] == u_out) { |
|
1084 for (last = u; _depth[last] > _depth[u_out]; last = _thread[last]) ; |
|
1085 if (last == u_out) last = _thread[last]; |
|
1086 } |
|
1087 else last = _thread[v_in]; |
|
1088 |
|
1089 // Update stem nodes |
|
1090 _thread[v_in] = stem = u_in; |
1102 _thread[v_in] = stem = u_in; |
|
1103 _dirty_revs.clear(); |
|
1104 _dirty_revs.push_back(v_in); |
1091 par_stem = v_in; |
1105 par_stem = v_in; |
1092 while (stem != u_out) { |
1106 while (stem != u_out) { |
1093 _thread[u] = new_stem = _parent[stem]; |
1107 // Insert the next stem node into the thread list |
1094 |
1108 new_stem = _parent[stem]; |
1095 // Find the node just before the stem node (u) according to |
1109 _thread[u] = new_stem; |
1096 // the original thread index |
1110 _dirty_revs.push_back(u); |
1097 for (u = new_stem; _thread[u] != stem; u = _thread[u]) ; |
1111 |
1098 _thread[u] = right; |
1112 // Remove the subtree of stem from the thread list |
1099 |
1113 w = _rev_thread[stem]; |
1100 // Change the parent node of stem and shift stem and par_stem nodes |
1114 _thread[w] = right; |
1101 _parent[stem] = par_stem; |
1115 _rev_thread[right] = w; |
|
1116 |
|
1117 // Change the parent node and shift stem nodes |
|
1118 _parent[stem] = par_stem; |
1102 par_stem = stem; |
1119 par_stem = stem; |
1103 stem = new_stem; |
1120 stem = new_stem; |
1104 |
1121 |
1105 // Find the last successor of stem (u) and the node after it (right) |
1122 // Update u and right nodes |
1106 // according to the thread index |
1123 u = _last_succ[stem] == _last_succ[par_stem] ? |
1107 for (u = stem; _depth[_thread[u]] > _depth[stem]; u = _thread[u]) ; |
1124 _rev_thread[par_stem] : _last_succ[stem]; |
1108 right = _thread[u]; |
1125 right = _thread[u]; |
1109 } |
1126 } |
1110 _parent[u_out] = par_stem; |
1127 _parent[u_out] = par_stem; |
|
1128 _last_succ[u_out] = u; |
1111 _thread[u] = last; |
1129 _thread[u] = last; |
1112 |
1130 _rev_thread[last] = u; |
1113 if (join == v_out && par_first) { |
1131 |
1114 if (first != v_in) _thread[first] = right; |
1132 // Remove the subtree of u_out from the thread list except for |
|
1133 // the case when old_rev_thread equals to v_in |
|
1134 // (it also means that join and v_out coincide) |
|
1135 if (old_rev_thread != v_in) { |
|
1136 _thread[old_rev_thread] = right; |
|
1137 _rev_thread[right] = old_rev_thread; |
|
1138 } |
|
1139 |
|
1140 // Update _rev_thread using the new _thread values |
|
1141 for (int i = 0; i < int(_dirty_revs.size()); ++i) { |
|
1142 u = _dirty_revs[i]; |
|
1143 _rev_thread[_thread[u]] = u; |
|
1144 } |
|
1145 |
|
1146 // Update _last_succ for the stem nodes from u_out to u_in |
|
1147 for (u = u_out; u != u_in; u = _parent[u]) { |
|
1148 _last_succ[_parent[u]] = _last_succ[u]; |
|
1149 } |
|
1150 |
|
1151 // Set limits for updating _last_succ form v_in and v_out |
|
1152 // towards the root |
|
1153 int up_limit_in = -1; |
|
1154 int up_limit_out = -1; |
|
1155 if (_last_succ[join] == v_in) { |
|
1156 up_limit_out = join; |
1115 } else { |
1157 } else { |
1116 for (u = v_out; _thread[u] != u_out; u = _thread[u]) ; |
1158 up_limit_in = join; |
1117 _thread[u] = right; |
1159 } |
1118 } |
1160 |
1119 } |
1161 // Update _last_succ from v_in towards the root |
1120 |
1162 for (u = v_in; u != up_limit_in && _last_succ[u] == v_in; |
1121 // Update _pred and _forward vectors |
1163 u = _parent[u]) { |
1122 void updatePredEdge() { |
1164 _last_succ[u] = _last_succ[u_out]; |
1123 int u = u_out, v; |
1165 } |
|
1166 // Update _last_succ from v_out towards the root |
|
1167 if (join != old_rev_thread && v_in != old_rev_thread) { |
|
1168 for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
|
1169 u = _parent[u]) { |
|
1170 _last_succ[u] = old_rev_thread; |
|
1171 } |
|
1172 } else { |
|
1173 for (u = v_out; u != up_limit_out && _last_succ[u] == old_last_succ; |
|
1174 u = _parent[u]) { |
|
1175 _last_succ[u] = _last_succ[u_out]; |
|
1176 } |
|
1177 } |
|
1178 |
|
1179 // Update _pred and _forward for the stem nodes from u_out to u_in |
|
1180 u = u_out; |
1124 while (u != u_in) { |
1181 while (u != u_in) { |
1125 v = _parent[u]; |
1182 w = _parent[u]; |
1126 _pred[u] = _pred[v]; |
1183 _pred[u] = _pred[w]; |
1127 _forward[u] = !_forward[v]; |
1184 _forward[u] = !_forward[w]; |
1128 u = v; |
1185 u = w; |
1129 } |
1186 } |
1130 _pred[u_in] = _in_edge; |
1187 _pred[u_in] = _in_edge; |
1131 _forward[u_in] = (u_in == _source[_in_edge]); |
1188 _forward[u_in] = (u_in == _source[_in_edge]); |
1132 } |
1189 |
1133 |
1190 // Update _succ_num from v_in to join |
1134 // Update _depth and _potential vectors |
1191 for (u = v_in; u != join; u = _parent[u]) { |
1135 void updateDepthPotential() { |
1192 _succ_num[u] += old_succ_num; |
1136 _depth[u_in] = _depth[v_in] + 1; |
1193 } |
|
1194 // Update _succ_num from v_out to join |
|
1195 for (u = v_out; u != join; u = _parent[u]) { |
|
1196 _succ_num[u] -= old_succ_num; |
|
1197 } |
|
1198 // Update _succ_num for the stem nodes from u_out to u_in |
|
1199 int tmp = 0; |
|
1200 u = u_out; |
|
1201 while (u != u_in) { |
|
1202 w = _parent[u]; |
|
1203 tmp = _succ_num[u] - _succ_num[w] + tmp; |
|
1204 _succ_num[u] = tmp; |
|
1205 u = w; |
|
1206 } |
|
1207 _succ_num[u_in] = old_succ_num; |
|
1208 } |
|
1209 |
|
1210 // Update potentials |
|
1211 void updatePotential() { |
1137 Cost sigma = _forward[u_in] ? |
1212 Cost sigma = _forward[u_in] ? |
1138 _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] : |
1213 _pi[v_in] - _pi[u_in] - _cost[_pred[u_in]] : |
1139 _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]]; |
1214 _pi[v_in] - _pi[u_in] + _cost[_pred[u_in]]; |
1140 _pi[u_in] += sigma; |
1215 // Update in the lower subtree (which has been moved) |
1141 for(int u = _thread[u_in]; _parent[u] != -1; u = _thread[u]) { |
1216 int end = _thread[_last_succ[u_in]]; |
1142 _depth[u] = _depth[_parent[u]] + 1; |
1217 for (int u = u_in; u != end; u = _thread[u]) { |
1143 if (_depth[u] <= _depth[u_in]) break; |
|
1144 _pi[u] += sigma; |
1218 _pi[u] += sigma; |
1145 } |
1219 } |
1146 } |
1220 } |
1147 |
1221 |
1148 // Execute the algorithm |
1222 // Execute the algorithm |