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