Island Transport HDU - 4280(ISAP模板)

Island Transport HDU - 4280(ISAP模板)_第1张图片

链接

#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e5+5;
const int maxm=1e5+5;
const int inf=0x3f3f3f3f;

int tt,n,m,s,t;

struct Edge
{
    int nxt,to,flow;
}edges[maxm<<1];

struct ISAP
{
	int head[maxn],cur[maxn],cnt;
	int gap[maxn],last[maxn],depth[maxn];
	
	void add(int u,int v,int flow,int flag)
	{
	    edges[++cnt].to=v;
	    edges[cnt].flow=flag?flow:0;
	    edges[cnt].nxt=head[u];
	    head[u]=cnt;
	} 
	
	void init()
	{
        cnt=-1;
        memset(head,-1,sizeof(head));
        memset(gap,0,sizeof(gap));
        memset(last,-1,sizeof(last));     		
	}
	
	void bfs(int t)
	{
	    queue<int> q;
	    for(int i=0;i<=n;++i)
	        cur[i]=head[i],depth[i]=n;
	    depth[t]=0;
	    q.push(t);
	    while(!q.empty())
	    {
	        int u=q.front();
	        q.pop();
	        for(int i=head[u];i!=-1;i=edges[i].nxt)
	        {
	            int v=edges[i].to;
	            if(depth[v]==n&&edges[i^1].flow)
	            {
	                depth[v]=depth[u]+1;
	                q.push(v);
	            }
	        }        
	    }    
	}
	
	int augment(int s,int t)
	{
	    int flow=inf,u=t;
	    while(u!=s)
	    {
	        int i=last[u];
	        flow=min(flow,edges[i].flow);
	        u=edges[i^1].to;
	    } 
	    u=t;
	    while(u!=s)
	    {
	        int i=last[u];
	        edges[i].flow-=flow;
	        edges[i^1].flow+=flow;
	        u=edges[i^1].to;
	    }
	    return flow;
	}
	
	int Maxflow(int s,int t)
	{
	    int u=s;
	    bfs(t);
	    for(int i=1;i<=n;++i)
	        gap[depth[i]]++;    
	    int maxflow=0;
	    while(depth[s]<n)
	    {
	        if(u==t)
	        {
	            maxflow+=augment(s,t);
	            u=s;
	        }
	        
	        bool isfind=false;
	        for(int i=cur[u];i!=-1;i=edges[i].nxt)
	        {
	            int v=edges[i].to;
	            if(depth[u]==depth[v]+1&&edges[i].flow)
	            {
	                isfind=true;
	                cur[u]=i;
	                u=v;
	                last[v]=i;
	                break;        
	            }
	        }
	        
	        if(!isfind)
	        {
	            int minn=n-1;
	            for(int i=head[u];i!=-1;i=edges[i].nxt)
	                if(edges[i].flow)
	                    minn=min(minn,depth[edges[i].to]);
	            if((--gap[depth[u]])==0)
	                break;
	            gap[depth[u]=minn+1]++;
	            cur[u]=head[u];
	            if(u!=s)
	                u=edges[last[u]^1].to;            
	        }       
	    }
	    return maxflow;
	}
};

int main()
{
    ISAP isap;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);
        int minx=inf,maxx=-inf,s,t;
        for(int i=1;i<=n;++i)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            if(minx>x)
                minx=x,s=i;            
            if(maxx<x)
                maxx=x,t=i;
        }
        
        isap.init();	         
        for(int i=1;i<=m;++i)
        {
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            isap.add(u,v,w,true);
            isap.add(v,u,w,true);
        }
        
        int ans=isap.Maxflow(s,t);
        printf("%d\n",ans);    
    }
    return 0;
}

你可能感兴趣的:(图论,-,网络流,最大流)