Magic triangle is a bit more DONE, and is already not only a triangle.
2 * src/lemon/dimacs.h - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Combinatorial Optimization Research Group, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #ifndef LEMON_DIMACS_H
18 #define LEMON_DIMACS_H
23 #include <lemon/maps.h>
24 #include <lemon/invalid.h>
28 /// \brief Dimacs file format reader.
36 /// Dimacs min cost flow reader function.
38 /// This function reads a min cost flow instance from dimacs format,
39 /// i.e. from dimacs files having a line starting with
43 /// At the beginning \c g is cleared by \c g.clear(). The edge
44 /// capacities are written to \c capacity, \c s and \c t are set to
45 /// the source and the target nodes resp. and the cost of the edges
46 /// are written to \c cost.
48 /// \author Marton Makai
49 template<typename Graph, typename CapacityMap, typename CostMap>
50 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
51 typename Graph::Node &s, typename Graph::Node &t,
54 typename CapacityMap::Value _cap;
55 typename CostMap::Value _cost;
62 typename Graph::Edge e;
63 std::vector<typename Graph::Node> nodes;
69 case 'p': //problem definition
70 is >> problem >> n >> m;
73 for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
75 case 'n': //node definition
76 if (problem=="sp") { //shortest path problem
81 if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
84 if (d=='s') s=nodes[i];
85 if (d=='t') t=nodes[i];
89 if ( problem == "max" || problem == "sp") {
92 e=g.addEdge(nodes[i], nodes[j]);
94 capacity.set(e, _cap);
96 if ( problem == "min" ) {
97 is >> i >> j >> _cap >> _cost;
99 e=g.addEdge(nodes[i], nodes[j]);
101 capacity.set(e, _cap);
107 g.addEdge(nodes[i], nodes[j]);
116 /// Dimacs max flow reader function.
118 /// This function reads a max flow instance from dimacs format,
119 /// i.e. from dimacs files having a line starting with
123 ///At the beginning \c g is cleared by \c g.clear(). The
124 /// edge capacities are written to \c capacity and \c s and \c t are
125 /// set to the source and the target nodes.
127 /// \author Marton Makai
128 template<typename Graph, typename CapacityMap>
129 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
130 typename Graph::Node &s, typename Graph::Node &t) {
131 NullMap<typename Graph::Edge, int> n;
132 readDimacs(is, g, capacity, s, t, n);
136 /// Dimacs shortest path reader function.
138 /// This function reads a shortest path instance from dimacs format,
139 /// i.e. from dimacs files having a line starting with
143 /// At the beginning \c g is cleared by \c g.clear(). The edge
144 /// capacities are written to \c capacity and \c s is set to the
147 /// \author Marton Makai
148 template<typename Graph, typename CapacityMap>
149 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
150 typename Graph::Node &s) {
151 NullMap<typename Graph::Edge, int> n;
152 readDimacs(is, g, capacity, s, s, n);
156 /// Dimacs capacitated graph reader function.
158 /// This function reads an edge capacitated graph instance from
159 /// dimacs format. At the beginning \c g is cleared by \c g.clear()
160 /// and the edge capacities are written to \c capacity.
162 /// \author Marton Makai
163 template<typename Graph, typename CapacityMap>
164 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
165 typename Graph::Node u;
166 NullMap<typename Graph::Edge, int> n;
167 readDimacs(is, g, capacity, u, u, n);
171 /// Dimacs plain graph reader function.
173 /// This function reads a graph without any designated nodes and
174 /// maps from dimacs format, i.e. from dimacs files having a line
179 /// At the beginning \c g is cleared
182 /// \author Marton Makai
183 template<typename Graph>
184 void readDimacs(std::istream& is, Graph &g) {
185 typename Graph::Node u;
186 NullMap<typename Graph::Edge, int> n;
187 readDimacs(is, g, n, u, u, n);
193 /// write matching problem
194 template<typename Graph>
195 void writeDimacs(std::ostream& os, const Graph &g) {
196 typedef typename Graph::NodeIt NodeIt;
197 typedef typename Graph::EdgeIt EdgeIt;
199 typename Graph::template NodeMap<int> nodes(g);
201 os << "c matching problem" << std::endl;
204 for(NodeIt v(g); v!=INVALID; ++v) {
209 os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
211 for(EdgeIt e(g); e!=INVALID; ++e) {
212 os << "a " << nodes[g.source(e)] << " " << nodes[g.target(e)] << std::endl;
221 #endif //LEMON_DIMACS_H