write_dimacs.hpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2006, Stephan Diederich
  2. //
  3. // This code may be used under either of the following two licences:
  4. //
  5. // Permission is hereby granted, free of charge, to any person
  6. // obtaining a copy of this software and associated documentation
  7. // files (the "Software"), to deal in the Software without
  8. // restriction, including without limitation the rights to use,
  9. // copy, modify, merge, publish, distribute, sublicense, and/or
  10. // sell copies of the Software, and to permit persons to whom the
  11. // Software is furnished to do so, subject to the following
  12. // conditions:
  13. //
  14. // The above copyright notice and this permission notice shall be
  15. // included in all copies or substantial portions of the Software.
  16. //
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  19. // OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. // WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. // OTHER DEALINGS IN THE SOFTWARE. OF SUCH DAMAGE.
  25. //
  26. // Or:
  27. //
  28. // Distributed under the Boost Software License, Version 1.0.
  29. // (See accompanying file LICENSE_1_0.txt or copy at
  30. // http://www.boost.org/LICENSE_1_0.txt)
  31. /*
  32. Writes maximal flow problem in extended DIMACS format to an OutputIterator
  33. Vertex indices are read from an IndexMap and shiftet by 1.
  34. so their new range is [1..num_vertices(g)]
  35. */
  36. /* ----------------------------------------------------------------- */
  37. #include <vector>
  38. #include <string>
  39. #include <ostream>
  40. namespace boost
  41. {
  42. template < class Graph, class CapacityMap, class IndexMap >
  43. void write_dimacs_max_flow(const Graph& g, CapacityMap capacity, IndexMap idx,
  44. typename graph_traits< Graph >::vertex_descriptor src,
  45. typename graph_traits< Graph >::vertex_descriptor sink, std::ostream& out)
  46. {
  47. typedef typename graph_traits< Graph >::edge_iterator edge_iterator;
  48. out << "c DIMACS max-flow file generated from boost::write_dimacs_max_flow"
  49. << std::endl;
  50. out << "p max " << num_vertices(g) << " " << num_edges(g)
  51. << std::endl; // print problem description "max" and number of verts and
  52. // edges
  53. out << "n " << get(idx, src) + 1 << " s" << std::endl;
  54. ; // say which one is source
  55. out << "n " << get(idx, sink) + 1 << " t"
  56. << std::endl; // say which one is sink
  57. // output the edges
  58. edge_iterator ei, e_end;
  59. for (boost::tie(ei, e_end) = edges(g); ei != e_end; ++ei)
  60. {
  61. out << "a " << idx[source(*ei, g)] + 1 << " " << idx[target(*ei, g)] + 1
  62. << " " << get(capacity, *ei) << std::endl;
  63. }
  64. }
  65. } // namespace boost