2 * demo/dim_to_lgf.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
19 ///\brief DIMACS to LGF converter (demo).
21 /// This program converts various DIMACS formats to the LEMON Graph Format
28 #include <lemon/smart_graph.h>
29 #include <lemon/dimacs.h>
30 #include <lemon/graph_writer.h>
33 using namespace lemon;
35 const char* versionString =
36 "dim_to_lgf - part of lemon library\n";
38 const char* helpString =
39 "DIMACS to LGF converter\n"
40 "Usage: dim_to_lgf [OPTIONS]\n"
43 " dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
46 " -i FILE, --input FILE use FILE as input instead of standard input\n"
47 " -o FILE, --output FILE use FILE as output instead of standard output\n"
48 " -t TYPE, --type TYPE set up the type of the graph\n"
51 " maxflow (default)\n"
55 " -v, --version shows the version of the converter\n"
56 " -h, --help shows the help of the converter\n";
59 int main(int argc, const char *argv[]) {
60 typedef SmartGraph Graph;
62 typedef Graph::Edge Edge;
63 typedef Graph::Node Node;
64 typedef Graph::EdgeIt EdgeIt;
65 typedef Graph::NodeIt NodeIt;
66 typedef Graph::EdgeMap<string> StringMap;
68 std::string inputName;
69 std::string outputName;
75 for (int arg = 1; arg < argc; ++arg) {
76 if (strcmp(argv[arg], "--type") == 0 ||
77 strcmp(argv[arg], "-t") == 0) {
78 if (!typeName.empty()) {
79 cerr << "Multiple type description" << endl;
82 if (arg + 1 == argc) {
83 cerr << "Parameter without value" << endl;
86 typeName = argv[++arg];
88 else if (strcmp(argv[arg], "--input") == 0 ||
89 strcmp(argv[arg], "-i") == 0) {
90 if (!inputName.empty()) {
91 cerr << "Multiple input description" << endl;
94 if (arg + 1 == argc) {
95 cerr << "Parameter without value" << endl;
98 inputName = argv[++arg];
100 else if (strcmp(argv[arg], "--output") == 0 ||
101 strcmp(argv[arg], "-o") == 0) {
102 if (!outputName.empty()) {
103 cerr << "Multiple input description" << endl;
106 if (arg + 1 == argc) {
107 cerr << "Parameter without value" << endl;
110 outputName = argv[++arg];
111 } else if (strcmp(argv[arg], "--help") == 0 ||
112 strcmp(argv[arg], "-h") == 0) {
114 } else if (strcmp(argv[arg], "--version") == 0 ||
115 strcmp(argv[arg], "-v") == 0) {
118 cerr << "Invalid option: " << argv[arg] << endl;
124 cout << versionString;
129 if (help || version) {
134 if (!inputName.empty()) {
135 input.open(inputName.c_str());
137 cerr << "File open error" << endl;
141 istream& is = (inputName.empty() ? cin : input);
144 if (!outputName.empty()) {
145 output.open(outputName.c_str());
147 cerr << "File open error" << endl;
151 ostream& os = (outputName.empty() ? cout : output);
153 if (typeName.empty()) {
154 typeName = "maxflow";
157 if (typeName == "mincostflow") {
160 StringMap cost(graph), capacity(graph);
161 readDimacs(is, graph, capacity, s, t, cost);
162 writeGraph(os, graph, capacity, s, t, cost);
163 } else if (typeName == "maxflow") {
166 StringMap capacity(graph);
167 readDimacs(is, graph, capacity, s, t);
168 writeGraph(os, graph, capacity, s, t);
169 } else if (typeName == "shortestpath") {
172 StringMap capacity(graph);
173 readDimacs(is, graph, capacity, s);
174 writeGraph(os, graph, capacity, s);
175 } else if (typeName == "capacitated") {
177 StringMap capacity(graph);
178 readDimacs(is, graph, capacity);
179 writeGraph(os, graph, capacity);
180 } else if (typeName == "plain") {
182 readDimacs(is, graph);
183 writeGraph(os, graph);
185 cerr << "Invalid type error" << endl;