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

/*
  Name: 最大流问题预流推进算法
  Copyright: 
  Author: 巧若拙 
  Date: 14-06-17 09:26
  Description: 改进的预流推进算法,有如下优化:
  进行了预先逆序BFS分层,
  利用结点链表,每次都是从高度最大的结点开始处理.
*/
#include<iostream>    
#include <fstream>  
    
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<m; ++i)    
    {    
        fcin >> u >> v;    
        fcin >> capacity[u][v];        
    }    
      
    cout << n << " " << m << endl;  
    for (int i=0; i<n; i++)  
    {  
        for (int j=0; j<n; j++)  
        {  
            cout << capacity[i][j] << " ";  
        }  
        cout << endl;  
    }  
        
    cout << MaxFlow_relabel_to_front(0, n-1, n) << endl;   
    
    system("pause");                       
    return 0;    
}  

void Check(int u, int n) //对顶点u进行预流推进或重新标号操作,直到其剩余流量为0 
{
    int minFlow, minLevel;
    
    while (flow[u] > 0)
    {
        //先假设顶点u需要relabel  
        bool relabel = true;  
        for (int v=0; v<n; v++)
        {
		 	if (dis[u] == dis[v]+1 && capacity[u][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<n; v++)
 	        {
 	            if (capacity[u][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; i<n; ++i) //初始化列表     
    {    
        dis[i] = 0;  
    }    
    //汇点加入队列   
    Queue[rear++] = des;    
        
    while (front < rear) //队列非空    
    {    
        v = Queue[front++];   
        for(int i=0; i<n; ++i) //寻找未访问过的邻接点,并设置层数    
        {    
            if (dis[i] == 0 && capacity[i][v] != 0)     
            {    
                dis[i] = dis[v] + 1; 
                Queue[rear++] = i;    
            }    
        }      
    }    
    
    if (dis[src] == 0) //未找到源点,说明不存在流
    {
        return false;
    }
    
    VertexNode *s;
    head = new VertexNode;
    head->next = 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; 
}    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值