williamr@2
|
1 |
//=======================================================================
|
williamr@2
|
2 |
// Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
|
williamr@2
|
3 |
// Authors: Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee
|
williamr@2
|
4 |
//
|
williamr@2
|
5 |
// Distributed under the Boost Software License, Version 1.0. (See
|
williamr@2
|
6 |
// accompanying file LICENSE_1_0.txt or copy at
|
williamr@2
|
7 |
// http://www.boost.org/LICENSE_1_0.txt)
|
williamr@2
|
8 |
//=======================================================================
|
williamr@2
|
9 |
|
williamr@2
|
10 |
/*
|
williamr@2
|
11 |
Reads maximal flow problem in extended DIMACS format.
|
williamr@2
|
12 |
|
williamr@2
|
13 |
Reads from stdin.
|
williamr@2
|
14 |
|
williamr@2
|
15 |
This works, but could use some polishing.
|
williamr@2
|
16 |
*/
|
williamr@2
|
17 |
|
williamr@2
|
18 |
/* ----------------------------------------------------------------- */
|
williamr@2
|
19 |
|
williamr@2
|
20 |
#include <vector>
|
williamr@2
|
21 |
#include <stdio.h>
|
williamr@2
|
22 |
|
williamr@2
|
23 |
namespace boost {
|
williamr@2
|
24 |
|
williamr@2
|
25 |
template <class Graph, class CapacityMap, class ReverseEdgeMap>
|
williamr@2
|
26 |
int read_dimacs_max_flow(Graph& g,
|
williamr@2
|
27 |
CapacityMap capacity,
|
williamr@2
|
28 |
ReverseEdgeMap reverse_edge,
|
williamr@2
|
29 |
typename graph_traits<Graph>::vertex_descriptor& src,
|
williamr@2
|
30 |
typename graph_traits<Graph>::vertex_descriptor& sink)
|
williamr@2
|
31 |
{
|
williamr@2
|
32 |
// const int MAXLINE = 100; /* max line length in the input file */
|
williamr@2
|
33 |
const int ARC_FIELDS = 3; /* no of fields in arc line */
|
williamr@2
|
34 |
const int NODE_FIELDS = 2; /* no of fields in node line */
|
williamr@2
|
35 |
const int P_FIELDS = 3; /* no of fields in problem line */
|
williamr@2
|
36 |
const char* PROBLEM_TYPE = "max"; /* name of problem type*/
|
williamr@2
|
37 |
|
williamr@2
|
38 |
typedef typename graph_traits<Graph>::vertices_size_type vertices_size_type;
|
williamr@2
|
39 |
typedef typename graph_traits<Graph>::vertex_descriptor vertex_descriptor;
|
williamr@2
|
40 |
typedef typename graph_traits<Graph>::edge_descriptor edge_descriptor;
|
williamr@2
|
41 |
|
williamr@2
|
42 |
std::vector<vertex_descriptor> verts;
|
williamr@2
|
43 |
|
williamr@2
|
44 |
long m, n, /* number of edges and nodes */
|
williamr@2
|
45 |
i, head, tail, cap;
|
williamr@2
|
46 |
|
williamr@2
|
47 |
long no_lines=0, /* no of current input line */
|
williamr@2
|
48 |
no_plines=0, /* no of problem-lines */
|
williamr@2
|
49 |
no_nslines=0, /* no of node-source-lines */
|
williamr@2
|
50 |
no_nklines=0, /* no of node-source-lines */
|
williamr@2
|
51 |
no_alines=0; /* no of arc-lines */
|
williamr@2
|
52 |
|
williamr@2
|
53 |
std::string in_line; /* for reading input line */
|
williamr@2
|
54 |
char pr_type[3]; /* for reading type of the problem */
|
williamr@2
|
55 |
char nd; /* source (s) or sink (t) */
|
williamr@2
|
56 |
|
williamr@2
|
57 |
int k, /* temporary */
|
williamr@2
|
58 |
err_no; /* no of detected error */
|
williamr@2
|
59 |
|
williamr@2
|
60 |
/* -------------- error numbers & error messages ---------------- */
|
williamr@2
|
61 |
const int EN1 = 0;
|
williamr@2
|
62 |
const int EN2 = 1;
|
williamr@2
|
63 |
const int EN3 = 2;
|
williamr@2
|
64 |
const int EN4 = 3;
|
williamr@2
|
65 |
// const int EN6 = 4;
|
williamr@2
|
66 |
// const int EN10 = 5;
|
williamr@2
|
67 |
// const int EN7 = 6;
|
williamr@2
|
68 |
const int EN8 = 7;
|
williamr@2
|
69 |
const int EN9 = 8;
|
williamr@2
|
70 |
const int EN11 = 9;
|
williamr@2
|
71 |
const int EN12 = 10;
|
williamr@2
|
72 |
// const int EN13 = 11;
|
williamr@2
|
73 |
const int EN14 = 12;
|
williamr@2
|
74 |
const int EN16 = 13;
|
williamr@2
|
75 |
const int EN15 = 14;
|
williamr@2
|
76 |
const int EN17 = 15;
|
williamr@2
|
77 |
const int EN18 = 16;
|
williamr@2
|
78 |
const int EN21 = 17;
|
williamr@2
|
79 |
const int EN19 = 18;
|
williamr@2
|
80 |
const int EN20 = 19;
|
williamr@2
|
81 |
const int EN22 = 20;
|
williamr@2
|
82 |
|
williamr@2
|
83 |
static char *err_message[] =
|
williamr@2
|
84 |
{
|
williamr@2
|
85 |
/* 0*/ "more than one problem line.",
|
williamr@2
|
86 |
/* 1*/ "wrong number of parameters in the problem line.",
|
williamr@2
|
87 |
/* 2*/ "it is not a Max Flow problem line.",
|
williamr@2
|
88 |
/* 3*/ "bad value of a parameter in the problem line.",
|
williamr@2
|
89 |
/* 4*/ "can't obtain enough memory to solve this problem.",
|
williamr@2
|
90 |
/* 5*/ "more than one line with the problem name.",
|
williamr@2
|
91 |
/* 6*/ "can't read problem name.",
|
williamr@2
|
92 |
/* 7*/ "problem description must be before node description.",
|
williamr@2
|
93 |
/* 8*/ "this parser doesn't support multiply sources and sinks.",
|
williamr@2
|
94 |
/* 9*/ "wrong number of parameters in the node line.",
|
williamr@2
|
95 |
/*10*/ "wrong value of parameters in the node line.",
|
williamr@2
|
96 |
/*11*/ " ",
|
williamr@2
|
97 |
/*12*/ "source and sink descriptions must be before arc descriptions.",
|
williamr@2
|
98 |
/*13*/ "too many arcs in the input.",
|
williamr@2
|
99 |
/*14*/ "wrong number of parameters in the arc line.",
|
williamr@2
|
100 |
/*15*/ "wrong value of parameters in the arc line.",
|
williamr@2
|
101 |
/*16*/ "unknown line type in the input.",
|
williamr@2
|
102 |
/*17*/ "reading error.",
|
williamr@2
|
103 |
/*18*/ "not enough arcs in the input.",
|
williamr@2
|
104 |
/*19*/ "source or sink doesn't have incident arcs.",
|
williamr@2
|
105 |
/*20*/ "can't read anything from the input file."
|
williamr@2
|
106 |
};
|
williamr@2
|
107 |
/* --------------------------------------------------------------- */
|
williamr@2
|
108 |
|
williamr@2
|
109 |
/* The main loop:
|
williamr@2
|
110 |
- reads the line of the input,
|
williamr@2
|
111 |
- analyses its type,
|
williamr@2
|
112 |
- checks correctness of parameters,
|
williamr@2
|
113 |
- puts data to the arrays,
|
williamr@2
|
114 |
- does service functions
|
williamr@2
|
115 |
*/
|
williamr@2
|
116 |
|
williamr@2
|
117 |
while (std::getline(std::cin, in_line)) {
|
williamr@2
|
118 |
++no_lines;
|
williamr@2
|
119 |
|
williamr@2
|
120 |
switch (in_line[0]) {
|
williamr@2
|
121 |
case 'c': /* skip lines with comments */
|
williamr@2
|
122 |
case '\n': /* skip empty lines */
|
williamr@2
|
123 |
case '\0': /* skip empty lines at the end of file */
|
williamr@2
|
124 |
break;
|
williamr@2
|
125 |
|
williamr@2
|
126 |
case 'p': /* problem description */
|
williamr@2
|
127 |
if ( no_plines > 0 )
|
williamr@2
|
128 |
/* more than one problem line */
|
williamr@2
|
129 |
{ err_no = EN1 ; goto error; }
|
williamr@2
|
130 |
|
williamr@2
|
131 |
no_plines = 1;
|
williamr@2
|
132 |
|
williamr@2
|
133 |
if (
|
williamr@2
|
134 |
/* reading problem line: type of problem, no of nodes, no of arcs */
|
williamr@2
|
135 |
sscanf ( in_line.c_str(), "%*c %3s %ld %ld", pr_type, &n, &m )
|
williamr@2
|
136 |
!= P_FIELDS
|
williamr@2
|
137 |
)
|
williamr@2
|
138 |
/*wrong number of parameters in the problem line*/
|
williamr@2
|
139 |
{ err_no = EN2; goto error; }
|
williamr@2
|
140 |
|
williamr@2
|
141 |
if ( strcmp ( pr_type, PROBLEM_TYPE ) )
|
williamr@2
|
142 |
/*wrong problem type*/
|
williamr@2
|
143 |
{ err_no = EN3; goto error; }
|
williamr@2
|
144 |
|
williamr@2
|
145 |
if ( n <= 0 || m <= 0 )
|
williamr@2
|
146 |
/*wrong value of no of arcs or nodes*/
|
williamr@2
|
147 |
{ err_no = EN4; goto error; }
|
williamr@2
|
148 |
|
williamr@2
|
149 |
{
|
williamr@2
|
150 |
for (long vi = 0; vi < n; ++vi)
|
williamr@2
|
151 |
verts.push_back(add_vertex(g));
|
williamr@2
|
152 |
}
|
williamr@2
|
153 |
break;
|
williamr@2
|
154 |
|
williamr@2
|
155 |
case 'n': /* source(s) description */
|
williamr@2
|
156 |
if ( no_plines == 0 )
|
williamr@2
|
157 |
/* there was not problem line above */
|
williamr@2
|
158 |
{ err_no = EN8; goto error; }
|
williamr@2
|
159 |
|
williamr@2
|
160 |
/* reading source or sink */
|
williamr@2
|
161 |
k = sscanf ( in_line.c_str(),"%*c %ld %c", &i, &nd );
|
williamr@2
|
162 |
--i; // index from 0
|
williamr@2
|
163 |
if ( k < NODE_FIELDS )
|
williamr@2
|
164 |
/* node line is incorrect */
|
williamr@2
|
165 |
{ err_no = EN11; goto error; }
|
williamr@2
|
166 |
|
williamr@2
|
167 |
if ( i < 0 || i > n )
|
williamr@2
|
168 |
/* wrong value of node */
|
williamr@2
|
169 |
{ err_no = EN12; goto error; }
|
williamr@2
|
170 |
|
williamr@2
|
171 |
switch (nd) {
|
williamr@2
|
172 |
case 's': /* source line */
|
williamr@2
|
173 |
|
williamr@2
|
174 |
if ( no_nslines != 0)
|
williamr@2
|
175 |
/* more than one source line */
|
williamr@2
|
176 |
{ err_no = EN9; goto error; }
|
williamr@2
|
177 |
|
williamr@2
|
178 |
no_nslines = 1;
|
williamr@2
|
179 |
src = verts[i];
|
williamr@2
|
180 |
break;
|
williamr@2
|
181 |
|
williamr@2
|
182 |
case 't': /* sink line */
|
williamr@2
|
183 |
|
williamr@2
|
184 |
if ( no_nklines != 0)
|
williamr@2
|
185 |
/* more than one sink line */
|
williamr@2
|
186 |
{ err_no = EN9; goto error; }
|
williamr@2
|
187 |
|
williamr@2
|
188 |
no_nklines = 1;
|
williamr@2
|
189 |
sink = verts[i];
|
williamr@2
|
190 |
break;
|
williamr@2
|
191 |
|
williamr@2
|
192 |
default:
|
williamr@2
|
193 |
/* wrong type of node-line */
|
williamr@2
|
194 |
err_no = EN12; goto error;
|
williamr@2
|
195 |
}
|
williamr@2
|
196 |
break;
|
williamr@2
|
197 |
|
williamr@2
|
198 |
case 'a': /* arc description */
|
williamr@2
|
199 |
if ( no_nslines == 0 || no_nklines == 0 )
|
williamr@2
|
200 |
/* there was not source and sink description above */
|
williamr@2
|
201 |
{ err_no = EN14; goto error; }
|
williamr@2
|
202 |
|
williamr@2
|
203 |
if ( no_alines >= m )
|
williamr@2
|
204 |
/*too many arcs on input*/
|
williamr@2
|
205 |
{ err_no = EN16; goto error; }
|
williamr@2
|
206 |
|
williamr@2
|
207 |
if (
|
williamr@2
|
208 |
/* reading an arc description */
|
williamr@2
|
209 |
sscanf ( in_line.c_str(),"%*c %ld %ld %ld",
|
williamr@2
|
210 |
&tail, &head, &cap )
|
williamr@2
|
211 |
!= ARC_FIELDS
|
williamr@2
|
212 |
)
|
williamr@2
|
213 |
/* arc description is not correct */
|
williamr@2
|
214 |
{ err_no = EN15; goto error; }
|
williamr@2
|
215 |
|
williamr@2
|
216 |
--tail; // index from 0, not 1
|
williamr@2
|
217 |
--head;
|
williamr@2
|
218 |
if ( tail < 0 || tail > n ||
|
williamr@2
|
219 |
head < 0 || head > n
|
williamr@2
|
220 |
)
|
williamr@2
|
221 |
/* wrong value of nodes */
|
williamr@2
|
222 |
{ err_no = EN17; goto error; }
|
williamr@2
|
223 |
|
williamr@2
|
224 |
{
|
williamr@2
|
225 |
edge_descriptor e1, e2;
|
williamr@2
|
226 |
bool in1, in2;
|
williamr@2
|
227 |
tie(e1, in1) = add_edge(verts[tail], verts[head], g);
|
williamr@2
|
228 |
tie(e2, in2) = add_edge(verts[head], verts[tail], g);
|
williamr@2
|
229 |
if (!in1 || !in2) {
|
williamr@2
|
230 |
std::cerr << "unable to add edge (" << head << "," << tail << ")"
|
williamr@2
|
231 |
<< std::endl;
|
williamr@2
|
232 |
return -1;
|
williamr@2
|
233 |
}
|
williamr@2
|
234 |
capacity[e1] = cap;
|
williamr@2
|
235 |
capacity[e2] = 0;
|
williamr@2
|
236 |
reverse_edge[e1] = e2;
|
williamr@2
|
237 |
reverse_edge[e2] = e1;
|
williamr@2
|
238 |
}
|
williamr@2
|
239 |
++no_alines;
|
williamr@2
|
240 |
break;
|
williamr@2
|
241 |
|
williamr@2
|
242 |
default:
|
williamr@2
|
243 |
/* unknown type of line */
|
williamr@2
|
244 |
err_no = EN18; goto error;
|
williamr@2
|
245 |
|
williamr@2
|
246 |
} /* end of switch */
|
williamr@2
|
247 |
} /* end of input loop */
|
williamr@2
|
248 |
|
williamr@2
|
249 |
/* ----- all is red or error while reading ----- */
|
williamr@2
|
250 |
|
williamr@2
|
251 |
if ( feof (stdin) == 0 ) /* reading error */
|
williamr@2
|
252 |
{ err_no=EN21; goto error; }
|
williamr@2
|
253 |
|
williamr@2
|
254 |
if ( no_lines == 0 ) /* empty input */
|
williamr@2
|
255 |
{ err_no = EN22; goto error; }
|
williamr@2
|
256 |
|
williamr@2
|
257 |
if ( no_alines < m ) /* not enough arcs */
|
williamr@2
|
258 |
{ err_no = EN19; goto error; }
|
williamr@2
|
259 |
|
williamr@2
|
260 |
if ( out_degree(src, g) == 0 || out_degree(sink, g) == 0 )
|
williamr@2
|
261 |
/* no arc goes out of the source */
|
williamr@2
|
262 |
{ err_no = EN20; goto error; }
|
williamr@2
|
263 |
|
williamr@2
|
264 |
/* Thanks God! all is done */
|
williamr@2
|
265 |
return (0);
|
williamr@2
|
266 |
|
williamr@2
|
267 |
/* ---------------------------------- */
|
williamr@2
|
268 |
error: /* error found reading input */
|
williamr@2
|
269 |
|
williamr@2
|
270 |
printf ( "\nline %ld of input - %s\n",
|
williamr@2
|
271 |
no_lines, err_message[err_no] );
|
williamr@2
|
272 |
|
williamr@2
|
273 |
exit (1);
|
williamr@2
|
274 |
return (0); /* to avoid warning */
|
williamr@2
|
275 |
}
|
williamr@2
|
276 |
/* -------------------- end of parser -------------------*/
|
williamr@2
|
277 |
|
williamr@2
|
278 |
} // namespace boost
|