dimacs.h

Go to the documentation of this file.
00001 /* -*- C++ -*- 00002 * src/lemon/dimacs.h - Part of LEMON, a generic C++ optimization library 00003 * 00004 * Copyright (C) 2004 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport 00005 * (Egervary Combinatorial Optimization Research Group, EGRES). 00006 * 00007 * Permission to use, modify and distribute this software is granted 00008 * provided that this copyright notice appears in all copies. For 00009 * precise terms see the accompanying LICENSE file. 00010 * 00011 * This software is provided "AS IS" with no warranty of any kind, 00012 * express or implied, and with no claim as to its suitability for any 00013 * purpose. 00014 * 00015 */ 00016 00017 #ifndef LEMON_DIMACS_H 00018 #define LEMON_DIMACS_H 00019 00020 #include <iostream> 00021 #include <string> 00022 #include <vector> 00023 #include <lemon/maps.h> 00024 00028 00029 namespace lemon { 00030 00031 00034 00036 00045 template<typename Graph, typename CapacityMap, typename CostMap> 00046 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 00047 typename Graph::Node &s, typename Graph::Node &t, 00048 CostMap& cost) { 00049 g.clear(); 00050 typename CapacityMap::ValueType _cap; 00051 typename CostMap::ValueType _cost; 00052 char d; 00053 std::string problem; 00054 char c; 00055 int i, j; 00056 std::string str; 00057 int n, m; 00058 typename Graph::Edge e; 00059 std::vector<typename Graph::Node> nodes; 00060 while (is>>c) { 00061 switch (c) { 00062 case 'c': //comment 00063 getline(is, str); 00064 break; 00065 case 'p': //problem definition 00066 is >> problem >> n >> m; 00067 getline(is, str); 00068 nodes.resize(n+1); 00069 for (int k=1; k<=n; ++k) nodes[k]=g.addNode(); 00070 break; 00071 case 'n': //node definition 00072 if (problem=="sp") { //shortest path problem 00073 is >> i; 00074 getline(is, str); 00075 s=nodes[i]; 00076 } 00077 if (problem=="max" || problem=="min") { //((max) or (min cost)) flow problem 00078 is >> i >> d; 00079 getline(is, str); 00080 if (d=='s') s=nodes[i]; 00081 if (d=='t') t=nodes[i]; 00082 } 00083 break; 00084 case 'a': 00085 if ( problem == "max" || problem == "sp") { 00086 is >> i >> j >> _cap; 00087 getline(is, str); 00088 e=g.addEdge(nodes[i], nodes[j]); 00089 //capacity.update(); 00090 capacity.set(e, _cap); 00091 } else { 00092 if ( problem == "min" ) { 00093 is >> i >> j >> _cap >> _cost; 00094 getline(is, str); 00095 e=g.addEdge(nodes[i], nodes[j]); 00096 //capacity.update(); 00097 capacity.set(e, _cap); 00098 //cost.update(); 00099 cost.set(e, _cost); 00100 } else { 00101 is >> i >> j; 00102 getline(is, str); 00103 g.addEdge(nodes[i], nodes[j]); 00104 } 00105 } 00106 break; 00107 } 00108 } 00109 } 00110 00111 00113 00121 template<typename Graph, typename CapacityMap> 00122 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 00123 typename Graph::Node &s, typename Graph::Node &t) { 00124 NullMap<typename Graph::Edge, int> n; 00125 readDimacs(is, g, capacity, s, t, n); 00126 } 00127 00128 00130 00138 template<typename Graph, typename CapacityMap> 00139 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity, 00140 typename Graph::Node &s) { 00141 NullMap<typename Graph::Edge, int> n; 00142 readDimacs(is, g, capacity, s, s, n); 00143 } 00144 00145 00147 00153 template<typename Graph, typename CapacityMap> 00154 void readDimacs(std::istream& is, Graph &g, CapacityMap& capacity) { 00155 typename Graph::Node u; 00156 NullMap<typename Graph::Edge, int> n; 00157 readDimacs(is, g, capacity, u, u, n); 00158 } 00159 00160 00162 00169 template<typename Graph> 00170 void readDimacs(std::istream& is, Graph &g) { 00171 typename Graph::Node u; 00172 NullMap<typename Graph::Edge, int> n; 00173 readDimacs(is, g, n, u, u, n); 00174 } 00175 00176 00177 00178 00180 template<typename Graph> 00181 void writeDimacs(std::ostream& os, const Graph &g) { 00182 typedef typename Graph::NodeIt NodeIt; 00183 typedef typename Graph::EdgeIt EdgeIt; 00184 00185 typename Graph::template NodeMap<int> nodes(g); 00186 00187 os << "c matching problem" << std::endl; 00188 00189 int i=1; 00190 for(NodeIt v(g); v!=INVALID; ++v) { 00191 nodes.set(v, i); 00192 ++i; 00193 } 00194 00195 os << "p mat " << g.nodeNum() << " " << g.edgeNum() << std::endl; 00196 00197 for(EdgeIt e(g); e!=INVALID; ++e) { 00198 os << "a " << nodes[g.tail(e)] << " " << nodes[g.head(e)] << std::endl; 00199 } 00200 00201 } 00202 00204 00205 } //namespace lemon 00206 00207 #endif //LEMON_DIMACS_H

Generated on Thu Sep 30 12:18:33 2004 for LEMON by doxygen 1.3.8