... |
... |
@@ -22,9 +22,9 @@
|
22 |
22 |
#include <iostream>
|
23 |
23 |
#include <string>
|
24 |
24 |
#include <vector>
|
|
25 |
#include <limits>
|
25 |
26 |
#include <lemon/maps.h>
|
26 |
27 |
#include <lemon/error.h>
|
27 |
|
|
28 |
28 |
/// \ingroup dimacs_group
|
29 |
29 |
/// \file
|
30 |
30 |
/// \brief DIMACS file format reader.
|
... |
... |
@@ -55,7 +55,7 @@
|
55 |
55 |
|
56 |
56 |
///Discover the type of a DIMACS file
|
57 |
57 |
|
58 |
|
///It starts seeking the begining of the file for the problem type
|
|
58 |
///It starts seeking the beginning of the file for the problem type
|
59 |
59 |
///and size info. The found data is returned in a special struct
|
60 |
60 |
///that can be evaluated and passed to the appropriate reader
|
61 |
61 |
///function.
|
... |
... |
@@ -105,9 +105,17 @@
|
105 |
105 |
/// p min
|
106 |
106 |
/// \endcode
|
107 |
107 |
/// At the beginning, \c g is cleared by \c g.clear(). The supply
|
108 |
|
/// amount of the nodes are written to \c supply (signed). The
|
109 |
|
/// lower bounds, capacities and costs of the arcs are written to
|
110 |
|
/// \c lower, \c capacity and \c cost.
|
|
108 |
/// amount of the nodes are written to the \c supply node map
|
|
109 |
/// (they are signed values). The lower bounds, capacities and costs
|
|
110 |
/// of the arcs are written to the \c lower, \c capacity and \c cost
|
|
111 |
/// arc maps.
|
|
112 |
///
|
|
113 |
/// If the capacity of an arc is less than the lower bound, it will
|
|
114 |
/// be set to "infinite" instead. The actual value of "infinite" is
|
|
115 |
/// contolled by the \c infty parameter. If it is 0 (the default value),
|
|
116 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available,
|
|
117 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
|
|
118 |
/// a non-zero value, that value will be used as "infinite".
|
111 |
119 |
///
|
112 |
120 |
/// If the file type was previously evaluated by dimacsType(), then
|
113 |
121 |
/// the descriptor struct should be given by the \c dest parameter.
|
... |
... |
@@ -120,6 +128,7 @@
|
120 |
128 |
CapacityMap& capacity,
|
121 |
129 |
CostMap& cost,
|
122 |
130 |
SupplyMap& supply,
|
|
131 |
typename CapacityMap::Value infty = 0,
|
123 |
132 |
DimacsDescriptor desc=DimacsDescriptor())
|
124 |
133 |
{
|
125 |
134 |
g.clear();
|
... |
... |
@@ -142,6 +151,12 @@
|
142 |
151 |
typename CapacityMap::Value low;
|
143 |
152 |
typename CapacityMap::Value cap;
|
144 |
153 |
typename CostMap::Value co;
|
|
154 |
typedef typename CapacityMap::Value Capacity;
|
|
155 |
if(infty==0)
|
|
156 |
infty = std::numeric_limits<Capacity>::has_infinity ?
|
|
157 |
std::numeric_limits<Capacity>::infinity() :
|
|
158 |
std::numeric_limits<Capacity>::max();
|
|
159 |
|
145 |
160 |
while (is >> c) {
|
146 |
161 |
switch (c) {
|
147 |
162 |
case 'c': // comment line
|
... |
... |
@@ -152,15 +167,15 @@
|
152 |
167 |
getline(is, str);
|
153 |
168 |
supply.set(nodes[i], sup);
|
154 |
169 |
break;
|
155 |
|
case 'a': // arc (arc) definition line
|
|
170 |
case 'a': // arc definition line
|
156 |
171 |
is >> i >> j >> low >> cap >> co;
|
157 |
172 |
getline(is, str);
|
158 |
173 |
e = g.addArc(nodes[i], nodes[j]);
|
159 |
174 |
lower.set(e, low);
|
160 |
|
if (cap >= 0)
|
|
175 |
if (cap >= low)
|
161 |
176 |
capacity.set(e, cap);
|
162 |
177 |
else
|
163 |
|
capacity.set(e, -1);
|
|
178 |
capacity.set(e, infty);
|
164 |
179 |
cost.set(e, co);
|
165 |
180 |
break;
|
166 |
181 |
}
|
... |
... |
@@ -173,6 +188,7 @@
|
173 |
188 |
CapacityMap& capacity,
|
174 |
189 |
typename Digraph::Node &s,
|
175 |
190 |
typename Digraph::Node &t,
|
|
191 |
typename CapacityMap::Value infty = 0,
|
176 |
192 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
177 |
193 |
g.clear();
|
178 |
194 |
s=t=INVALID;
|
... |
... |
@@ -186,6 +202,12 @@
|
186 |
202 |
for (int k = 1; k <= desc.nodeNum; ++k) {
|
187 |
203 |
nodes[k] = g.addNode();
|
188 |
204 |
}
|
|
205 |
typedef typename CapacityMap::Value Capacity;
|
|
206 |
|
|
207 |
if(infty==0)
|
|
208 |
infty = std::numeric_limits<Capacity>::has_infinity ?
|
|
209 |
std::numeric_limits<Capacity>::infinity() :
|
|
210 |
std::numeric_limits<Capacity>::max();
|
189 |
211 |
|
190 |
212 |
while (is >> c) {
|
191 |
213 |
switch (c) {
|
... |
... |
@@ -205,14 +227,23 @@
|
205 |
227 |
if (d == 't') t = nodes[i];
|
206 |
228 |
}
|
207 |
229 |
break;
|
208 |
|
case 'a': // arc (arc) definition line
|
209 |
|
if (desc.type==DimacsDescriptor::SP ||
|
210 |
|
desc.type==DimacsDescriptor::MAX) {
|
|
230 |
case 'a': // arc definition line
|
|
231 |
if (desc.type==DimacsDescriptor::SP) {
|
211 |
232 |
is >> i >> j >> _cap;
|
212 |
233 |
getline(is, str);
|
213 |
234 |
e = g.addArc(nodes[i], nodes[j]);
|
214 |
235 |
capacity.set(e, _cap);
|
215 |
|
} else {
|
|
236 |
}
|
|
237 |
else if (desc.type==DimacsDescriptor::MAX) {
|
|
238 |
is >> i >> j >> _cap;
|
|
239 |
getline(is, str);
|
|
240 |
e = g.addArc(nodes[i], nodes[j]);
|
|
241 |
if (_cap >= 0)
|
|
242 |
capacity.set(e, _cap);
|
|
243 |
else
|
|
244 |
capacity.set(e, infty);
|
|
245 |
}
|
|
246 |
else {
|
216 |
247 |
is >> i >> j;
|
217 |
248 |
getline(is, str);
|
218 |
249 |
g.addArc(nodes[i], nodes[j]);
|
... |
... |
@@ -230,8 +261,15 @@
|
230 |
261 |
/// p max
|
231 |
262 |
/// \endcode
|
232 |
263 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc
|
233 |
|
/// capacities are written to \c capacity and \c s and \c t are
|
234 |
|
/// set to the source and the target nodes.
|
|
264 |
/// capacities are written to the \c capacity arc map and \c s and
|
|
265 |
/// \c t are set to the source and the target nodes.
|
|
266 |
///
|
|
267 |
/// If the capacity of an arc is negative, it will
|
|
268 |
/// be set to "infinite" instead. The actual value of "infinite" is
|
|
269 |
/// contolled by the \c infty parameter. If it is 0 (the default value),
|
|
270 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available,
|
|
271 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
|
|
272 |
/// a non-zero value, that value will be used as "infinite".
|
235 |
273 |
///
|
236 |
274 |
/// If the file type was previously evaluated by dimacsType(), then
|
237 |
275 |
/// the descriptor struct should be given by the \c dest parameter.
|
... |
... |
@@ -241,11 +279,12 @@
|
241 |
279 |
CapacityMap& capacity,
|
242 |
280 |
typename Digraph::Node &s,
|
243 |
281 |
typename Digraph::Node &t,
|
|
282 |
typename CapacityMap::Value infty = 0,
|
244 |
283 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
245 |
284 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
246 |
285 |
if(desc.type!=DimacsDescriptor::MAX)
|
247 |
286 |
throw FormatError("Problem type mismatch");
|
248 |
|
_readDimacs(is,g,capacity,s,t,desc);
|
|
287 |
_readDimacs(is,g,capacity,s,t,infty,desc);
|
249 |
288 |
}
|
250 |
289 |
|
251 |
290 |
/// DIMACS shortest path reader function.
|
... |
... |
@@ -256,7 +295,7 @@
|
256 |
295 |
/// p sp
|
257 |
296 |
/// \endcode
|
258 |
297 |
/// At the beginning, \c g is cleared by \c g.clear(). The arc
|
259 |
|
/// lengths are written to \c length and \c s is set to the
|
|
298 |
/// lengths are written to the \c length arc map and \c s is set to the
|
260 |
299 |
/// source node.
|
261 |
300 |
///
|
262 |
301 |
/// If the file type was previously evaluated by dimacsType(), then
|
... |
... |
@@ -271,15 +310,24 @@
|
271 |
310 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
272 |
311 |
if(desc.type!=DimacsDescriptor::SP)
|
273 |
312 |
throw FormatError("Problem type mismatch");
|
274 |
|
_readDimacs(is, g, length, s, t,desc);
|
|
313 |
_readDimacs(is, g, length, s, t, 0, desc);
|
275 |
314 |
}
|
276 |
315 |
|
277 |
316 |
/// DIMACS capacitated digraph reader function.
|
278 |
317 |
///
|
279 |
318 |
/// This function reads an arc capacitated digraph instance from
|
280 |
|
/// DIMACS 'mat' or 'sp' format.
|
|
319 |
/// DIMACS 'max' or 'sp' format.
|
281 |
320 |
/// At the beginning, \c g is cleared by \c g.clear()
|
282 |
|
/// and the arc capacities/lengths are written to \c capacity.
|
|
321 |
/// and the arc capacities/lengths are written to the \c capacity
|
|
322 |
/// arc map.
|
|
323 |
///
|
|
324 |
/// In case of the 'max' format, if the capacity of an arc is negative,
|
|
325 |
/// it will
|
|
326 |
/// be set to "infinite" instead. The actual value of "infinite" is
|
|
327 |
/// contolled by the \c infty parameter. If it is 0 (the default value),
|
|
328 |
/// \c std::numeric_limits<Capacity>::infinity() will be used if available,
|
|
329 |
/// \c std::numeric_limits<Capacity>::max() otherwise. If \c infty is set to
|
|
330 |
/// a non-zero value, that value will be used as "infinite".
|
283 |
331 |
///
|
284 |
332 |
/// If the file type was previously evaluated by dimacsType(), then
|
285 |
333 |
/// the descriptor struct should be given by the \c dest parameter.
|
... |
... |
@@ -287,12 +335,13 @@
|
287 |
335 |
void readDimacsCap(std::istream& is,
|
288 |
336 |
Digraph &g,
|
289 |
337 |
CapacityMap& capacity,
|
|
338 |
typename CapacityMap::Value infty = 0,
|
290 |
339 |
DimacsDescriptor desc=DimacsDescriptor()) {
|
291 |
340 |
typename Digraph::Node u,v;
|
292 |
341 |
if(desc.type==DimacsDescriptor::NONE) desc=dimacsType(is);
|
293 |
342 |
if(desc.type!=DimacsDescriptor::MAX || desc.type!=DimacsDescriptor::SP)
|
294 |
343 |
throw FormatError("Problem type mismatch");
|
295 |
|
_readDimacs(is, g, capacity, u, v, desc);
|
|
344 |
_readDimacs(is, g, capacity, u, v, infty, desc);
|
296 |
345 |
}
|
297 |
346 |
|
298 |
347 |
template<typename Graph>
|
... |
... |
@@ -347,7 +396,7 @@
|
347 |
396 |
break;
|
348 |
397 |
case 'n': // node definition line
|
349 |
398 |
break;
|
350 |
|
case 'a': // arc (arc) definition line
|
|
399 |
case 'a': // arc definition line
|
351 |
400 |
is >> i >> j;
|
352 |
401 |
getline(is, str);
|
353 |
402 |
_addArcEdge(g,nodes[i], nodes[j]);
|