11 /// \brief Dimacs file format reader.
15 /// Dimacs flow file format reader function.
17 /// This function reads a flow instance from dimacs flow format.
18 /// At the beginning \c g is cleared by \c g.clear().
19 /// If the data coming from \c is is a max flow problem instance, then
20 /// \c s and \c t will be respectively the source and target nodes
21 /// and \c capacity will contain the edge capacities.
22 /// If the data is a shortest path problem instance then \c s will be the
23 /// source node and \c capacity will contain the edge lengths.
25 ///\author Marton Makai
26 template<typename Graph, typename CapacityMap>
27 void readDimacsMaxFlow(std::istream& is, Graph &g,
28 typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
37 typename Graph::Edge e;
38 std::vector<typename Graph::Node> nodes;
44 case 'p': //problem definition
45 is >> problem >> n >> m;
48 for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
50 case 'n': //node definition
51 if (problem=="sp") { //shortest path problem
56 if (problem=="max") { //max flow problem
59 if (d=='s') s=nodes[i];
60 if (d=='t') t=nodes[i];
66 e=g.addEdge(nodes[i], nodes[j]);
75 template<typename Graph>
76 void readDimacs(std::istream& is, Graph &g) {
77 typename Graph::Node u;
78 NullMap<typename Graph::Edge, int> n;
79 readDimacs(is, g, n, u, u, n);
83 template<typename Graph, typename CapacityMap>
84 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
85 typename Graph::Node u;
86 NullMap<typename Graph::Edge, int> n;
87 readDimacs(is, g, capacity, u, u, n);
90 /// shortest path problem
91 template<typename Graph, typename CapacityMap>
92 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
93 typename Graph::Node &s) {
94 NullMap<typename Graph::Edge, int> n;
95 readDimacs(is, g, capacity, s, s, n);
99 template<typename Graph, typename CapacityMap>
100 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
101 typename Graph::Node &s, typename Graph::Node &t) {
102 NullMap<typename Graph::Edge, int> n;
103 readDimacs(is, g, capacity, s, t, n);
106 /// min cost flow problem
107 template<typename Graph, typename CapacityMap, typename CostMap>
108 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
109 typename Graph::Node &s, typename Graph::Node &t,
112 typename CapacityMap::ValueType _cap;
113 typename CostMap::ValueType _cost;
120 typename Graph::Edge e;
121 std::vector<typename Graph::Node> nodes;
127 case 'p': //problem definition
128 is >> problem >> n >> m;
131 for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
133 case 'n': //node definition
134 if (problem=="sp") { //shortest path problem
139 if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
142 if (d=='s') s=nodes[i];
143 if (d=='t') t=nodes[i];
147 if ( problem == "mat" ) {
150 g.addEdge(nodes[i], nodes[j]);
152 if ( problem == "max" || problem == "sp") {
153 is >> i >> j >> _cap;
155 e=g.addEdge(nodes[i], nodes[j]);
157 capacity.set(e, _cap);
159 if ( problem == "min" ) {
160 is >> i >> j >> _cap >> _cost;
162 e=g.addEdge(nodes[i], nodes[j]);
164 capacity.set(e, _cap);
175 /// write matching problem
176 template<typename Graph>
177 void writeDimacs(std::ostream& os, const Graph &g) {
178 typedef typename Graph::NodeIt NodeIt;
179 typedef typename Graph::EdgeIt EdgeIt;
181 typename Graph::template NodeMap<int> nodes(g);
183 os << "c matching problem" << std::endl;
187 for(g.first(v); g.valid(v); g.next(v)) {
192 os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
195 for(g.first(e); g.valid(e); g.next(e)) {
196 os << "a " << nodes[g.tail(e)] << " " << nodes[g.head(e)] << std::endl;
205 #endif //HUGO_DIMACS_H