src/work/marci/dimacs.h
author marci
Fri, 12 Mar 2004 09:40:03 +0000
changeset 175 ebccffe4d47b
child 180 95f0c5f3fc70
permissions -rw-r--r--
correcting implicit typenames
marci@174
     1
// -*- c++ -*-
marci@174
     2
#ifndef DIMACS_H
marci@174
     3
#define 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@174
    11
  template<typename Graph, typename CapacityMap>
marci@174
    12
  void readDimacsMaxFlow(std::istream& is, Graph &G, typename Graph::Node &s, typename Graph::Node &t, CapacityMap& capacity) {
marci@174
    13
    G.clear();
marci@174
    14
    int cap;
marci@174
    15
    char d;
marci@174
    16
    std::string problem;
marci@174
    17
    char c;
marci@174
    18
    int i, j;
marci@174
    19
    std::string str;
marci@174
    20
    int n, m; 
marci@174
    21
    std::vector<typename Graph::Node> nodes;
marci@174
    22
    while (is>>c) {
marci@174
    23
      switch (c) {
marci@174
    24
      case 'c': //comment
marci@174
    25
	getline(is, str);
marci@174
    26
	break;
marci@174
    27
//       case 't': //type
marci@174
    28
// 	getline(is, str);
marci@174
    29
// 	break;
marci@174
    30
      case 'p': //problem definition
marci@174
    31
	is >> problem >> n >> m;
marci@174
    32
	getline(is, str);
marci@174
    33
	nodes.resize(n+1);
marci@174
    34
	for (int k=1; k<=n; ++k) nodes[k]=G.addNode();
marci@174
    35
	break;
marci@174
    36
      case 'n': //node definition
marci@174
    37
	if (problem=="sp") { //shortest path problem
marci@174
    38
	  is >> i;
marci@174
    39
	  getline(is, str);
marci@174
    40
	  s=nodes[i];
marci@174
    41
	}
marci@174
    42
	if (problem=="max") { //max flow problem
marci@174
    43
	  is >> i >> d;
marci@174
    44
	  getline(is, str);
marci@174
    45
	  if (d=='s') s=nodes[i];
marci@174
    46
	  if (d=='t') t=nodes[i];
marci@174
    47
	}
marci@174
    48
	break;
marci@174
    49
      case 'a':
marci@174
    50
	is >> i >> j >> cap;
marci@174
    51
	getline(is, str);
marci@174
    52
	typename Graph::Edge e=G.addEdge(nodes[i], nodes[j]);
marci@174
    53
	capacity.update();
marci@174
    54
	capacity.set(e, cap);
marci@174
    55
	break;
marci@174
    56
      }
marci@174
    57
    }
marci@174
    58
  }
marci@174
    59
  
marci@174
    60
} //namespace hugo
marci@174
    61
marci@174
    62
#endif //DIMACS_H