Backport relevant parts of bugfixes [ad22262328b3], [61fdd06833a6] and [4add05447ca0] to branch 1.2 (#623)
1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2009
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
21 ///\brief DIMACS to LGF converter.
23 /// This program converts various DIMACS formats to the LEMON Digraph Format
28 /// dimacs-to-lgf --help
30 /// for more info on the usage.
36 #include <lemon/smart_graph.h>
37 #include <lemon/dimacs.h>
38 #include <lemon/lgf_writer.h>
40 #include <lemon/arg_parser.h>
41 #include <lemon/error.h>
44 using namespace lemon;
47 int main(int argc, const char *argv[]) {
48 typedef SmartDigraph Digraph;
50 typedef Digraph::Arc Arc;
51 typedef Digraph::Node Node;
52 typedef Digraph::ArcIt ArcIt;
53 typedef Digraph::NodeIt NodeIt;
54 typedef Digraph::ArcMap<double> DoubleArcMap;
55 typedef Digraph::NodeMap<double> DoubleNodeMap;
57 std::string inputName;
58 std::string outputName;
60 ArgParser ap(argc, argv);
61 ap.other("[INFILE [OUTFILE]]",
62 "If either the INFILE or OUTFILE file is missing the standard\n"
63 " input/output will be used instead.")
69 switch(ap.files().size())
72 output.open(ap.files()[1].c_str());
74 throw IoError("Cannot open the file for writing", ap.files()[1]);
78 input.open(ap.files()[0].c_str());
80 throw IoError("File cannot be found", ap.files()[0]);
86 cerr << ap.commandName() << ": too many arguments\n";
89 istream& is = (ap.files().size()<1 ? cin : input);
90 ostream& os = (ap.files().size()<2 ? cout : output);
92 DimacsDescriptor desc = dimacsType(is);
95 case DimacsDescriptor::MIN:
98 DoubleArcMap lower(digraph), capacity(digraph), cost(digraph);
99 DoubleNodeMap supply(digraph);
100 readDimacsMin(is, digraph, lower, capacity, cost, supply, 0, desc);
101 DigraphWriter<Digraph>(digraph, os).
102 nodeMap("supply", supply).
103 arcMap("lower", lower).
104 arcMap("capacity", capacity).
105 arcMap("cost", cost).
106 attribute("problem","min").
110 case DimacsDescriptor::MAX:
114 DoubleArcMap capacity(digraph);
115 readDimacsMax(is, digraph, capacity, s, t, 0, desc);
116 DigraphWriter<Digraph>(digraph, os).
117 arcMap("capacity", capacity).
120 attribute("problem","max").
124 case DimacsDescriptor::SP:
128 DoubleArcMap capacity(digraph);
129 readDimacsSp(is, digraph, capacity, s, desc);
130 DigraphWriter<Digraph>(digraph, os).
131 arcMap("capacity", capacity).
133 attribute("problem","sp").
137 case DimacsDescriptor::MAT:
140 readDimacsMat(is, digraph,desc);
141 DigraphWriter<Digraph>(digraph, os).
142 attribute("problem","mat").