网络流(最大流)

#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
const int maxn = 300;  
const int MAX_INT = ((1 << 31) - 1);
int n;                 //节点数 
int mp[maxn][maxn];    //图 
int pre[maxn];
bool vis[maxn];

bool bfs(int s,int t)  //起点到终点 
{
	queue <int> q1;
	memset(pre,-1,sizeof(pre));
	memset(vis,0,sizeof(vis));
	pre[s]=s;
	vis[s]=1;
	q1.push(s);
	while(!q1.empty())
	{
		int u=q1.front();
		q1.pop();
		for(int i=1;i<=n;i++)
		{
			if(mp[u][i] && !vis[i])
			{
				pre[i]=u;
				vis[i]=1;
				if(i==t)  return 1;
				q1.push(i);
			}	
	    }
	}
	return 0;
}

int EK(int s,int t)
{
	int ans=0;    
	while(bfs(s,t))
	{
		int mi=MAX_INT;
		for(int i=t;i!=s;i=pre[i])
		  mi=min(mi,mp[pre[i]][i]);
		for(int i=t;i!=s;i=pre[i])
		{
			mp[pre[i]][i]-=mi;
			mp[i][pre[i]]+=mi;
		}
		ans+=mi;  
	}
	return ans;
}


猜你喜欢

转载自blog.csdn.net/Unusual_Pants/article/details/80501834