$title Min-Cost Network Flow (Williams, p.82) option limrow=0, limcol=0, solprint=off; set node /0*7/; parameter supply(node) /0 10, 1 15, 5 -9, 6 -10, 7 -6/; parameter cost(node,node) / 0.2 5 1.3 4 2.3 2 2.4 6 2.5 5 3.4 1 3.7 2 4.2 4 4.5 6 4.6 3 7.6 4 /; alias (node,i,j,k); abort$(smin((i,j), cost(i,j)) lt 0) "bad costs given"; abort$(sum(i, supply(i)) ne 0) "supply must equal demand"; * define a dynamic set that indicates the "legal" arcs set arc(node,node); arc(i,j) = yes$(cost(i,j) gt 0); positive variable f(node,node); variable totalcost; equations balance(node), objective; balance(i).. sum(k$arc(i,k), f(i,k)) - sum(j$arc(j,i), f(j,i)) =e= supply(i); objective.. totalcost =e= sum(arc, cost(arc)*f(arc)); model mcf /balance, objective/; solve mcf using lp minimizing totalcost; option f:0:0:2; display f.l;