marci@747: // -*- c++ -*-
marci@747: #include <iostream>
marci@747: #include <fstream>
marci@747: 
marci@747: #include <sage_graph.h>
alpar@921: #include <lemon/smart_graph.h>
alpar@921: #include <lemon/dimacs.h>
alpar@921: //#include <lemon/time_measure.h>
marci@747: //#include <graph_wrapper.h>
alpar@921: #include <lemon/max_flow.h>
marci@747: //#include <preflow_res.h>
marci@747: #include <for_each_macros.h>
marci@747: #include <graph_concept.h>
marci@747: 
marci@747: using std::cout;
marci@747: using std::endl;
marci@747: 
alpar@921: using namespace lemon;
marci@747: 
marci@747: // Use a DIMACS min cost flow file as stdin.
marci@747: // read_dimacs_demo < dimacs_max_flow_file
marci@747: 
marci@747: int main(int, char **) {
marci@747: 
marci@747:   //typedef SageGraph MutableGraph;
marci@747:   //typedef FullFeatureGraphConcept Graph;
marci@747:   //typedef SmartGraph Graph;
marci@747:   typedef SageGraph Graph;
marci@747:   typedef Graph::Node Node;
marci@747:   typedef Graph::EdgeIt EdgeIt;
marci@747: 
marci@747:   Graph g;
marci@747: 
marci@747:   Node s, t;
marci@747:   Graph::EdgeMap<int> cap(g);
marci@747:   Graph::EdgeMap<int> flow(g); //0 flow
marci@747:   //readDimacs(std::cin, g, cap, s, t);
marci@747:   readDimacs(std::cin, g, cap, s, t, flow);
marci@747: //  Timer ts;
marci@747:   MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> > 
marci@747:     max_flow_test(g, s, t, cap, flow);
marci@747:   
marci@747:   Graph::NodeMap<bool> cut(g);
marci@747: 
marci@747:   {
marci@747:     Graph::EdgeIt e;
marci@747:     for (g.first(e); g.valid(e); g.next(e))
alpar@986:       cout << 1+g.id(g.source(e)) << "->" << 1+g.id(g.target(e)) << " cap: " << cap[e] << " preflow: " << flow[e] << endl;
marci@747:   }
marci@747:   {
marci@747:     Graph::NodeIt n;
marci@747:     for (g.first(n); g.valid(n); g.next(n)) {
marci@747:       int a=0;
marci@747:       {
marci@747: 	Graph::InEdgeIt e;
marci@747: 	for (g.first(e, n); g.valid(e); g.next(e)) a+=flow[e];
marci@747:       }
marci@747:       {
marci@747: 	Graph::OutEdgeIt e;
marci@747: 	for (g.first(e, n); g.valid(e); g.next(e)) a-=flow[e];
marci@747:       }
marci@747:       cout << 1+g.id(n) << " excess: " << a << endl;
marci@747:     }
marci@747:   }
marci@747: 
marci@747: 
marci@747:   {
marci@747: //    std::cout << "preflow ..." << std::endl;
marci@747: //    ts.reset();
marci@747:     max_flow_test.preflowPhase1(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::PRE_FLOW);
marci@747: //    std::cout << "elapsed time: " << ts << std::endl;
marci@747:     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747:     std::cout << "flow value 2: "<< max_flow_test.flowValue2() << std::endl;
marci@747:     max_flow_test.actMinCut(cut);
marci@747:     
marci@747:     Graph::EdgeIt e;
marci@747:     for (g.first(e); g.valid(e); g.next(e)) {
alpar@986:       if (cut[g.source(e)] && !cut[g.target(e)]) {
alpar@986: 	cout << 1+g.id(g.source(e)) << "->" << 1+g.id(g.target(e)) 
marci@747: 	     << "(forward edge) flow: " << flow[e] 
marci@747: 	     << " cap: " << cap[e]<< endl;
marci@747: 	if (flow[e]!=cap[e]) 
marci@747: 	std::cout << "Slackness does not hold!" << std::endl;
marci@747:       }
alpar@986:       if (!cut[g.source(e)] && cut[g.target(e)]) {
alpar@986: 	cout << 1+g.id(g.source(e)) << "->" << 1+g.id(g.target(e)) 
marci@747: 	     << "(backward edge) flow: " << flow[e] << endl;
marci@747: 	if (flow[e]!=0) 
marci@747: 	std::cout << "Slackness does not hold!" << std::endl;
marci@747:       }
marci@747:     }
marci@747:     Graph::NodeIt n;
marci@747:     for (g.first(n); g.valid(n); g.next(n)) {
marci@747:       if (cut[n]) 
marci@747: 	cout << "in cut: " << 1+g.id(n) << endl;
marci@747:     }
marci@747:   }
marci@747: 
marci@747: //   {
marci@747: //     std::cout << "preflow ..." << std::endl;
marci@747: //     ts.reset();
marci@747: //     max_flow_test.run();
marci@747: //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: //     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747: //     max_flow_test.actMinCut(cut);
marci@747: 
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986: //       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986: //       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
marci@747: //     }
marci@747: //   }
marci@747: 
marci@747: //   {
marci@747: //     std::cout << "preflow ..." << std::endl;
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: //     ts.reset();
marci@747: //     max_flow_test.preflow(MaxFlow<Graph, int, Graph::EdgeMap<int>, Graph::EdgeMap<int> >::GEN_FLOW);
marci@747: //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: //     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747: 
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986: //       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986: //       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
marci@747: //     }
marci@747: //   }
marci@747: 
marci@747: // //   {
marci@747: // //     std::cout << "wrapped preflow ..." << std::endl;
marci@747: // //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: // //     ts.reset();
marci@747: // //     pre_flow_res.run();
marci@747: // //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: // //     std::cout << "flow value: "<< pre_flow_test.flowValue() << std::endl;
marci@747: // //   }
marci@747: 
marci@747: //   {
marci@747: //     std::cout << "physical blocking flow augmentation ..." << std::endl;
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: //     ts.reset();
marci@747: //     int i=0;
marci@747: //     while (augmenting_flow_test.augmentOnBlockingFlow<MutableGraph>()) { ++i; }
marci@747: //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: //     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747: //     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747: 
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986: //       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986: //       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
marci@747: //     }
marci@747: //   }
marci@747: 
marci@747: // //   {
marci@747: // //     std::cout << "faster physical blocking flow augmentation ..." << std::endl;
marci@747: // //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: // //     ts.reset();
marci@747: // //     int i=0;
marci@747: // //     while (max_flow_test.augmentOnBlockingFlow1<MutableGraph>()) { ++i; }
marci@747: // //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: // //     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747: // //     std::cout << "flow value: "<< max_flow_test.flowValue() << std::endl;
marci@747: // //   }
marci@747: 
marci@747: //   {
marci@747: //     std::cout << "on-the-fly blocking flow augmentation ..." << std::endl;
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: //     ts.reset();
marci@747: //     int i=0;
marci@747: //     while (augmenting_flow_test.augmentOnBlockingFlow2()) { ++i; }
marci@747: //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: //     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747: //     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747: 
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986: //       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986: //       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
marci@747: //     }
marci@747: //   }
marci@747: 
marci@747: //   {
marci@747: //     std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: //     ts.reset();
marci@747: //     int i=0;
marci@747: //     while (augmenting_flow_test.augmentOnShortestPath()) { ++i; }
marci@747: //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: //     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747: //     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747: 
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986: //       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986: //       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
marci@747: //     }
marci@747: //   }
marci@747: 
marci@747: //   {
marci@747: //     std::cout << "on-the-fly shortest path augmentation ..." << std::endl;
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) flow.set(e, 0);
marci@747: //     ts.reset();
marci@747: //     int i=0;
marci@747: //     while (augmenting_flow_test.augmentOnShortestPath2()) { ++i; }
marci@747: //     std::cout << "elapsed time: " << ts << std::endl;
marci@747: //     std::cout << "number of augmentation phases: " << i << std::endl; 
marci@747: //     std::cout << "flow value: "<< augmenting_flow_test.flowValue() << std::endl;
marci@747: 
marci@747: //     FOR_EACH_LOC(Graph::EdgeIt, e, g) {
alpar@986: //       if (cut[g.source(e)] && !cut[g.target(e)] && !flow[e]==cap[e]) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
alpar@986: //       if (!cut[g.source(e)] && cut[g.target(e)] && flow[e]>0) 
marci@747: // 	std::cout << "Slackness does not hold!" << std::endl;
marci@747: //     }
marci@747: //   }
marci@747: 
marci@747:   return 0;
marci@747: }