read_dimacs.hpp 9.9 KB

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