src/demo/lp_maxflow_demo.cc
author deba
Tue, 19 Apr 2005 13:33:44 +0000
changeset 1374 bcfa3980b432
child 1381 998e8def9676
permissions -rw-r--r--
Call the default constructor of the ObserverBase.
alpar@1361
     1
#include<lemon/lp_glpk.h>
alpar@1361
     2
#include<lemon/graph_reader.h>
alpar@1361
     3
#include<lemon/list_graph.h>
alpar@1361
     4
alpar@1361
     5
using namespace lemon;
alpar@1361
     6
alpar@1361
     7
template<class G,class C>
alpar@1361
     8
double maxFlow(const G &g,const C &cap,typename G::Node s,typename G::Node t)
alpar@1361
     9
{
alpar@1361
    10
  LpGlpk lp;
alpar@1361
    11
  
alpar@1361
    12
  typedef G Graph;
alpar@1361
    13
  typedef typename G::Node Node;
alpar@1361
    14
  typedef typename G::NodeIt NodeIt;
alpar@1361
    15
  typedef typename G::Edge Edge;
alpar@1361
    16
  typedef typename G::EdgeIt EdgeIt;
alpar@1361
    17
  typedef typename G::OutEdgeIt OutEdgeIt;
alpar@1361
    18
  typedef typename G::InEdgeIt InEdgeIt;
alpar@1361
    19
  
alpar@1361
    20
  typename G::template EdgeMap<LpGlpk::Col> x(g);
alpar@1361
    21
  lp.addColSet(x);
alpar@1361
    22
  
alpar@1361
    23
  for(EdgeIt e(g);e!=INVALID;++e) {
alpar@1361
    24
    lp.colUpperBound(x[e],cap[e]);
alpar@1361
    25
    lp.colLowerBound(x[e],0);
alpar@1361
    26
  }
alpar@1361
    27
alpar@1361
    28
  for(NodeIt n(g);n!=INVALID;++n) if(n!=s&&n!=t) {
alpar@1361
    29
    LpGlpk::Expr ex;
alpar@1361
    30
    for(InEdgeIt  e(g,n);e!=INVALID;++e) ex+=x[e];
alpar@1361
    31
    for(OutEdgeIt e(g,n);e!=INVALID;++e) ex-=x[e];
alpar@1361
    32
    lp.addRow(ex==0);
alpar@1361
    33
  }
alpar@1361
    34
  {
alpar@1361
    35
    LpGlpk::Expr ex;
alpar@1361
    36
    for(InEdgeIt  e(g,t);e!=INVALID;++e) ex+=x[e];
alpar@1361
    37
    for(OutEdgeIt e(g,t);e!=INVALID;++e) ex-=x[e];
alpar@1361
    38
    lp.setObj(ex);
alpar@1361
    39
  }
alpar@1361
    40
  lp.max();
alpar@1361
    41
alpar@1361
    42
  lp.presolver(true);
alpar@1361
    43
  
alpar@1361
    44
  lp.messageLevel(3);
alpar@1361
    45
alpar@1361
    46
  lp.solve();
alpar@1361
    47
alpar@1361
    48
  return lp.primalValue();
alpar@1361
    49
}
alpar@1361
    50
alpar@1361
    51
int main() 
alpar@1361
    52
{
alpar@1361
    53
  ListGraph g;
alpar@1361
    54
  ListGraph::Node s;
alpar@1361
    55
  ListGraph::Node t;
alpar@1361
    56
  
alpar@1361
    57
  ListGraph::EdgeMap<double> cap(g);
alpar@1361
    58
  
alpar@1361
    59
  GraphReader<ListGraph> reader(std::cin,g);
alpar@1361
    60
  reader.addNode("source",s).addNode("target",t)
alpar@1361
    61
    .addEdgeMap("capacity",cap).run();
alpar@1361
    62
  
alpar@1361
    63
  // std::ifstream file("../test/preflow_");
alpar@1361
    64
//   readDimacs(file, g, cap, s, t);
alpar@1361
    65
alpar@1361
    66
  std::cout << "Max flow value = " << maxFlow(g,cap,s,t) << std::endl;
alpar@1361
    67
alpar@1361
    68
}