最大流问题预流推进算法(BFS优化)

/*
  Name: 最大流问题预流推进算法
  Copyright: 
  Author: 巧若拙 
  Date: 14-06-17 09:26
  Description: 改进的预流推进算法,有如下优化:
  进行了预先逆序BFS分层,
  利用结点链表,每次都是从高度最大的结点开始处理.
*/
#include    
#include   
    
using namespace std;   

typedef struct VertexNode{ //顶点表结点
    int num;  //存储该顶点对应的下标
    struct VertexNode *next; //链域,指向下一个顶点
} VertexNode;
    
const int MAXV=2000;   //最大顶点数量     
const int MAXE=2000;   //最大边数量    
const int INFINITY = 0x7fffffff;   //无穷大     
int capacity[MAXV][MAXV]; //记录残流网络的容量    
int flow[MAXV];  //标记当前节点的剩余流量     
int dis[MAXV]; //标记节点所在的层次  
VertexNode *head; //存储顶点表结点信息  
    
int MaxFlow_relabel_to_front(int src, int des, int n) ;    
bool BFS(int src, int des, int n); //广度优先搜索构造分层网络   
void Check(int u, int n); //对顶点u进行预流推进或重新标号操作,直到其剩余流量为0  
    
int main()    
{   
    int  m, n, u, v;    
    ifstream fcin("maxflow.txt");  
      
    if (!fcin.is_open())  
    {  
        cout << "Error opening file"; exit (1);  
    }  
    
    fcin >> n >> m;  
     
    for(int i=0; i> u >> v;    
        fcin >> capacity[u][v];        
    }    
      
    cout << n << " " << m << endl;  
    for (int i=0; i 0)
    {
        //先假设顶点u需要relabel  
        bool relabel = true;  
        for (int v=0; v 0) //寻找可行弧,并预流推进 
		 	{
                relabel = false;  
   	            minFlow = (flow[u] < capacity[u][v]) ? flow[u] : capacity[u][v];	   
                flow[u] -= minFlow;  
				flow[v] += minFlow;  
				capacity[u][v] -= minFlow;  
				capacity[v][u] += minFlow; 
				printf("push %d --%d--> %d, e[%d] = %d\n", u, minFlow, v, u, flow[u]);  
				if (flow[u] == 0)
				{
  		            break;
                }
			}
		}
					//没有可以push的顶点,执行relabel  
        if (relabel) 
		{   
            minLevel = INFINITY; 
 	        for (int v=0; v 0 && minLevel > dis[v]) //寻找下一层节点  
                {
		            minLevel = dis[v];
                } 
            }
            dis[u] = minLevel + 1;
            printf("relabel %d height to %d\n", u, dis[u]); 
        }  
    }
}  
    
int MaxFlow_relabel_to_front(int src, int des, int n)    
{           
    int u, v, oldLevel, minFlow; 
	VertexNode *p = head;
      
    if (BFS(src, des, n))//先逆向构造分层网络 
    {   
        dis[src] = n;  //直接设置源点的高度为n
	    for (v = 0; v < n; v++) //将源点的流量推进到邻接点 
		{  
	        if (capacity[src][v] > 0) //更新结点剩余流量和残流网络 
			{  
	            flow[src] -= capacity[src][v];  
	            flow[v] = capacity[src][v];  
	            capacity[v][src] = capacity[src][v];  
	            capacity[src][v] = 0; 
	        }  
	    }   
	    
	    VertexNode *pre = head;   
	    while (pre->next)
	    {
	 	    u = pre->next->num;
		 	oldLevel = dis[u];
		 	Check(u, n);
		 	
		 	if (oldLevel < dis[u])//层高增加了,插入到链表首部
			{
	 	        p = pre->next;
	 	        pre->next = p->next; //删除结点p 
	 	        p->next = head->next;//将结点p插入到头结点后面 
	 	        head->next = p;
	 	        pre = head;
			} 
			
			pre = pre->next; //查看下一个结点
		} 
    }
    
    p = head->next;
    while (p)
    {
	     delete head;
	     head = p;
	     p = head->next;
    }
    
    return flow[des];    
}    

bool BFS(int src, int des, int n)//逆向广度优先搜索构造分层网络,若不存在增广路,则返回false    
{    
    int Queue[MAXV];   //求最短增广路算法需要用到的队列   
    int v, front = 0, rear = 0; //清空队列     
        
    for(int i=0; inext = NULL;
    for (front = 1; front < rear; front++)//使用头插法将结点按高度顺序插入链表 
    {
	 	if (Queue[front] != src)
	 	{ 				 
		 	s = new VertexNode;
		 	s->num = Queue[front];  
		 	s->next = head->next;
		 	head->next = s;
		}
	}
        
    return true; 
}    

你可能感兴趣的:(常用算法分析,算法进化历程,图论)