zoukankan      html  css  js  c++  java
  • 最大网络流(Dinic)

    hdu1532

    Drainage Ditches

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


    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
    程序:

    #include"stdio.h"
    #include"string.h"
    #include"stdlib.h"
    #define M 100005
    #define inf 999999999
    #pragma comment(linker, "/STACK:1024000000,1024000000")//可以有效的防止爆栈
    int min(int a,int b)
    {
        return a<b?a:b;
    }
    struct st
    {
        int u,v,next,w;
    }edge[M*2];
    int head[M],t,dis[M],q[M],work[M];
    void init()
    {
        t=0;
        memset(head,-1,sizeof(head));
    }
    void add(int u,int v,int w)//有向边
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].next=head[u];
        head[u]=t++;
        edge[t].u=v;
        edge[t].v=u;
        edge[t].w=0;
        edge[t].next=head[v];
        head[v]=t++;
    }
    /*void add(int u,int v,int w)//无向边
    {
        edge[t].u=u;
        edge[t].v=v;
        edge[t].w=w;
        edge[t].next=head[u];
        head[u]=t++;
        edge[t].u=v;
        edge[t].v=u;
        edge[t].w=w;
        edge[t].next=head[v];
        head[v]=t++;
    }*/
    int bfs(int S,int T)
    {
        int rear=0;
        memset(dis,-1,sizeof(dis));
        dis[S]=0;
        q[rear++]=S;
        for(int i=0;i<rear;i++)
        {
            for(int j=head[q[i]];j!=-1;j=edge[j].next)
            {
                int v=edge[j].v;
                if(edge[j].w&&dis[v]==-1)
                {
                    dis[v]=dis[q[i]]+1;
                    q[rear++]=v;
                    if(v==T)
                        return 1;
                }
            }
        }
        return 0;
    }
    int dfs(int cur,int a,int T)
    {
        if(cur==T)
            return a;
        for(int &i=work[cur];i!=-1;i=edge[i].next)//一定要这样写否则会超时
        {
            int v=edge[i].v;
            if(edge[i].w&&dis[v]==dis[cur]+1)
            {
                int tt=dfs(v,min(a,edge[i].w),T);
                if(tt)
                {
                    edge[i].w-=tt;
                    edge[i^1].w+=tt;
                    return tt;
                }
            }
    
        }
        return 0;
    }
    int Dinic(int S,int T)
    {
        int ans=0;
        while(bfs(S,T))
        {
            memcpy(work,head,sizeof(head));
            while(int tt=dfs(S,inf,T))
                ans+=tt;
        }
        return ans;
    }
    int main()
    {
        int n,m;
        while(scanf("%d%d",&m,&n)!=-1)
        {
            init();
            int a,b,c;
            while(m--)
            {
                scanf("%d%d%d",&a,&b,&c);
                add(a,b,c);
                //add(b,a,c);
            }
            int ans=Dinic(1,n);
            printf("%d
    ",ans);
        }
    }
    


  • 相关阅读:
    开发者必看!探秘阿里云Hi购季开发者分会场:海量学习资源0元起!
    表格存储TableStore2.0重磅发布,提供更强大数据管理能力
    配置管理 ACM 在高可用服务 AHAS 流控降级组件中的应用场景
    利用栈将中缀表达式转换为后缀表达式并进行计算
    利用栈将中缀表达式转换为后缀表达式并进行计算
    Matlab学习点滴
    Matlab学习点滴
    Matlab学习点滴
    栈的基本应用_将字符串逆序输出
    栈的基本应用_将字符串逆序输出
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348241.html
Copyright © 2011-2022 走看看