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