5 #include <lemon/smart_graph.h>
6 #include <lemon/list_graph.h>
7 #include <lemon/dimacs.h>
8 #include <lemon/time_measure.h>
9 #include <lp_solver_base.h>
13 using namespace lemon;
15 template<typename Edge, typename EdgeIndexMap>
19 EdgeIndexMap* edge_index_map;
21 PrimalMap(LPGLPK& _lp, EdgeIndexMap& _edge_index_map) :
22 lp(&_lp), edge_index_map(&_edge_index_map) { }
23 double operator[](Edge e) const {
24 return lp->getPrimal((*edge_index_map)[e]);
28 // Use a DIMACS max flow file as stdin.
29 // max_flow_expresion < dimacs_max_flow_file
31 int main(int, char **) {
33 typedef ListGraph Graph;
34 typedef Graph::Node Node;
35 typedef Graph::Edge Edge;
36 typedef Graph::EdgeIt EdgeIt;
41 Graph::EdgeMap<int> cap(g);
42 readDimacs(std::cin, g, cap, s, t);
45 typedef LPGLPK LPSolver;
48 typedef LPSolver::ColIt ColIt;
49 typedef LPSolver::RowIt RowIt;
50 typedef Graph::EdgeMap<ColIt> EdgeIndexMap;
51 EdgeIndexMap edge_index_map(g);
52 PrimalMap<Edge, EdgeIndexMap> flow(lp, edge_index_map);
54 // nonnegativity of flow and capacity function
55 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
56 ColIt col_it=lp.addCol();
57 edge_index_map.set(e, col_it);
58 // interesting property in GLPK:
59 // if you change the order of the following two lines, the
60 // two runs of GLPK are extremely different
61 lp.setColLowerBound(col_it, 0);
62 lp.setColUpperBound(col_it, cap[e]);
65 for (Graph::NodeIt n(g); n!=INVALID; ++n) {
66 LPSolver::Expression expr;
67 for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e)
68 expr+=edge_index_map[e];
69 for (Graph::InEdgeIt e(g, n); e!=INVALID; ++e)
70 expr-=edge_index_map[e];
73 lp.setObjCoeffs(expr);
75 // flow conservation constraints
76 if ((n!=s) && (n!=t)) {
77 RowIt row_it=lp.addRow();
78 lp.setRowCoeffs(row_it, expr);
79 lp.setRowLowerBound(row_it, 0.0);
80 lp.setRowUpperBound(row_it, 0.0);
84 cout << "elapsed time: " << ts << endl;