/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2009
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
* (Egervary Research Group on Combinatorial Optimization, EGRES).
* Permission to use, modify and distribute this software is granted
* provided that this copyright notice appears in all copies. For
* precise terms see the accompanying LICENSE file.
* This software is provided "AS IS" with no warranty of any kind,
* express or implied, and with no claim as to its suitability for any
///\brief DIMACS problem solver.
/// This program solves various problems given in DIMACS format.
/// for more info on usage.
#include <lemon/smart_graph.h>
#include <lemon/dimacs.h>
#include <lemon/lgf_writer.h>
#include <lemon/time_measure.h>
#include <lemon/arg_parser.h>
#include <lemon/dijkstra.h>
#include <lemon/preflow.h>
#include <lemon/matching.h>
#include <lemon/network_simplex.h>
typedef SmartDigraph Digraph;
DIGRAPH_TYPEDEFS(Digraph);
typedef SmartGraph Graph;
void solve_sp(ArgParser &ap, std::istream &is, std::ostream &,
bool report = !ap.given("q");
Digraph::ArcMap<Value> len(g);
readDimacsSp(is, g, len, s, desc);
if(report) std::cerr << "Read the file: " << t << '\n';
Dijkstra<Digraph, Digraph::ArcMap<Value> > dij(g,len);
if(report) std::cerr << "Setup Dijkstra class: " << t << '\n';
if(report) std::cerr << "Run Dijkstra: " << t << '\n';
void solve_max(ArgParser &ap, std::istream &is, std::ostream &,
Value infty, DimacsDescriptor &desc)
bool report = !ap.given("q");
Digraph::ArcMap<Value> cap(g);
readDimacsMax(is, g, cap, s, t, infty, desc);
if(report) std::cerr << "Read the file: " << ti << '\n';
Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t);
if(report) std::cerr << "Setup Preflow class: " << ti << '\n';
if(report) std::cerr << "Run Preflow: " << ti << '\n';
if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n';
template<class Value, class LargeValue>
void solve_min(ArgParser &ap, std::istream &is, std::ostream &,
Value infty, DimacsDescriptor &desc)
bool report = !ap.given("q");
Digraph::ArcMap<Value> lower(g), cap(g), cost(g);
Digraph::NodeMap<Value> sup(g);
readDimacsMin(is, g, lower, cap, cost, sup, infty, desc);
for (Digraph::NodeIt n(g); n != INVALID; ++n) {
std::cerr << "Sum of supply values: " << sum_sup << "\n";
std::cerr << "GEQ supply contraints are used for NetworkSimplex\n\n";
std::cerr << "LEQ supply contraints are used for NetworkSimplex\n\n";
if (report) std::cerr << "Read the file: " << ti << '\n';
NetworkSimplex<Digraph, Value> ns(g);
ns.lowerMap(lower).upperMap(cap).costMap(cost).supplyMap(sup);
if (sum_sup > 0) ns.supplyType(ns.LEQ);
if (report) std::cerr << "Setup NetworkSimplex class: " << ti << '\n';
std::cerr << "Run NetworkSimplex: " << ti << "\n\n";
std::cerr << "Feasible flow: " << (res ? "found" : "not found") << '\n';
if (res) std::cerr << "Min flow cost: "
<< ns.template totalCost<LargeValue>() << '\n';
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &,
bool report = !ap.given("q");
readDimacsMat(is, g, desc);
if(report) std::cerr << "Read the file: " << ti << '\n';
MaxMatching<Graph> mat(g);
if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n';
if(report) std::cerr << "Run MaxMatching: " << ti << '\n';
if(report) std::cerr << "\nCardinality of max matching: "
<< mat.matchingSize() << '\n';
template<class Value, class LargeValue>
void solve(ArgParser &ap, std::istream &is, std::ostream &os,
std::stringstream iss(static_cast<std::string>(ap["infcap"]));
std::cerr << "Cannot interpret '"
<< static_cast<std::string>(ap["infcap"]) << "' as infinite"
case DimacsDescriptor:
:MIN:
solve_min<Value, LargeValue>(ap,is,os,infty,desc);
case DimacsDescriptor:
:MAX:
solve_max<Value>(ap,is,os,infty,desc);
case DimacsDescriptor:
:SP:
solve_sp<Value>(ap,is,os,desc);
case DimacsDescriptor:
:MAT:
solve_mat(ap,is,os,desc);
int main(int argc, const char *argv[]) {
typedef SmartDigraph Digraph;
typedef Digraph::Arc Arc;
ArgParser ap(argc, argv);
ap.other("[INFILE [OUTFILE]]",
"If either the INFILE or OUTFILE file is missing the standard\n"
" input/output will be used instead.")
.boolOption("q", "Do not print any report")
.boolOption("int","Use 'int' for capacities, costs etc. (default)")
.optionGroup("datatype","int")
#ifdef LEMON_HAVE_LONG_LONG
.boolOption("long","Use 'long long' for capacities, costs etc.")
.optionGroup("datatype","long")
.boolOption("double","Use 'double' for capacities, costs etc.")
.optionGroup("datatype","double")
.boolOption("ldouble","Use 'long double' for capacities, costs etc.")
.optionGroup("datatype","ldouble")
.onlyOneGroup("datatype")
.stringOption("infcap","Value used for 'very high' capacities","0")
switch(ap.files().size())
output.open(ap.files()[1].c_str());
throw IoError("Cannot open the file for writing", ap.files()[1]);
input.open(ap.files()[0].c_str());
throw IoError("File cannot be found", ap.files()[0]);
std::cerr << ap.commandName() << ": too many arguments\n";
std::istream& is = (ap.files().size()<1 ? std::cin : input);
std::ostream& os = (ap.files().size()<2 ? std::cout : output);
DimacsDescriptor desc = dimacsType(is);
std::cout << "Problem type: ";
case DimacsDescriptor:
:MIN:
case DimacsDescriptor:
:MAX:
case DimacsDescriptor:
:SP:
case DimacsDescriptor:
:MAT:
std::cout << "\nNum of nodes: " << desc.nodeNum;
std::cout << "\nNum of arcs: " << desc.edgeNum;
solve<double, double>(ap,is,os,desc);
else if(ap.given("ldouble"))
solve<long double, long double>(ap,is,os,desc);
#ifdef LEMON_HAVE_LONG_LONG
else if(ap.given("long"))
solve<long long, long long>(ap,is,os,desc);
else solve<int, long long>(ap,is,os,desc);
else solve<int, long>(ap,is,os,desc);