/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2008
* 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
/// \ingroup dimacs_group
/// \brief DIMACS file format reader.
///@defgroup dimacs_group DIMACS format
///\brief Read and write files in DIMACS format
///Tools to read a digraph from or write it to a file in DIMACS format
/// \addtogroup dimacs_group
/// DIMACS min cost flow reader function.
/// This function reads a min cost flow instance from DIMACS format,
/// i.e. from DIMACS files having a line starting with
/// At the beginning \c g is cleared by \c g.clear(). The supply
/// amount of the nodes are written to \c supply (signed). The
/// lower bounds, capacities and costs of the arcs are written to
/// \c lower, \c capacity and \c cost.
template <typename Digraph, typename LowerMap,
typename CapacityMap, typename CostMap,
void readDimacs( std::istream& is,
std::vector<typename Digraph::Node> nodes;
std::string problem, str;
typename SupplyMap::Value sup;
typename CapacityMap::Value low;
typename CapacityMap::Value cap;
typename CostMap::Value co;
case 'c':
// comment line
case 'p':
// problem definition line
if (problem != "min") return;
for (int k = 1; k <= n; ++k) {
case 'n':
// node definition line
supply.set(nodes[i], sup);
case 'a':
// arc (arc) definition line
is >> i >> j >> low >> cap >> co;
e = g.addArc(nodes[i], nodes[j]);
/// DIMACS max flow reader function.
/// This function reads a max flow instance from DIMACS format,
/// i.e. from DIMACS files having a line starting with
/// At the beginning \c g is cleared by \c g.clear(). The arc
/// capacities are written to \c capacity and \c s and \c t are
/// set to the source and the target nodes.
template<typename Digraph, typename CapacityMap>
void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity,
typename Digraph::Node &s, typename Digraph::Node &t) {
std::vector<typename Digraph::Node> nodes;
typename CapacityMap::Value _cap;
case 'c':
// comment line
case 'p':
// problem definition line
for (int k = 1; k <= n; ++k)
case 'n':
// node definition line
if (problem == "sp") { // shortest path problem
if (problem == "max") { // max flow problem
if (d == 's') s = nodes[i];
if (d == 't') t = nodes[i];
case 'a':
// arc (arc) definition line
if (problem == "max" || problem == "sp") {
e = g.addArc(nodes[i], nodes[j]);
g.addArc(nodes[i], nodes[j]);
/// DIMACS shortest path reader function.
/// This function reads a shortest path instance from DIMACS format,
/// i.e. from DIMACS files having a line starting with
/// At the beginning \c g is cleared by \c g.clear(). The arc
/// capacities are written to \c capacity and \c s is set to the
template<typename Digraph, typename CapacityMap>
void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity,
typename Digraph::Node &s) {
readDimacs(is, g, capacity, s, s);
/// DIMACS capacitated digraph reader function.
/// This function reads an arc capacitated digraph instance from
/// DIMACS format. At the beginning \c g is cleared by \c g.clear()
/// and the arc capacities are written to \c capacity.
template<typename Digraph, typename CapacityMap>
void readDimacs(std::istream& is, Digraph &g, CapacityMap& capacity) {
typename Digraph::Node u;
readDimacs(is, g, capacity, u, u);
/// DIMACS plain digraph reader function.
/// This function reads a digraph without any designated nodes and
/// maps from DIMACS format, i.e. from DIMACS files having a line
/// At the beginning \c g is cleared by \c g.clear().
template<typename Digraph>
void readDimacs(std::istream& is, Digraph &g) {
typename Digraph::Node u;
NullMap<typename Digraph::Arc, int> n;
readDimacs(is, g, n, u, u);
/// DIMACS plain digraph writer function.
/// This function writes a digraph without any designated nodes and
/// maps into DIMACS format, i.e. into DIMACS file having a line
template<typename Digraph>
void writeDimacs(std::ostream& os, const Digraph &g) {
typedef typename Digraph::NodeIt NodeIt;
typedef typename Digraph::ArcIt ArcIt;
os << "c matching problem" << std::endl;
os << "p mat " << g.nodeNum() << " " << g.arcNum() << std::endl;
typename Digraph::template NodeMap<int> nodes(g);
for(NodeIt v(g); v != INVALID; ++v) {
for(ArcIt e(g); e != INVALID; ++e) {
os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)]