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_wrapper_3.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> lp_flow(lp, edge_index_map);
55 for (Graph::EdgeIt e(g); e!=INVALID; ++e) {
56 ColIt col_it=lp.addCol();
57 edge_index_map.set(e, col_it);
59 lp.setColBounds(col_it, LPSolver::FIXED, 0, cap[e]);
61 lp.setColBounds(col_it, LPSolver::DOUBLE, 0, cap[e]);
64 for (Graph::NodeIt n(g); n!=INVALID; ++n) {
65 LPSolver::Expression expr;
66 for (Graph::OutEdgeIt e(g, n); e!=INVALID; ++e)
67 expr+=edge_index_map[e];
68 for (Graph::InEdgeIt e(g, n); e!=INVALID; ++e)
69 expr-=edge_index_map[e];
72 lp.setObjCoeffs(expr);
75 if ((n!=s) && (n!=t)) {
76 RowIt row_it=lp.addRow();
77 lp.setRowCoeffs(row_it, expr);
78 lp.setRowBounds(row_it, LPSolver::FIXED, 0.0, 0.0);
82 //std::cout << lp.colNum() << std::endl;
83 //std::cout << lp.rowNum() << std::endl;
84 //std::cout << "flow value: "<< lp.getObjVal() << std::endl;
85 for (Graph::EdgeIt e(g); e!=INVALID; ++e)
86 flow.set(e, lp_flow[e]);