00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
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':
00063 getline(is, str);
00064
break;
00065
case 'p':
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':
00072
if (problem==
"sp") {
00073 is >> i;
00074 getline(is, str);
00075 s=nodes[i];
00076 }
00077
if (problem==
"max" || problem==
"min") {
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
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
00097 capacity.set(e, _cap);
00098
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 }
00206
00207
#endif //LEMON_DIMACS_H