最大流 C++实现

#include 
#include 

using namespace std;

#define VERTEX 7 //顶点的个数
#define UINT_MAX 0xFFFF
//图
/
//        S   A   B   C   D   E   T
//	  S   0   3   1   0   0   0   0 
//    A   0   0   0   3   0   0   0
//    B   0   0   0   5   4   0   0
//    C   0   0   0   0   0   0   2  
//	  D   0   0   0   0   0   2   0
//	  E   0   0   0   0   0   0   3
//	  T   0   0   0   0   0   0   0


//保存流量
int capacity[VERTEX][VERTEX];

//是否已访问
bool visited[VERTEX];

//保存增广路径
int from[VERTEX];

//求最小值
int min(int x,int y){
	return x q;

	q.push(0);
	
	visited[0] = true;

	init_from();

	//找出一条增广路线
	
	while(q.empty()==false){
		where = q.front();
		q.pop();
		for(next=0;next0){
				q.push(next);
				visited[next] = true;
				from[next] = where;
				if(next==VERTEX-1)
					goto now;//exit while
			}
		}
	}
now:
	//计算增广路线的最小流量
	where = VERTEX-1;
	path_cap = UINT_MAX;
	while(from[where]>-1){
		//前驱节点
		prev = from[where];
		path_cap = min(path_cap,capacity[prev][where]);
		where = prev;
	}

	//如果增广路径没找到或者path_cap是UINT_MAX
	if(path_cap == UINT_MAX)
		return 0;
	else return path_cap;
}

//计算最大流
int max_flow(){
	//最大流
	int result = 0;
	//每条路线的流量
	int path_capacity=0;

	while(true){
		//find_path返回找到的增广路线的流量,返回0表示没找到
		path_capacity=find_path();
		if(path_capacity==0){
			break;
		}else{
			result += path_capacity;
		}
	}

	return result;
}

void main(){
	init_capacity();
	init_visited();
	printf("%d\n",max_flow());
	return;
}


#include 
#include 
#include 

using namespace std;

#define VERTEX 7 //顶点的个数
#define UINT_MAX 0xFFFF
//保存流量
int capacity[VERTEX][VERTEX];

//是否已访问
bool visited[VERTEX];

//保存增广路径
int from[VERTEX];

typedef struct NODE{
	int vertex;
	int priority;
	int from;//存储前驱节点
}node,*pnode;

//求最小值
int min(int x,int y){
	return xvertex = vertex;
	n->priority = priority;
	n->from = from;
	return n;
}

//priority-first search (PFS)
int find_path(){
	int where,cost;
	int new_cost;
	int path_cap;
	int next;
	int prev;//前驱节点

	priority_queue pq;
	pq.push(init_node(0,UINT_MAX,-1));
	init_from();
	path_cap = 0;
	while(pq.empty()==false){
		pnode aux = pq.top();
		pq.pop();
		where = aux->vertex;
		cost = aux->priority;
		if(visited[where]) continue;
		from[where] = aux->from;
		if(where == VERTEX-1){
			path_cap = cost;
			break;
		}
		visited[where] = true;
		for(next=0;next0){
				new_cost = min(cost,capacity[where][next]);
				pq.push(init_node(next,new_cost,where));
			}
		}
	}
	//更新残余网络
	where = VERTEX-1;
	while(from[where]>-1){
		prev = from[where];
		capacity[prev][where] -= path_cap;
		capacity[where][prev] += path_cap;
		where = prev;
	}
	return path_cap;
}

//计算最大流
int max_flow(){
	//最大流
	int result = 0;
	//每条路线的流量
	int path_capacity=0;

	while(true){
		//find_path返回找到的增广路线的流量,返回0表示没找到
		path_capacity=find_path();
		if(path_capacity==0){
			break;
		}else{
			result += path_capacity;
		}
	}

	return result;
}

void main(){
	init_capacity();
	init_visited();
	printf("%d\n",max_flow());
	return;
}





参考:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=maxFlow

你可能感兴趣的:(面试题操练场)