src/include/dimacs.h
author marci
Thu, 29 Apr 2004 17:34:42 +0000
changeset 485 7f461ab4af1a
parent 442 267dfa567ad3
child 528 c00f6ebbe1e6
permissions -rw-r--r--
Some docu in MaxFlow class, jacint/max_flow.h
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@465
    23
  ///
marci@465
    24
  ///\author Marton Makai
marci@423
    25
  template<typename Graph, typename CapacityMap>
marci@423
    26
  void readDimacsMaxFlow(std::istream& is, Graph &g, typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
marci@423
    27
    g.clear();
marci@423
    28
    int cap;
marci@423
    29
    char d;
marci@423
    30
    std::string problem;
marci@423
    31
    char c;
marci@423
    32
    int i, j;
marci@423
    33
    std::string str;
marci@423
    34
    int n, m; 
marci@423
    35
    typename Graph::Edge e;
marci@423
    36
    std::vector<typename Graph::Node> nodes;
marci@423
    37
    while (is>>c) {
marci@423
    38
      switch (c) {
marci@423
    39
      case 'c': //comment
marci@423
    40
	getline(is, str);
marci@423
    41
	break;
marci@423
    42
      case 'p': //problem definition
marci@423
    43
	is >> problem >> n >> m;
marci@423
    44
	getline(is, str);
marci@423
    45
	nodes.resize(n+1);
marci@423
    46
	for (int k=1; k<=n; ++k) nodes[k]=g.addNode();
marci@423
    47
	break;
marci@423
    48
      case 'n': //node definition
marci@423
    49
	if (problem=="sp") { //shortest path problem
marci@423
    50
	  is >> i;
marci@423
    51
	  getline(is, str);
marci@423
    52
	  s=nodes[i];
marci@423
    53
	}
marci@423
    54
	if (problem=="max") { //max flow problem
marci@423
    55
	  is >> i >> d;
marci@423
    56
	  getline(is, str);
marci@423
    57
	  if (d=='s') s=nodes[i];
marci@423
    58
	  if (d=='t') t=nodes[i];
marci@423
    59
	}
marci@423
    60
	break;
marci@423
    61
      case 'a':
marci@423
    62
	is >> i >> j >> cap;
marci@423
    63
	getline(is, str);
marci@423
    64
	e=g.addEdge(nodes[i], nodes[j]);
marci@423
    65
	capacity.update();
marci@423
    66
	capacity.set(e, cap);
marci@423
    67
	break;
marci@423
    68
      }
marci@423
    69
    }
marci@423
    70
  }
marci@423
    71
  
marci@423
    72
} //namespace hugo
marci@423
    73
marci@423
    74
#endif //HUGO_DIMACS_H