alpar@2404: /* -*- C++ -*- alpar@2404: * alpar@2404: * This file is a part of LEMON, a generic C++ optimization library alpar@2404: * alpar@2404: * Copyright (C) 2003-2007 alpar@2404: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@2404: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@2404: * alpar@2404: * Permission to use, modify and distribute this software is granted alpar@2404: * provided that this copyright notice appears in all copies. For alpar@2404: * precise terms see the accompanying LICENSE file. alpar@2404: * alpar@2404: * This software is provided "AS IS" with no warranty of any kind, alpar@2404: * express or implied, and with no claim as to its suitability for any alpar@2404: * purpose. alpar@2404: * alpar@2404: */ alpar@2404: deba@2491: ///\ingroup tools alpar@2404: ///\file deba@2410: ///\brief DIMACS to LGF converter. alpar@2404: /// alpar@2404: /// This program converts various DIMACS formats to the LEMON Graph Format alpar@2404: /// (LGF). alpar@2404: /// deba@2491: ///\verbatim deba@2491: ///Usage: deba@2491: /// ./tools/dim_to_lgf deba@2491: /// --mincostflow|-mcf|--maxflow|-mf|--shortestpath|-sp|--capacitated|-cap|--plain|-pl deba@2491: /// [--help|-h|-help] [--input|-i str] [--output|-o str] [--version|-v] deba@2491: ///Where: deba@2491: /// --capacitated|-cap deba@2491: /// set the type of the graph to "capacitated" graph deba@2491: /// --help|-h|-help deba@2491: /// Print a short help message deba@2491: /// --input|-i str deba@2491: /// use FILE as input instead of standard input deba@2491: /// --maxflow|-mf deba@2491: /// set the type of the graph to "maxflow" graph deba@2491: /// --mincostflow|-mcf deba@2491: /// set the type of the graph to "mincostflow" graph deba@2491: /// --output|-o str deba@2491: /// use FILE as output instead of standard output deba@2491: /// --plain|-pl deba@2491: /// set the type of the graph to "plain" graph deba@2491: /// --shortestpath|-sp deba@2491: /// set the type of the graph to "shortestpath" graph deba@2491: /// --version|-v deba@2491: /// show version information deba@2491: ///\endverbatim deba@2491: /// alpar@2404: alpar@2404: #include alpar@2404: #include alpar@2404: #include alpar@2404: alpar@2404: #include alpar@2404: #include alpar@2404: #include alpar@2404: deba@2410: #include deba@2410: alpar@2404: using namespace std; alpar@2404: using namespace lemon; alpar@2404: alpar@2404: alpar@2404: int main(int argc, const char *argv[]) { alpar@2404: typedef SmartGraph Graph; alpar@2404: alpar@2404: typedef Graph::Edge Edge; alpar@2404: typedef Graph::Node Node; alpar@2404: typedef Graph::EdgeIt EdgeIt; alpar@2404: typedef Graph::NodeIt NodeIt; deba@2413: typedef Graph::EdgeMap DoubleEdgeMap; deba@2413: typedef Graph::NodeMap DoubleNodeMap; alpar@2404: alpar@2404: std::string inputName; alpar@2404: std::string outputName; alpar@2404: std::string typeName; alpar@2404: deba@2410: bool mincostflow; deba@2410: bool maxflow; deba@2410: bool shortestpath; deba@2410: bool capacitated; deba@2410: bool plain; alpar@2404: deba@2410: bool version; alpar@2404: deba@2410: ArgParser ap(argc, argv); deba@2410: ap.refOption("-input", deba@2410: "use FILE as input instead of standard input", deba@2410: inputName).synonym("i", "-input") deba@2410: .refOption("-output", deba@2410: "use FILE as output instead of standard output", deba@2410: outputName).synonym("o", "-output") deba@2410: .refOption("-mincostflow", deba@2410: "set the type of the graph to \"mincostflow\" graph", deba@2410: mincostflow) deba@2410: .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow") deba@2410: .refOption("-maxflow", deba@2410: "set the type of the graph to \"maxflow\" graph", deba@2410: maxflow) deba@2410: .optionGroup("type", "-maxflow").synonym("mf", "-maxflow") deba@2410: .refOption("-shortestpath", deba@2410: "set the type of the graph to \"shortestpath\" graph", deba@2410: shortestpath) deba@2410: .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath") deba@2410: .refOption("-capacitated", deba@2410: "set the type of the graph to \"capacitated\" graph", deba@2410: capacitated) deba@2410: .optionGroup("type", "-capacitated").synonym("cap", "-capacitated") deba@2410: .refOption("-plain", deba@2410: "set the type of the graph to \"plain\" graph", deba@2410: plain) deba@2410: .optionGroup("type", "-plain").synonym("pl", "-plain") deba@2410: .onlyOneGroup("type") deba@2410: .mandatoryGroup("type") deba@2410: .refOption("-version", "show version information", version) deba@2410: .synonym("v", "-version") deba@2410: .run(); alpar@2404: alpar@2404: ifstream input; alpar@2404: if (!inputName.empty()) { alpar@2404: input.open(inputName.c_str()); alpar@2404: if (!input) { alpar@2404: cerr << "File open error" << endl; alpar@2404: return -1; alpar@2404: } alpar@2404: } alpar@2404: istream& is = (inputName.empty() ? cin : input); alpar@2404: alpar@2404: ofstream output; alpar@2404: if (!outputName.empty()) { alpar@2404: output.open(outputName.c_str()); alpar@2404: if (!output) { alpar@2404: cerr << "File open error" << endl; alpar@2404: return -1; alpar@2404: } alpar@2404: } alpar@2404: ostream& os = (outputName.empty() ? cout : output); alpar@2404: deba@2410: if (mincostflow) { alpar@2404: Graph graph; deba@2417: DoubleEdgeMap lower(graph), capacity(graph), cost(graph); deba@2413: DoubleNodeMap supply(graph); deba@2417: readDimacs(is, graph, lower, capacity, cost, supply); alpar@2404: GraphWriter(os, graph). deba@2413: writeNodeMap("supply", supply). deba@2413: writeEdgeMap("lower", lower). alpar@2404: writeEdgeMap("capacity", capacity). alpar@2404: writeEdgeMap("cost", cost). alpar@2404: run(); deba@2410: } else if (maxflow) { alpar@2404: Graph graph; alpar@2404: Node s, t; deba@2413: DoubleEdgeMap capacity(graph); alpar@2404: readDimacs(is, graph, capacity, s, t); alpar@2404: GraphWriter(os, graph). alpar@2404: writeEdgeMap("capacity", capacity). alpar@2404: writeNode("source", s). alpar@2404: writeNode("target", t). alpar@2404: run(); deba@2410: } else if (shortestpath) { alpar@2404: Graph graph; alpar@2404: Node s; deba@2413: DoubleEdgeMap capacity(graph); alpar@2404: readDimacs(is, graph, capacity, s); alpar@2404: GraphWriter(os, graph). alpar@2404: writeEdgeMap("capacity", capacity). alpar@2404: writeNode("source", s). alpar@2404: run(); deba@2410: } else if (capacitated) { alpar@2404: Graph graph; deba@2413: DoubleEdgeMap capacity(graph); alpar@2404: readDimacs(is, graph, capacity); alpar@2404: GraphWriter(os, graph). alpar@2404: writeEdgeMap("capacity", capacity). alpar@2404: run(); deba@2410: } else if (plain) { alpar@2404: Graph graph; alpar@2404: readDimacs(is, graph); alpar@2404: GraphWriter(os, graph).run(); alpar@2404: } else { alpar@2404: cerr << "Invalid type error" << endl; alpar@2404: return -1; alpar@2404: } alpar@2404: return 0; alpar@2404: }