src/work/marci/dimacs.h
author marci
Mon, 26 Apr 2004 16:58:14 +0000
changeset 421 54b943063901
parent 259 509ba9f136d2
permissions -rw-r--r--
For working with undirected graphs, head is changed to aNode.
Some dimacs doki.
marci@174
     1
// -*- c++ -*-
marci@259
     2
#ifndef HUGO_DIMACS_H
marci@259
     3
#define HUGO_DIMACS_H
marci@174
     4
marci@174
     5
#include <iostream>
marci@174
     6
#include <string>
marci@174
     7
#include <vector>
marci@174
     8
marci@174
     9
namespace hugo {
marci@174
    10
marci@421
    11
  /// Dimacs flow files.
marci@421
    12
marci@421
    13
  /// This function reads a flow instance from dimacs flow format.
marci@421
    14
  /// At the beginning \c g is destroyed by \c g.clear().
marci@421
    15
  /// If the data coming from \c is is a max flow innstance, then 
marci@421
    16
  /// \c s and \t will be respectively the source and target nodes 
marci@421
    17
  /// and \c capacity will contain the edge capacities.
marci@421
    18
  /// If the data is a shortest path problem then \c s will be the 
marci@421
    19
  /// source node and \capacity will contain the edge lengths.
marci@174
    20
  template<typename Graph, typename CapacityMap>
marci@421
    21
  void readDimacsMaxFlow(std::istream& is, Graph &g, typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
marci@421
    22
    g.clear();
marci@174
    23
    int cap;
marci@174
    24
    char d;
marci@174
    25
    std::string problem;
marci@174
    26
    char c;
marci@174
    27
    int i, j;
marci@174
    28
    std::string str;
marci@174
    29
    int n, m; 
marci@180
    30
    typename Graph::Edge e;
marci@174
    31
    std::vector<typename Graph::Node> nodes;
marci@174
    32
    while (is>>c) {
marci@174
    33
      switch (c) {
marci@174
    34
      case 'c': //comment
marci@174
    35
	getline(is, str);
marci@174
    36
	break;
marci@174
    37
      case 'p': //problem definition
marci@174
    38
	is >> problem >> n >> m;
marci@174
    39
	getline(is, str);
marci@174
    40
	nodes.resize(n+1);
marci@421
    41
	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
marci@174
    42
	break;
marci@174
    43
      case 'n': //node definition
marci@174
    44
	if (problem=="sp") { //shortest path problem
marci@174
    45
	  is >> i;
marci@174
    46
	  getline(is, str);
marci@174
    47
	  s=nodes[i];
marci@174
    48
	}
marci@174
    49
	if (problem=="max") { //max flow problem
marci@174
    50
	  is >> i >> d;
marci@174
    51
	  getline(is, str);
marci@174
    52
	  if (d=='s') s=nodes[i];
marci@174
    53
	  if (d=='t') t=nodes[i];
marci@174
    54
	}
marci@174
    55
	break;
marci@174
    56
      case 'a':
marci@174
    57
	is >> i >> j >> cap;
marci@174
    58
	getline(is, str);
marci@421
    59
	e=g.addEdge(nodes[i], nodes[j]);
marci@174
    60
	capacity.update();
marci@174
    61
	capacity.set(e, cap);
marci@174
    62
	break;
marci@174
    63
      }
marci@174
    64
    }
marci@174
    65
  }
marci@174
    66
  
marci@174
    67
} //namespace hugo
marci@174
    68
marci@259
    69
#endif //HUGO_DIMACS_H