| 1 | // -*- c++ -*- |
|---|
| 2 | #include <iostream> |
|---|
| 3 | #include <fstream> |
|---|
| 4 | |
|---|
| 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> |
|---|
| 10 | |
|---|
| 11 | using std::cout; |
|---|
| 12 | using std::endl; |
|---|
| 13 | using namespace lemon; |
|---|
| 14 | |
|---|
| 15 | template<typename Edge, typename EdgeIndexMap> |
|---|
| 16 | class PrimalMap { |
|---|
| 17 | protected: |
|---|
| 18 | LPGLPK* lp; |
|---|
| 19 | EdgeIndexMap* edge_index_map; |
|---|
| 20 | public: |
|---|
| 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]); |
|---|
| 25 | } |
|---|
| 26 | }; |
|---|
| 27 | |
|---|
| 28 | // Use a DIMACS max flow file as stdin. |
|---|
| 29 | // max_flow_expresion < dimacs_max_flow_file |
|---|
| 30 | |
|---|
| 31 | int main(int, char **) { |
|---|
| 32 | |
|---|
| 33 | typedef ListGraph Graph; |
|---|
| 34 | typedef Graph::Node Node; |
|---|
| 35 | typedef Graph::Edge Edge; |
|---|
| 36 | typedef Graph::EdgeIt EdgeIt; |
|---|
| 37 | |
|---|
| 38 | Graph g; |
|---|
| 39 | |
|---|
| 40 | Node s, t; |
|---|
| 41 | Graph::EdgeMap<int> cap(g); |
|---|
| 42 | readDimacs(std::cin, g, cap, s, t); |
|---|
| 43 | Timer ts; |
|---|
| 44 | |
|---|
| 45 | typedef LPGLPK LPSolver; |
|---|
| 46 | LPSolver lp; |
|---|
| 47 | lp.setMaximize(); |
|---|
| 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); |
|---|
| 53 | |
|---|
| 54 | // 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.setColUpperBound(col_it, cap[e]); |
|---|
| 62 | lp.setColLowerBound(col_it, 0); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 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]; |
|---|
| 71 | // cost function |
|---|
| 72 | if (n==s) { |
|---|
| 73 | lp.setObjCoeffs(expr); |
|---|
| 74 | } |
|---|
| 75 | // flow conservation |
|---|
| 76 | if ((n!=s) && (n!=t)) { |
|---|
| 77 | RowIt row_it=lp.addRow(); |
|---|
| 78 | lp.setRowCoeffs(row_it, expr); |
|---|
| 79 | lp.setRowBounds(row_it, LPSolver::FIXED, 0.0, 0.0); |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | lp.solveSimplex(); |
|---|
| 83 | cout << "elapsed time: " << ts << endl; |
|---|
| 84 | } |
|---|