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