最大流自用模板(例题:HDU1532)

三种模板:Edmonds_Karp,Dinic,SAP

例题:

Drainage Ditches(HDU1532)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 22365    Accepted Submission(s): 10683)

 

Problem Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

 

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond. 

 

Sample Input

5 4

1 2 40

1 4 20

2 4 20

2 3 30

3 4 10

 

Sample Output

50 

 

Source

HDU1532

题意:最大流模板题

方法一:Edmonds_Karp
 

#include 
#include 
#include 
#include 
#include 
#include 
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{
	int from,to,cap,flow;
	Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};

vector edges;
vector G[maxn];
int a[maxn];
int pre[maxn];

void init(int n)
{
	for (int i=0;i Q;
		Q.push(s);
		a[s]=INF;
		while(!Q.empty())
		{
			int x=Q.front();
			Q.pop();
			int sz=G[x].size();
			for (int i=0;ie.flow)
				{
					pre[e.to]=G[x][i];
					a[e.to]=min(a[x],e.cap-e.flow);
					Q.push(e.to);
				}
			}
			if (a[t])
				break;
		}
		if (!a[t])
			break;
		for (int u=t;u!=s;u=edges[pre[u]].from)
		{
			edges[pre[u]].flow+=a[t];
			edges[pre[u]^1].flow-=a[t];
		}
		flow+=a[t];
	}
	return flow;
}

int main()
{
	int n,m,u,v,f;
	while(cin >> n >> m)
	{
		init(n);
		for (int i=0;i

方法二:Dinic

#include 
#include 
#include 
#include 
#include 
#include 
const int maxn=205;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{
    int from,to,flow;
    Edge(int u,int v,int f):from(u),to(v),flow(f){}
};

vector edges;
vector G[maxn];

int dis[maxn];
int cur[maxn];

void init(int n)
{
    for (int i=0;i Q;
    Q.push(s);
    memset(dis,-1,sizeof(dis));
    dis[s]=0;
    while(!Q.empty())
    {
        int x=Q.front();
        Q.pop();
        int sz=G[x].size();
        for (int i=0;i0) 
            {
                if (dis[e.to]<0) 
                {
                    dis[e.to]=dis[x]+1;
                    Q.push(e.to);
                }
            }
        }
    }
    return bool(~dis[t]);
}

int dfs(int s,int t,int maxflow) 
{
    if (s==t)
        return maxflow;
    int sz=int(G[s].size());
    for (int i=cur[s],num;num=G[s][i],i0)
        {
            int flow=dfs(e.to,t,min(maxflow,e.flow));
            if (flow!=0)
            {
                e.flow-=flow;
                edges[num^1].flow+=flow;
                return flow;
            }
        }
    }
    return 0;
}

int Dinic(int s,int t)
{
    int ans=0;
    while(bfs(s,t))
    {
        int flow;
        memset(cur,0,sizeof(cur));
        while((bool)(flow=dfs(s,t,INF)))
            ans+=flow;
    }
    return ans;
}

int main()
{
    int n,m,u,v,f;
    while(cin >> n >> m)
    {
        init(n);
        for (int i=0;i

方法三:SAP

#include 
#include 
#include 
#include 
#include 
#include 
const int maxn=205;
const int maxm=maxn*maxn;
const int INF=0x3f3f3f3f;
using namespace std;

struct Edge
{  
    int v,w,next;  
}edge[maxm];

int dis[maxn],pre[maxn],rec[maxn],head[maxn],gap[maxn],now[maxn];  
int n,m,no,up;
queue q;

void addedge(int u,int v,int w)
{
    edge[no].v=v; edge[no].w=w;
    edge[no].next=head[u]; head[u]=no++;
    edge[no].v=u; edge[no].w=0;
    edge[no].next=head[v]; head[v]=no++;
}

void pre_init()  
{  
    no=0; up=n;
    memset(head,-1,sizeof(head));  
}

void init(int s,int t)
{
    for(int i=0;i<=up;i++) 
    {
    	now[i]=head[i];
    	gap[i]=0;
        dis[i]=INF;
	}

    while(!q.empty())
        q.pop();

    dis[t]=0; q.push(t);
    while(!q.empty())
    {
        int tp=q.front();
        q.pop();
        gap[dis[tp]]++;
        int k=head[tp];
        while(k!=-1)
        {
            if(dis[edge[k].v]==INF && edge[k^1].w)  
            {
                dis[edge[k].v]=dis[tp]+1;  
                q.push(edge[k].v);  
            }
            k=edge[k].next;  
        }
    }
}

int SAP(int s,int t)  
{  
    int ans=0,flow=INF,top=s;  
    pre[s]=s;
    init(s,t);  
    while(dis[s]dis[edge[k].v])
                    mind=dis[edge[k].v];  
                k=edge[k].next;  
            }  
            gap[dis[top]=mind+1]++;  
            top=pre[top];  
        }
    }
    return ans;  
}

int main()
{
    int u,v,f;
    while(cin >> n >> m)
    {
        pre_init();
        for (int i=0;i

 

转载于:https://www.cnblogs.com/Radium1209/p/10415348.html

你可能感兴趣的:(java)