write_dimacs.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. template <class Graph, class CapacityMap, class IndexMap>
  42. void write_dimacs_max_flow(const Graph& g,
  43. CapacityMap capacity,
  44. IndexMap idx,
  45. typename graph_traits<Graph>::vertex_descriptor src,
  46. typename graph_traits<Graph>::vertex_descriptor sink,
  47. std::ostream& out)
  48. {
  49. typedef typename graph_traits<Graph>::edge_iterator edge_iterator;
  50. out << "c DIMACS max-flow file generated from boost::write_dimacs_max_flow" << std::endl;
  51. out << "p max " << num_vertices(g) << " " << num_edges(g) << std::endl; //print problem description "max" and number of verts and edges
  52. out << "n " << get(idx, src) + 1 << " s" << std::endl;; //say which one is source
  53. out << "n " << get(idx, sink) + 1 << " t" << std::endl; //say which one is sink
  54. //output the edges
  55. edge_iterator ei, e_end;
  56. for(boost::tie(ei,e_end) = edges(g); ei!=e_end; ++ei){
  57. out << "a " << idx[ source(*ei, g) ] + 1 << " " << idx[ target(*ei, g) ] + 1 << " " << get(capacity,*ei) << std::endl;
  58. }
  59. }
  60. } // namespace boost