deba@1296
|
1 |
#include <iostream>
|
deba@1296
|
2 |
#include <fstream>
|
deba@1296
|
3 |
#include <cstring>
|
deba@1296
|
4 |
|
deba@1296
|
5 |
#include <lemon/smart_graph.h>
|
deba@1296
|
6 |
#include <lemon/dimacs.h>
|
deba@1296
|
7 |
#include <lemon/graph_writer.h>
|
deba@1296
|
8 |
|
deba@1296
|
9 |
using namespace std;
|
deba@1296
|
10 |
using namespace lemon;
|
deba@1296
|
11 |
|
deba@1296
|
12 |
const char* versionString =
|
deba@1296
|
13 |
"dim_to_lgf - part of lemon library\n";
|
deba@1296
|
14 |
|
deba@1296
|
15 |
const char* helpString =
|
deba@1296
|
16 |
"Dimacs to LGF converter\n"
|
deba@1296
|
17 |
"Usage: dim_to_lgf [OPTIONS]\n"
|
deba@1296
|
18 |
"\n"
|
deba@1296
|
19 |
"Examples:\n"
|
deba@1296
|
20 |
" dim_to_lgf --type shortestpath --input graph.dim --output graph.lgf\n"
|
deba@1296
|
21 |
"\n"
|
deba@1296
|
22 |
"Options:\n"
|
deba@1296
|
23 |
" -i FILE, --input FILE use FILE as input instead of standard input\n"
|
deba@1296
|
24 |
" -o FILE, --output FILE use FILE as output instead of standard output\n"
|
deba@1296
|
25 |
" -t TYPE, --type TYPE set up the type of the graph\n"
|
deba@1296
|
26 |
" Possible types:\n"
|
deba@1296
|
27 |
" mincostflow\n"
|
deba@1296
|
28 |
" maxflow (default)\n"
|
deba@1296
|
29 |
" shortestpath\n"
|
deba@1296
|
30 |
" capacitated\n"
|
deba@1296
|
31 |
" plain\n"
|
deba@1296
|
32 |
" -v, --version shows the version of the converter\n"
|
deba@1296
|
33 |
" -h, --help shows the help of the converter\n";
|
deba@1296
|
34 |
|
deba@1296
|
35 |
|
deba@1296
|
36 |
int main(int argc, const char *argv[]) {
|
deba@1296
|
37 |
typedef SmartGraph Graph;
|
deba@1296
|
38 |
|
deba@1296
|
39 |
typedef Graph::Edge Edge;
|
deba@1296
|
40 |
typedef Graph::Node Node;
|
deba@1296
|
41 |
typedef Graph::EdgeIt EdgeIt;
|
deba@1296
|
42 |
typedef Graph::NodeIt NodeIt;
|
deba@1296
|
43 |
typedef Graph::EdgeMap<string> StringMap;
|
deba@1296
|
44 |
|
deba@1296
|
45 |
std::string inputName;
|
deba@1296
|
46 |
std::string outputName;
|
deba@1296
|
47 |
std::string typeName;
|
deba@1296
|
48 |
|
deba@1296
|
49 |
bool help = false;
|
deba@1296
|
50 |
bool version = false;
|
deba@1296
|
51 |
|
deba@1296
|
52 |
for (int arg = 1; arg < argc; ++arg) {
|
deba@1296
|
53 |
if (strcmp(argv[arg], "--type") == 0 ||
|
deba@1296
|
54 |
strcmp(argv[arg], "-t") == 0) {
|
deba@1296
|
55 |
if (!typeName.empty()) {
|
deba@1296
|
56 |
cerr << "Multiple type description" << endl;
|
deba@1296
|
57 |
return -1;
|
deba@1296
|
58 |
}
|
deba@1296
|
59 |
if (arg + 1 == argc) {
|
deba@1296
|
60 |
cerr << "Parameter without value" << endl;
|
deba@1296
|
61 |
return -1;
|
deba@1296
|
62 |
}
|
deba@1296
|
63 |
typeName = argv[++arg];
|
deba@1296
|
64 |
}
|
deba@1296
|
65 |
else if (strcmp(argv[arg], "--input") == 0 ||
|
deba@1296
|
66 |
strcmp(argv[arg], "-i") == 0) {
|
deba@1296
|
67 |
if (!inputName.empty()) {
|
deba@1296
|
68 |
cerr << "Multiple input description" << endl;
|
deba@1296
|
69 |
return -1;
|
deba@1296
|
70 |
}
|
deba@1296
|
71 |
if (arg + 1 == argc) {
|
deba@1296
|
72 |
cerr << "Parameter without value" << endl;
|
deba@1296
|
73 |
return -1;
|
deba@1296
|
74 |
}
|
deba@1296
|
75 |
inputName = argv[++arg];
|
deba@1296
|
76 |
}
|
deba@1296
|
77 |
else if (strcmp(argv[arg], "--output") == 0 ||
|
deba@1296
|
78 |
strcmp(argv[arg], "-o") == 0) {
|
deba@1296
|
79 |
if (!outputName.empty()) {
|
deba@1296
|
80 |
cerr << "Multiple input description" << endl;
|
deba@1296
|
81 |
return -1;
|
deba@1296
|
82 |
}
|
deba@1296
|
83 |
if (arg + 1 == argc) {
|
deba@1296
|
84 |
cerr << "Parameter without value" << endl;
|
deba@1296
|
85 |
return -1;
|
deba@1296
|
86 |
}
|
deba@1296
|
87 |
outputName = argv[++arg];
|
deba@1296
|
88 |
} else if (strcmp(argv[arg], "--help") == 0 ||
|
deba@1296
|
89 |
strcmp(argv[arg], "-h") == 0) {
|
deba@1296
|
90 |
help = true;
|
deba@1296
|
91 |
} else if (strcmp(argv[arg], "--version") == 0 ||
|
deba@1296
|
92 |
strcmp(argv[arg], "-v") == 0) {
|
deba@1296
|
93 |
version = true;
|
deba@1296
|
94 |
} else {
|
deba@1296
|
95 |
cerr << "Invalid option: " << argv[arg] << endl;
|
deba@1296
|
96 |
return -1;
|
deba@1296
|
97 |
}
|
deba@1296
|
98 |
}
|
deba@1296
|
99 |
|
deba@1296
|
100 |
if (version) {
|
deba@1296
|
101 |
cout << versionString;
|
deba@1296
|
102 |
}
|
deba@1296
|
103 |
if (help) {
|
deba@1296
|
104 |
cout << helpString;
|
deba@1296
|
105 |
}
|
deba@1296
|
106 |
if (help || version) {
|
deba@1296
|
107 |
return 0;
|
deba@1296
|
108 |
}
|
deba@1296
|
109 |
|
deba@1296
|
110 |
ifstream input;
|
deba@1296
|
111 |
if (!inputName.empty()) {
|
deba@1296
|
112 |
input.open(inputName.c_str());
|
deba@1296
|
113 |
if (!input) {
|
deba@1296
|
114 |
cerr << "File open error" << endl;
|
deba@1296
|
115 |
return -1;
|
deba@1296
|
116 |
}
|
deba@1296
|
117 |
}
|
deba@1296
|
118 |
istream& is = (inputName.empty() ? cin : input);
|
deba@1296
|
119 |
|
deba@1296
|
120 |
ofstream output;
|
deba@1296
|
121 |
if (!outputName.empty()) {
|
deba@1296
|
122 |
output.open(outputName.c_str());
|
deba@1296
|
123 |
if (!output) {
|
deba@1296
|
124 |
cerr << "File open error" << endl;
|
deba@1296
|
125 |
return -1;
|
deba@1296
|
126 |
}
|
deba@1296
|
127 |
}
|
deba@1296
|
128 |
ostream& os = (outputName.empty() ? cout : output);
|
deba@1296
|
129 |
|
deba@1296
|
130 |
if (typeName.empty()) {
|
deba@1296
|
131 |
typeName = "maxflow";
|
deba@1296
|
132 |
}
|
deba@1296
|
133 |
|
deba@1296
|
134 |
if (typeName == "mincostflow") {
|
deba@1296
|
135 |
Graph graph;
|
deba@1296
|
136 |
Node s, t;
|
deba@1296
|
137 |
StringMap cost(graph), capacity(graph);
|
deba@1296
|
138 |
readDimacs(is, graph, capacity, s, t, cost);
|
deba@1296
|
139 |
writeGraph(os, graph, capacity, s, t, cost);
|
deba@1296
|
140 |
} else if (typeName == "maxflow") {
|
deba@1296
|
141 |
Graph graph;
|
deba@1296
|
142 |
Node s, t;
|
deba@1296
|
143 |
StringMap capacity(graph);
|
deba@1296
|
144 |
readDimacs(is, graph, capacity, s, t);
|
deba@1296
|
145 |
writeGraph(os, graph, capacity, s, t);
|
deba@1296
|
146 |
} else if (typeName == "shortestpath") {
|
deba@1296
|
147 |
Graph graph;
|
deba@1296
|
148 |
Node s;
|
deba@1296
|
149 |
StringMap capacity(graph);
|
deba@1296
|
150 |
readDimacs(is, graph, capacity, s);
|
deba@1296
|
151 |
writeGraph(os, graph, capacity, s);
|
deba@1296
|
152 |
} else if (typeName == "capacitated") {
|
deba@1296
|
153 |
Graph graph;
|
deba@1296
|
154 |
StringMap capacity(graph);
|
deba@1296
|
155 |
readDimacs(is, graph, capacity);
|
deba@1296
|
156 |
writeGraph(os, graph, capacity);
|
deba@1296
|
157 |
} else if (typeName == "plain") {
|
deba@1296
|
158 |
Graph graph;
|
deba@1296
|
159 |
readDimacs(is, graph);
|
deba@1296
|
160 |
writeGraph(os, graph);
|
deba@1296
|
161 |
} else {
|
deba@1296
|
162 |
cerr << "Invalid type error" << endl;
|
deba@1296
|
163 |
return -1;
|
deba@1296
|
164 |
}
|
deba@1296
|
165 |
return 0;
|
deba@1296
|
166 |
}
|