5 #include<lemon/graph_reader.h>
6 #include<lemon/list_graph.h>
10 #include <lemon/lp_glpk.h>
12 #include <lemon/lp_cplex.h>
15 using namespace lemon;
18 typedef LpGlpk LpDefault;
20 typedef LpCplex LpDefault;
24 template<class G,class C>
25 double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
30 typedef typename G::Node Node;
31 typedef typename G::NodeIt NodeIt;
32 typedef typename G::Edge Edge;
33 typedef typename G::EdgeIt EdgeIt;
34 typedef typename G::OutEdgeIt OutEdgeIt;
35 typedef typename G::InEdgeIt InEdgeIt;
37 //Define a map on the edges for the variables of the LP problem
38 typename G::template EdgeMap<LpDefault::Col> x(g);
41 //Nonnegativity and capacity constraints
42 for(EdgeIt e(g);e!=INVALID;++e) {
43 lp.colUpperBound(x[e],cap[e]);
44 lp.colLowerBound(x[e],0);
48 //Flow conservation constraints for the nodes (except for 's' and 't')
49 for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
51 for(InEdgeIt e(g,n);e!=INVALID;++e) ex+=x[e];
52 for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
56 //Objective function: the flow value entering 't'
59 for(InEdgeIt e(g,t);e!=INVALID;++e) ex+=x[e];
60 for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
72 //Solve with the underlying solver
75 return lp.primalValue();
84 ListGraph::EdgeMap<double> cap(g);
86 GraphReader<ListGraph> reader(std::cin,g);
87 reader.readNode("source",s).readNode("target",t)
88 .readEdgeMap("capacity",cap).run();
90 // std::ifstream file("../test/preflow_");
91 // readDimacs(file, g, cap, s, t);
93 std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;