Grafalgo
Library of useful data structures and algorithms
|
00001 #include "maxCap.h" 00002 00003 maxCap::maxCap(Flograph& fg1,int& floVal) : augPath(fg1,floVal) { 00004 // Find maximum flow in fg using the shortest augment path algorithm. 00005 floVal = 0; 00006 while(findPath()) floVal += augment(); 00007 } 00008 00009 bool maxCap::findPath() { 00010 // Find a path with unused residual capacity. 00011 vertex u, v; edge e; 00012 Dheap nheap(fg->n(),2+fg->m()/fg->n()); int bcap[fg->n()+1]; 00013 00014 for (u = 1; u <= fg->n(); u++) { pEdge[u] = 0; bcap[u] = 0; } 00015 bcap[fg->src()] = Util::BIGINT32; 00016 nheap.insert(fg->src(),-Util::BIGINT32); // store negative values, 00017 // so deletemin gives max cap 00018 while (!nheap.empty()) { 00019 u = nheap.deletemin(); 00020 for (e = fg->firstAt(u); e != 0; e = fg->nextAt(u,e)) { 00021 v = fg->mate(u,e); 00022 if (min(bcap[u], fg->res(u,e)) > bcap[v]) { 00023 bcap[v] = min(bcap[u],fg->res(u,e)); 00024 pEdge[v] = e; 00025 if (v == fg->snk()) return true; 00026 if (nheap.member(v)) 00027 nheap.changekey(v,-bcap[v]); 00028 else 00029 nheap.insert(v,-bcap[v]); 00030 } 00031 } 00032 } 00033 return false; 00034 }