COIN-OR::LEMON - Graph Library

source: lemon-0.x/src/demo/lp_maxflow_demo.cc @ 1387:37d1b20cd9ef

Last change on this file since 1387:37d1b20cd9ef was 1387:37d1b20cd9ef, checked in by Akos Ladanyi, 15 years ago
  • include config.h instead of manually adding -DHAVE_{CPLEX,GLPK} to CXXFLAGS
  • removed the checking of LpSkeleton? from lp_test.cc, because it is an abstract base class now
  • removed unnecessary LDFLAGS when linking with libemon.la
File size: 1.7 KB
RevLine 
[1387]1#ifdef HAVE_CONFIG_H
2#include <config.h>
3#endif
4
[1361]5#include<lemon/graph_reader.h>
6#include<lemon/list_graph.h>
7
[1381]8
9#ifdef HAVE_GLPK
10#include <lemon/lp_glpk.h>
11#elif HAVE_CPLEX
12#include <lemon/lp_cplex.h>
13#endif
14
[1361]15using namespace lemon;
16
[1381]17#ifdef HAVE_GLPK
18typedef LpGlpk LpDefault;
19#elif HAVE_CPLEX
20typedef LpCplex LpDefault;
21#endif
22
23
[1361]24template<class G,class C>
25double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
26{
[1381]27  LpDefault lp;
[1361]28 
29  typedef G Graph;
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;
36 
[1381]37  typename G::template EdgeMap<LpDefault::Col> x(g);
[1361]38  lp.addColSet(x);
39 
40  for(EdgeIt e(g);e!=INVALID;++e) {
41    lp.colUpperBound(x[e],cap[e]);
42    lp.colLowerBound(x[e],0);
43  }
44
45  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
[1381]46    LpDefault::Expr ex;
[1361]47    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
48    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
49    lp.addRow(ex==0);
50  }
51  {
[1381]52    LpDefault::Expr ex;
[1361]53    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
54    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
55    lp.setObj(ex);
56  }
57  lp.max();
58
[1381]59#ifdef HAVE_GLPK
[1361]60  lp.presolver(true);
61  lp.messageLevel(3);
[1381]62#endif
[1361]63
64  lp.solve();
65
66  return lp.primalValue();
67}
68
69int main()
70{
71  ListGraph g;
72  ListGraph::Node s;
73  ListGraph::Node t;
74 
75  ListGraph::EdgeMap<double> cap(g);
76 
77  GraphReader<ListGraph> reader(std::cin,g);
78  reader.addNode("source",s).addNode("target",t)
79    .addEdgeMap("capacity",cap).run();
80 
81  // std::ifstream file("../test/preflow_");
82//   readDimacs(file, g, cap, s, t);
83
84  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
85
86}
Note: See TracBrowser for help on using the repository browser.