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