read_dimacs.hpp 12 KB

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