C++ code multistage network flow allocation problem