alpar@385: /* -*- mode: C++; indent-tabs-mode: nil; -*- alpar@385: * alpar@385: * This file is a part of LEMON, a generic C++ optimization library. alpar@385: * alpar@385: * Copyright (C) 2003-2008 alpar@385: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@385: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@385: * alpar@385: * Permission to use, modify and distribute this software is granted alpar@385: * provided that this copyright notice appears in all copies. For alpar@385: * precise terms see the accompanying LICENSE file. alpar@385: * alpar@385: * This software is provided "AS IS" with no warranty of any kind, alpar@385: * express or implied, and with no claim as to its suitability for any alpar@385: * purpose. alpar@385: * alpar@385: */ alpar@385: alpar@385: ///\ingroup tools alpar@385: ///\file alpar@385: ///\brief DIMACS to LGF converter. alpar@385: /// alpar@385: /// This program converts various DIMACS formats to the LEMON Digraph Format alpar@385: /// (LGF). alpar@385: /// alpar@385: /// See alpar@385: /// \verbatim alpar@385: /// dimacs-to-lgf --help alpar@385: /// \endverbatim alpar@385: /// for more info on usage. alpar@385: /// alpar@385: alpar@385: #include alpar@385: #include alpar@385: #include alpar@385: alpar@385: #include alpar@385: #include alpar@385: #include alpar@385: alpar@385: #include alpar@385: alpar@385: using namespace std; alpar@385: using namespace lemon; alpar@385: alpar@385: alpar@385: int main(int argc, const char *argv[]) { alpar@385: typedef SmartDigraph Digraph; alpar@385: alpar@385: typedef Digraph::Arc Arc; alpar@385: typedef Digraph::Node Node; alpar@385: typedef Digraph::ArcIt ArcIt; alpar@385: typedef Digraph::NodeIt NodeIt; alpar@385: typedef Digraph::ArcMap DoubleArcMap; alpar@385: typedef Digraph::NodeMap DoubleNodeMap; alpar@385: alpar@385: std::string inputName; alpar@385: std::string outputName; alpar@385: std::string typeName; alpar@385: alpar@385: bool mincostflow; alpar@385: bool maxflow; alpar@385: bool shortestpath; alpar@385: bool capacitated; alpar@385: bool plain; alpar@385: alpar@385: bool version; alpar@385: alpar@385: ArgParser ap(argc, argv); alpar@385: ap.refOption("-input", alpar@385: "use FILE as input instead of standard input", alpar@385: inputName).synonym("i", "-input") alpar@385: .refOption("-output", alpar@385: "use FILE as output instead of standard output", alpar@385: outputName).synonym("o", "-output") alpar@385: .refOption("-mincostflow", alpar@385: "set the type of the digraph to \"mincostflow\" digraph", alpar@385: mincostflow) alpar@385: .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow") alpar@385: .refOption("-maxflow", alpar@385: "set the type of the digraph to \"maxflow\" digraph", alpar@385: maxflow) alpar@385: .optionGroup("type", "-maxflow").synonym("mf", "-maxflow") alpar@385: .refOption("-shortestpath", alpar@385: "set the type of the digraph to \"shortestpath\" digraph", alpar@385: shortestpath) alpar@385: .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath") alpar@385: .refOption("-capacitated", alpar@385: "set the type of the digraph to \"capacitated\" digraph", alpar@385: capacitated) alpar@385: .optionGroup("type", "-capacitated").synonym("cap", "-capacitated") alpar@385: .refOption("-plain", alpar@385: "set the type of the digraph to \"plain\" digraph", alpar@385: plain) alpar@385: .optionGroup("type", "-plain").synonym("pl", "-plain") alpar@385: .onlyOneGroup("type") alpar@385: .mandatoryGroup("type") alpar@385: .refOption("-version", "show version information", version) alpar@385: .synonym("v", "-version") alpar@385: .run(); alpar@385: alpar@385: ifstream input; alpar@385: if (!inputName.empty()) { alpar@385: input.open(inputName.c_str()); alpar@385: if (!input) { alpar@385: cerr << "File open error" << endl; alpar@385: return -1; alpar@385: } alpar@385: } alpar@385: istream& is = (inputName.empty() ? cin : input); alpar@385: alpar@385: ofstream output; alpar@385: if (!outputName.empty()) { alpar@385: output.open(outputName.c_str()); alpar@385: if (!output) { alpar@385: cerr << "File open error" << endl; alpar@385: return -1; alpar@385: } alpar@385: } alpar@385: ostream& os = (outputName.empty() ? cout : output); alpar@385: alpar@385: if (mincostflow) { alpar@385: Digraph digraph; alpar@385: DoubleArcMap lower(digraph), capacity(digraph), cost(digraph); alpar@385: DoubleNodeMap supply(digraph); alpar@386: readDimacsMin(is, digraph, lower, capacity, cost, supply); alpar@385: DigraphWriter(digraph, os). alpar@385: nodeMap("supply", supply). alpar@385: arcMap("lower", lower). alpar@385: arcMap("capacity", capacity). alpar@385: arcMap("cost", cost). alpar@385: run(); alpar@385: } else if (maxflow) { alpar@385: Digraph digraph; alpar@385: Node s, t; alpar@385: DoubleArcMap capacity(digraph); alpar@386: readDimacsMax(is, digraph, capacity, s, t); alpar@385: DigraphWriter(digraph, os). alpar@385: arcMap("capacity", capacity). alpar@385: node("source", s). alpar@385: node("target", t). alpar@385: run(); alpar@385: } else if (shortestpath) { alpar@385: Digraph digraph; alpar@385: Node s; alpar@385: DoubleArcMap capacity(digraph); alpar@386: readDimacsSp(is, digraph, capacity, s); alpar@385: DigraphWriter(digraph, os). alpar@385: arcMap("capacity", capacity). alpar@385: node("source", s). alpar@385: run(); alpar@385: } else if (capacitated) { alpar@385: Digraph digraph; alpar@385: DoubleArcMap capacity(digraph); alpar@386: readDimacsMax(is, digraph, capacity); alpar@385: DigraphWriter(digraph, os). alpar@385: arcMap("capacity", capacity). alpar@385: run(); alpar@385: } else if (plain) { alpar@385: Digraph digraph; alpar@386: readDimacsMat(is, digraph); alpar@385: DigraphWriter(digraph, os).run(); alpar@385: } else { alpar@385: cerr << "Invalid type error" << endl; alpar@385: return -1; alpar@385: } alpar@385: return 0; alpar@385: }