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);
80 std::cout<<"igen en.";
84 template<typename Graph, typename CapacityMap>
85 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) {
86 typename Graph::Node u;
87 NullMap<typename Graph::Edge, int> n;
88 readDimacs(is, g, capacity, u, u, n);
91 /// shortest path problem
92 template<typename Graph, typename CapacityMap>
93 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
94 typename Graph::Node &s) {
95 NullMap<typename Graph::Edge, int> n;
96 readDimacs(is, g, capacity, s, s, n);
100 template<typename Graph, typename CapacityMap>
101 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
102 typename Graph::Node &s, typename Graph::Node &t) {
103 NullMap<typename Graph::Edge, int> n;
104 readDimacs(is, g, capacity, s, t, n);
107 /// min cost flow problem
108 template<typename Graph, typename CapacityMap, typename CostMap>
109 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity,
110 typename Graph::Node &s, typename Graph::Node &t,
113 typename CapacityMap::ValueType _cap;
114 typename CostMap::ValueType _cost;
121 typename Graph::Edge e;
122 std::vector<typename Graph::Node> nodes;
128 case 'p': //problem definition
129 is >> problem >> n >> m;
132 for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
134 case 'n': //node definition
135 if (problem=="sp") { //shortest path problem
140 if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem
143 if (d=='s') s=nodes[i];
144 if (d=='t') t=nodes[i];
148 if ( problem == "mat" ) {
151 g.addEdge(nodes[i], nodes[j]);
153 if ( problem == "max" || problem == "sp") {
154 is >> i >> j >> _cap;
156 e=g.addEdge(nodes[i], nodes[j]);
158 capacity.set(e, _cap);
160 if ( problem == "min" ) {
161 is >> i >> j >> _cap >> _cost;
163 e=g.addEdge(nodes[i], nodes[j]);
165 capacity.set(e, _cap);
176 /// write matching problem
177 template<typename Graph>
178 void writeDimacs(std::ostream& os, const Graph &g) {
179 typedef typename Graph::NodeIt NodeIt;
180 typedef typename Graph::EdgeIt EdgeIt;
182 typename Graph::template NodeMap<int> nodes(g);
184 os << "c matching problem" << std::endl;
188 for(g.first(v); g.valid(v); g.next(v)) {
193 os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl;
196 for(g.first(e); g.valid(e); g.next(e)) {
197 os << "a " << nodes[g.tail(e)] << " " << nodes[g.head(e)] << std::endl;
206 #endif //HUGO_DIMACS_H