tools/dim_to_lgf.cc
author deba
Mon, 07 May 2007 08:47:38 +0000
changeset 2437 02c7076bf894
parent 2413 21eb3ccdc3df
child 2491 b63ae56979ef
permissions -rw-r--r--
Small improvements in MinMeanCycle class.

Patch from Peter Kovacs
     1 /* -*- C++ -*-
     2  *
     3  * This file is a part of LEMON, a generic C++ optimization library
     4  *
     5  * Copyright (C) 2003-2007
     6  * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
     7  * (Egervary Research Group on Combinatorial Optimization, EGRES).
     8  *
     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.
    12  *
    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
    15  * purpose.
    16  *
    17  */
    18 
    19 ///\ingroup demos
    20 ///\file
    21 ///\brief DIMACS to LGF converter.
    22 ///
    23 /// This program converts various DIMACS formats to the LEMON Graph Format
    24 /// (LGF).
    25 ///
    26 /// \include dim_to_lgf.cc
    27 
    28 #include <iostream>
    29 #include <fstream>
    30 #include <cstring>
    31 
    32 #include <lemon/smart_graph.h>
    33 #include <lemon/dimacs.h>
    34 #include <lemon/graph_writer.h>
    35 
    36 #include <lemon/arg_parser.h>
    37 
    38 using namespace std;
    39 using namespace lemon;
    40 
    41 
    42 int main(int argc, const char *argv[]) {
    43   typedef SmartGraph Graph;
    44 
    45   typedef Graph::Edge Edge;
    46   typedef Graph::Node Node;
    47   typedef Graph::EdgeIt EdgeIt;
    48   typedef Graph::NodeIt NodeIt;
    49   typedef Graph::EdgeMap<double> DoubleEdgeMap;
    50   typedef Graph::NodeMap<double> DoubleNodeMap;
    51 
    52   std::string inputName;
    53   std::string outputName;
    54   std::string typeName;
    55 
    56   bool mincostflow;
    57   bool maxflow;
    58   bool shortestpath;
    59   bool capacitated;
    60   bool plain;
    61 
    62   bool version;
    63 
    64   ArgParser ap(argc, argv);
    65   ap.refOption("-input", 
    66                "use FILE as input instead of standard input", 
    67                inputName).synonym("i", "-input")
    68     .refOption("-output", 
    69                "use FILE as output instead of standard output", 
    70                outputName).synonym("o", "-output")
    71     .refOption("-mincostflow", 
    72                "set the type of the graph to \"mincostflow\" graph", 
    73                mincostflow)
    74     .optionGroup("type", "-mincostflow").synonym("mcf", "-mincostflow")
    75     .refOption("-maxflow", 
    76                "set the type of the graph to \"maxflow\" graph", 
    77                maxflow)
    78     .optionGroup("type", "-maxflow").synonym("mf", "-maxflow")
    79     .refOption("-shortestpath", 
    80                "set the type of the graph to \"shortestpath\" graph", 
    81                shortestpath)
    82     .optionGroup("type", "-shortestpath").synonym("sp", "-shortestpath")
    83     .refOption("-capacitated", 
    84                "set the type of the graph to \"capacitated\" graph", 
    85                capacitated)
    86     .optionGroup("type", "-capacitated").synonym("cap", "-capacitated")
    87     .refOption("-plain", 
    88                "set the type of the graph to \"plain\" graph", 
    89                plain)
    90     .optionGroup("type", "-plain").synonym("pl", "-plain")
    91     .onlyOneGroup("type")
    92     .mandatoryGroup("type")
    93     .refOption("-version", "show version information", version)
    94     .synonym("v", "-version")
    95     .run();
    96 
    97   ifstream input;
    98   if (!inputName.empty()) {
    99     input.open(inputName.c_str());
   100     if (!input) {
   101       cerr << "File open error" << endl;
   102       return -1;
   103     }
   104   }
   105   istream& is = (inputName.empty() ? cin : input);
   106 
   107   ofstream output;
   108   if (!outputName.empty()) {
   109     output.open(outputName.c_str());
   110     if (!output) {
   111       cerr << "File open error" << endl;
   112       return -1;
   113     }
   114   }
   115   ostream& os = (outputName.empty() ? cout : output);
   116 
   117   if (mincostflow) {
   118     Graph graph;
   119     DoubleEdgeMap lower(graph), capacity(graph), cost(graph);
   120     DoubleNodeMap supply(graph);
   121     readDimacs(is, graph, lower, capacity, cost, supply);
   122     GraphWriter<Graph>(os, graph).
   123       writeNodeMap("supply", supply).
   124       writeEdgeMap("lower", lower).
   125       writeEdgeMap("capacity", capacity).
   126       writeEdgeMap("cost", cost).
   127       run();
   128   } else if (maxflow) {
   129     Graph graph;
   130     Node s, t;
   131     DoubleEdgeMap capacity(graph);
   132     readDimacs(is, graph, capacity, s, t);
   133     GraphWriter<Graph>(os, graph).
   134       writeEdgeMap("capacity", capacity).
   135       writeNode("source", s).
   136       writeNode("target", t).
   137       run();
   138   } else if (shortestpath) {
   139     Graph graph;
   140     Node s;
   141     DoubleEdgeMap capacity(graph);
   142     readDimacs(is, graph, capacity, s);
   143     GraphWriter<Graph>(os, graph).
   144       writeEdgeMap("capacity", capacity).
   145       writeNode("source", s).
   146       run();
   147   } else if (capacitated) {
   148     Graph graph;
   149     DoubleEdgeMap capacity(graph);
   150     readDimacs(is, graph, capacity);
   151     GraphWriter<Graph>(os, graph).
   152       writeEdgeMap("capacity", capacity).
   153       run();
   154   } else if (plain) {
   155     Graph graph;
   156     readDimacs(is, graph);
   157     GraphWriter<Graph>(os, graph).run();
   158   } else {
   159     cerr << "Invalid type error" << endl;
   160     return -1;
   161   }
   162   return 0;
   163 }