marci@423: // -*- c++ -*- marci@423: #ifndef HUGO_DIMACS_H marci@423: #define HUGO_DIMACS_H marci@423: marci@423: #include marci@423: #include marci@423: #include marci@423: klao@442: /// \file klao@442: /// \brief Dimacs file format reader. klao@427: marci@423: namespace hugo { marci@423: klao@427: /// Dimacs flow file format reader function. marci@423: marci@423: /// This function reads a flow instance from dimacs flow format. klao@427: /// At the beginning \c g is cleared by \c g.clear(). klao@427: /// If the data coming from \c is is a max flow problem instance, then klao@427: /// \c s and \c t will be respectively the source and target nodes marci@423: /// and \c capacity will contain the edge capacities. klao@427: /// If the data is a shortest path problem instance then \c s will be the klao@427: /// source node and \c capacity will contain the edge lengths. marci@423: template marci@423: void readDimacsMaxFlow(std::istream& is, Graph &g, typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) { marci@423: g.clear(); marci@423: int cap; marci@423: char d; marci@423: std::string problem; marci@423: char c; marci@423: int i, j; marci@423: std::string str; marci@423: int n, m; marci@423: typename Graph::Edge e; marci@423: std::vector nodes; marci@423: while (is>>c) { marci@423: switch (c) { marci@423: case 'c': //comment marci@423: getline(is, str); marci@423: break; marci@423: case 'p': //problem definition marci@423: is >> problem >> n >> m; marci@423: getline(is, str); marci@423: nodes.resize(n+1); marci@423: for (int k=1; k<=n; ++k) nodes[k]=g.addNode(); marci@423: break; marci@423: case 'n': //node definition marci@423: if (problem=="sp") { //shortest path problem marci@423: is >> i; marci@423: getline(is, str); marci@423: s=nodes[i]; marci@423: } marci@423: if (problem=="max") { //max flow problem marci@423: is >> i >> d; marci@423: getline(is, str); marci@423: if (d=='s') s=nodes[i]; marci@423: if (d=='t') t=nodes[i]; marci@423: } marci@423: break; marci@423: case 'a': marci@423: is >> i >> j >> cap; marci@423: getline(is, str); marci@423: e=g.addEdge(nodes[i], nodes[j]); marci@423: capacity.update(); marci@423: capacity.set(e, cap); marci@423: break; marci@423: } marci@423: } marci@423: } marci@423: marci@423: } //namespace hugo marci@423: marci@423: #endif //HUGO_DIMACS_H