src/work/jacint/max_save.h
changeset 1363 a32f990b73d9
parent 921 818510fa3d99
equal deleted inserted replaced
1:6cd3db136077 2:8502f85f7c2b
   256         Node w=queue.front();
   256         Node w=queue.front();
   257 	queue.pop();
   257 	queue.pop();
   258 
   258 
   259 	OutEdgeIt e;
   259 	OutEdgeIt e;
   260 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   260 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   261 	  Node v=g->head(e);
   261 	  Node v=g->target(e);
   262 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   262 	  if (!M[v] && (*flow)[e] < (*capacity)[e] ) {
   263 	    queue.push(v);
   263 	    queue.push(v);
   264 	    M.set(v, true);
   264 	    M.set(v, true);
   265 	  }
   265 	  }
   266 	} 
   266 	} 
   267 
   267 
   268 	InEdgeIt f;
   268 	InEdgeIt f;
   269 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   269 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   270 	  Node v=g->tail(f);
   270 	  Node v=g->source(f);
   271 	  if (!M[v] && (*flow)[f] > 0 ) {
   271 	  if (!M[v] && (*flow)[f] > 0 ) {
   272 	    queue.push(v);
   272 	    queue.push(v);
   273 	    M.set(v, true);
   273 	    M.set(v, true);
   274 	  }
   274 	  }
   275 	} 
   275 	} 
   302 	queue.pop();
   302 	queue.pop();
   303 
   303 
   304 
   304 
   305 	InEdgeIt e;
   305 	InEdgeIt e;
   306 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   306 	for(g->first(e,w) ; g->valid(e); g->next(e)) {
   307 	  Node v=g->tail(e);
   307 	  Node v=g->source(e);
   308 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   308 	  if (M[v] && (*flow)[e] < (*capacity)[e] ) {
   309 	    queue.push(v);
   309 	    queue.push(v);
   310 	    M.set(v, false);
   310 	    M.set(v, false);
   311 	  }
   311 	  }
   312 	}
   312 	}
   313 	
   313 	
   314 	OutEdgeIt f;
   314 	OutEdgeIt f;
   315 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   315 	for(g->first(f,w) ; g->valid(f); g->next(f)) {
   316 	  Node v=g->head(f);
   316 	  Node v=g->target(f);
   317 	  if (M[v] && (*flow)[f] > 0 ) {
   317 	  if (M[v] && (*flow)[f] > 0 ) {
   318 	    queue.push(v);
   318 	    queue.push(v);
   319 	    M.set(v, false);
   319 	    M.set(v, false);
   320 	  }
   320 	  }
   321 	}
   321 	}
   367 	  
   367 	  
   368       OutEdgeIt e;
   368       OutEdgeIt e;
   369       for(g->first(e,w); g->valid(e); g->next(e)) {
   369       for(g->first(e,w); g->valid(e); g->next(e)) {
   370 	    
   370 	    
   371 	if ( (*flow)[e] >= (*capacity)[e] ) continue; 
   371 	if ( (*flow)[e] >= (*capacity)[e] ) continue; 
   372 	Node v=g->head(e);            
   372 	Node v=g->target(e);            
   373 	    
   373 	    
   374 	if( lev > level[v] ) { //Push is allowed now
   374 	if( lev > level[v] ) { //Push is allowed now
   375 	  
   375 	  
   376 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   376 	  if ( excess[v]<=0 && v!=t && v!=s ) {
   377 	    int lev_v=level[v];
   377 	    int lev_v=level[v];
   400       if ( exc > 0 ) {	
   400       if ( exc > 0 ) {	
   401 	InEdgeIt e;
   401 	InEdgeIt e;
   402 	for(g->first(e,w); g->valid(e); g->next(e)) {
   402 	for(g->first(e,w); g->valid(e); g->next(e)) {
   403 	  
   403 	  
   404 	  if( (*flow)[e] <= 0 ) continue; 
   404 	  if( (*flow)[e] <= 0 ) continue; 
   405 	  Node v=g->tail(e); 
   405 	  Node v=g->source(e); 
   406 	  
   406 	  
   407 	  if( lev > level[v] ) { //Push is allowed now
   407 	  if( lev > level[v] ) { //Push is allowed now
   408 	    
   408 	    
   409 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   409 	    if ( excess[v]<=0 && v!=t && v!=s ) {
   410 	      int lev_v=level[v];
   410 	      int lev_v=level[v];
   454 				  bfs_queue.pop();
   454 				  bfs_queue.pop();
   455 				  int l=level[v]+1;
   455 				  int l=level[v]+1;
   456 	    
   456 	    
   457 				  InEdgeIt e;
   457 				  InEdgeIt e;
   458 				  for(g->first(e,v); g->valid(e); g->next(e)) {
   458 				  for(g->first(e,v); g->valid(e); g->next(e)) {
   459 				    Node w=g->tail(e);
   459 				    Node w=g->source(e);
   460 				    if ( level[w] == n && w != s ) {
   460 				    if ( level[w] == n && w != s ) {
   461 				      bfs_queue.push(w);
   461 				      bfs_queue.push(w);
   462 				      Node first=level_list[l];
   462 				      Node first=level_list[l];
   463 				      if ( g->valid(first) ) left.set(first,w);
   463 				      if ( g->valid(first) ) left.set(first,w);
   464 				      right.set(w,first);
   464 				      right.set(w,first);
   472 				OutEdgeIt e;
   472 				OutEdgeIt e;
   473 				for(g->first(e,s); g->valid(e); g->next(e)) 
   473 				for(g->first(e,s); g->valid(e); g->next(e)) 
   474 				  {
   474 				  {
   475 				    Num c=(*capacity)[e];
   475 				    Num c=(*capacity)[e];
   476 				    if ( c <= 0 ) continue;
   476 				    if ( c <= 0 ) continue;
   477 				    Node w=g->head(e);
   477 				    Node w=g->target(e);
   478 				    if ( level[w] < n ) {	  
   478 				    if ( level[w] < n ) {	  
   479 				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   479 				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   480 				      flow->set(e, c); 
   480 				      flow->set(e, c); 
   481 				      excess.set(w, excess[w]+c);
   481 				      excess.set(w, excess[w]+c);
   482 				    }
   482 				    }
   499 				  int l=level[v]+1;
   499 				  int l=level[v]+1;
   500 	    
   500 	    
   501 				  InEdgeIt e;
   501 				  InEdgeIt e;
   502 				  for(g->first(e,v); g->valid(e); g->next(e)) {
   502 				  for(g->first(e,v); g->valid(e); g->next(e)) {
   503 				    if ( (*capacity)[e] <= (*flow)[e] ) continue;
   503 				    if ( (*capacity)[e] <= (*flow)[e] ) continue;
   504 				    Node w=g->tail(e);
   504 				    Node w=g->source(e);
   505 				    if ( level[w] == n && w != s ) {
   505 				    if ( level[w] == n && w != s ) {
   506 				      bfs_queue.push(w);
   506 				      bfs_queue.push(w);
   507 				      Node first=level_list[l];
   507 				      Node first=level_list[l];
   508 				      if ( g->valid(first) ) left.set(first,w);
   508 				      if ( g->valid(first) ) left.set(first,w);
   509 				      right.set(w,first);
   509 				      right.set(w,first);
   513 				  }
   513 				  }
   514 	    
   514 	    
   515 				  OutEdgeIt f;
   515 				  OutEdgeIt f;
   516 				  for(g->first(f,v); g->valid(f); g->next(f)) {
   516 				  for(g->first(f,v); g->valid(f); g->next(f)) {
   517 				    if ( 0 >= (*flow)[f] ) continue;
   517 				    if ( 0 >= (*flow)[f] ) continue;
   518 				    Node w=g->head(f);
   518 				    Node w=g->target(f);
   519 				    if ( level[w] == n && w != s ) {
   519 				    if ( level[w] == n && w != s ) {
   520 				      bfs_queue.push(w);
   520 				      bfs_queue.push(w);
   521 				      Node first=level_list[l];
   521 				      Node first=level_list[l];
   522 				      if ( g->valid(first) ) left.set(first,w);
   522 				      if ( g->valid(first) ) left.set(first,w);
   523 				      right.set(w,first);
   523 				      right.set(w,first);
   532 				OutEdgeIt e;
   532 				OutEdgeIt e;
   533 				for(g->first(e,s); g->valid(e); g->next(e)) 
   533 				for(g->first(e,s); g->valid(e); g->next(e)) 
   534 				  {
   534 				  {
   535 				    Num rem=(*capacity)[e]-(*flow)[e];
   535 				    Num rem=(*capacity)[e]-(*flow)[e];
   536 				    if ( rem <= 0 ) continue;
   536 				    if ( rem <= 0 ) continue;
   537 				    Node w=g->head(e);
   537 				    Node w=g->target(e);
   538 				    if ( level[w] < n ) {	  
   538 				    if ( level[w] < n ) {	  
   539 				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   539 				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   540 				      flow->set(e, (*capacity)[e]); 
   540 				      flow->set(e, (*capacity)[e]); 
   541 				      excess.set(w, excess[w]+rem);
   541 				      excess.set(w, excess[w]+rem);
   542 				    }
   542 				    }
   544 	  
   544 	  
   545 				InEdgeIt f;
   545 				InEdgeIt f;
   546 				for(g->first(f,s); g->valid(f); g->next(f)) 
   546 				for(g->first(f,s); g->valid(f); g->next(f)) 
   547 				  {
   547 				  {
   548 				    if ( (*flow)[f] <= 0 ) continue;
   548 				    if ( (*flow)[f] <= 0 ) continue;
   549 				    Node w=g->tail(f);
   549 				    Node w=g->source(f);
   550 				    if ( level[w] < n ) {	  
   550 				    if ( level[w] < n ) {	  
   551 				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   551 				      if ( excess[w] <= 0 && w!=t ) active[level[w]].push(w);
   552 				      excess.set(w, excess[w]+(*flow)[f]);
   552 				      excess.set(w, excess[w]+(*flow)[f]);
   553 				      flow->set(f, 0); 
   553 				      flow->set(f, 0); 
   554 				    }
   554 				    }
   642       int operator[](const typename MapGraphWrapper::Node& n) 
   642       int operator[](const typename MapGraphWrapper::Node& n) 
   643       { return dist[n]; }
   643       { return dist[n]; }
   644       //       int get(const typename MapGraphWrapper::Node& n) const { 
   644       //       int get(const typename MapGraphWrapper::Node& n) const { 
   645       // 	return dist[n]; }
   645       // 	return dist[n]; }
   646       //       bool get(const typename MapGraphWrapper::Edge& e) const { 
   646       //       bool get(const typename MapGraphWrapper::Edge& e) const { 
   647       // 	return (dist.get(g->tail(e))<dist.get(g->head(e))); }
   647       // 	return (dist.get(g->source(e))<dist.get(g->target(e))); }
   648       bool operator[](const typename MapGraphWrapper::Edge& e) const { 
   648       bool operator[](const typename MapGraphWrapper::Edge& e) const { 
   649 	return (dist[g->tail(e)]<dist[g->head(e)]); 
   649 	return (dist[g->source(e)]<dist[g->target(e)]); 
   650       }
   650       }
   651     };
   651     };
   652     
   652     
   653   };
   653   };
   654 
   654 
   781       int l=level[v]+1;
   781       int l=level[v]+1;
   782 	      
   782 	      
   783       InEdgeIt e;
   783       InEdgeIt e;
   784       for(g->first(e,v); g->valid(e); g->next(e)) {
   784       for(g->first(e,v); g->valid(e); g->next(e)) {
   785 	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   785 	if ( (*capacity)[e] <= (*flow)[e] ) continue;
   786 	Node u=g->tail(e);
   786 	Node u=g->source(e);
   787 	if ( level[u] >= n ) { 
   787 	if ( level[u] >= n ) { 
   788 	  bfs_queue.push(u);
   788 	  bfs_queue.push(u);
   789 	  level.set(u, l);
   789 	  level.set(u, l);
   790 	  if ( excess[u] > 0 ) active[l].push(u);
   790 	  if ( excess[u] > 0 ) active[l].push(u);
   791 	}
   791 	}
   792       }
   792       }
   793 	
   793 	
   794       OutEdgeIt f;
   794       OutEdgeIt f;
   795       for(g->first(f,v); g->valid(f); g->next(f)) {
   795       for(g->first(f,v); g->valid(f); g->next(f)) {
   796 	if ( 0 >= (*flow)[f] ) continue;
   796 	if ( 0 >= (*flow)[f] ) continue;
   797 	Node u=g->head(f);
   797 	Node u=g->target(f);
   798 	if ( level[u] >= n ) { 
   798 	if ( level[u] >= n ) { 
   799 	  bfs_queue.push(u);
   799 	  bfs_queue.push(u);
   800 	  level.set(u, l);
   800 	  level.set(u, l);
   801 	  if ( excess[u] > 0 ) active[l].push(u);
   801 	  if ( excess[u] > 0 ) active[l].push(u);
   802 	}
   802 	}
   844 	
   844 	
   845     //searching for augmenting path
   845     //searching for augmenting path
   846     while ( !bfs.finished() ) { 
   846     while ( !bfs.finished() ) { 
   847       ResGWOutEdgeIt e=bfs;
   847       ResGWOutEdgeIt e=bfs;
   848       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   848       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
   849 	Node v=res_graph.tail(e);
   849 	Node v=res_graph.source(e);
   850 	Node w=res_graph.head(e);
   850 	Node w=res_graph.target(e);
   851 	pred.set(w, e);
   851 	pred.set(w, e);
   852 	if (res_graph.valid(pred[v])) {
   852 	if (res_graph.valid(pred[v])) {
   853 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   853 	  free.set(w, std::min(free[v], res_graph.resCap(e)));
   854 	} else {
   854 	} else {
   855 	  free.set(w, res_graph.resCap(e)); 
   855 	  free.set(w, res_graph.resCap(e)); 
   856 	}
   856 	}
   857 	if (res_graph.head(e)==t) { _augment=true; break; }
   857 	if (res_graph.target(e)==t) { _augment=true; break; }
   858       }
   858       }
   859 	
   859 	
   860       ++bfs;
   860       ++bfs;
   861     } //end of searching augmenting path
   861     } //end of searching augmenting path
   862 
   862 
   864       Node n=t;
   864       Node n=t;
   865       Num augment_value=free[t];
   865       Num augment_value=free[t];
   866       while (res_graph.valid(pred[n])) { 
   866       while (res_graph.valid(pred[n])) { 
   867 	ResGWEdge e=pred[n];
   867 	ResGWEdge e=pred[n];
   868 	res_graph.augment(e, augment_value); 
   868 	res_graph.augment(e, augment_value); 
   869 	n=res_graph.tail(e);
   869 	n=res_graph.source(e);
   870       }
   870       }
   871     }
   871     }
   872 
   872 
   873     return _augment;
   873     return _augment;
   874   }
   874   }
   917 
   917 
   918     while ( !bfs.finished() ) { 
   918     while ( !bfs.finished() ) { 
   919       ResGWOutEdgeIt e=bfs;
   919       ResGWOutEdgeIt e=bfs;
   920       if (res_graph.valid(e)) {
   920       if (res_graph.valid(e)) {
   921 	if (bfs.isBNodeNewlyReached()) {
   921 	if (bfs.isBNodeNewlyReached()) {
   922 	  dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
   922 	  dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
   923 	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
   923 	  typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)], res_graph_to_F[res_graph.target(e)]);
   924 	  original_edge.update();
   924 	  original_edge.update();
   925 	  original_edge.set(f, e);
   925 	  original_edge.set(f, e);
   926 	  residual_capacity.update();
   926 	  residual_capacity.update();
   927 	  residual_capacity.set(f, res_graph.resCap(e));
   927 	  residual_capacity.set(f, res_graph.resCap(e));
   928 	} else {
   928 	} else {
   929 	  if (dist[res_graph.head(e)]==(dist[res_graph.tail(e)]+1)) {
   929 	  if (dist[res_graph.target(e)]==(dist[res_graph.source(e)]+1)) {
   930 	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.tail(e)], res_graph_to_F[res_graph.head(e)]);
   930 	    typename MG::Edge f=F.addEdge(res_graph_to_F[res_graph.source(e)], res_graph_to_F[res_graph.target(e)]);
   931 	    original_edge.update();
   931 	    original_edge.update();
   932 	    original_edge.set(f, e);
   932 	    original_edge.set(f, e);
   933 	    residual_capacity.update();
   933 	    residual_capacity.update();
   934 	    residual_capacity.set(f, res_graph.resCap(e));
   934 	    residual_capacity.set(f, res_graph.resCap(e));
   935 	  }
   935 	  }
   979 	typename MG::Node n=tF;
   979 	typename MG::Node n=tF;
   980 	Num augment_value=free[tF];
   980 	Num augment_value=free[tF];
   981 	while (F.valid(pred[n])) { 
   981 	while (F.valid(pred[n])) { 
   982 	  typename MG::Edge e=pred[n];
   982 	  typename MG::Edge e=pred[n];
   983 	  res_graph.augment(original_edge[e], augment_value); 
   983 	  res_graph.augment(original_edge[e], augment_value); 
   984 	  n=F.tail(e);
   984 	  n=F.source(e);
   985 	  if (residual_capacity[e]==augment_value) 
   985 	  if (residual_capacity[e]==augment_value) 
   986 	    F.erase(e); 
   986 	    F.erase(e); 
   987 	  else 
   987 	  else 
   988 	    residual_capacity.set(e, residual_capacity[e]-augment_value);
   988 	    residual_capacity.set(e, residual_capacity[e]-augment_value);
   989 	}
   989 	}
  1013     bfs.pushAndSetReached(s);
  1013     bfs.pushAndSetReached(s);
  1014     DistanceMap<ResGW> dist(res_graph);
  1014     DistanceMap<ResGW> dist(res_graph);
  1015     while ( !bfs.finished() ) { 
  1015     while ( !bfs.finished() ) { 
  1016       ResGWOutEdgeIt e=bfs;
  1016       ResGWOutEdgeIt e=bfs;
  1017       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1017       if (res_graph.valid(e) && bfs.isBNodeNewlyReached()) {
  1018 	dist.set(res_graph.head(e), dist[res_graph.tail(e)]+1);
  1018 	dist.set(res_graph.target(e), dist[res_graph.source(e)]+1);
  1019       }
  1019       }
  1020       ++bfs;
  1020       ++bfs;
  1021     } //computing distances from s in the residual graph
  1021     } //computing distances from s in the residual graph
  1022 
  1022 
  1023       //Subgraph containing the edges on some shortest paths
  1023       //Subgraph containing the edges on some shortest paths
  1110 	// 	  Num j2=a2[b2];
  1110 	// 	  Num j2=a2[b2];
  1111 	Num augment_value=free1[n];
  1111 	Num augment_value=free1[n];
  1112 	while (erasing_res_graph.valid(pred[n])) { 
  1112 	while (erasing_res_graph.valid(pred[n])) { 
  1113 	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1113 	  typename ErasingResGW::OutEdgeIt e=pred[n];
  1114 	  res_graph.augment(e, augment_value);
  1114 	  res_graph.augment(e, augment_value);
  1115 	  n=erasing_res_graph.tail(e);
  1115 	  n=erasing_res_graph.source(e);
  1116 	  if (res_graph.resCap(e)==0)
  1116 	  if (res_graph.resCap(e)==0)
  1117 	    erasing_res_graph.erase(e);
  1117 	    erasing_res_graph.erase(e);
  1118 	}
  1118 	}
  1119       }
  1119       }
  1120       
  1120