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