1 条题解

  • 0
    @ 2025-10-8 16:57:38
    #include<bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int N=210,M=11100;
    struct node{int x,y;LL f; int pre;}a[M];int len,last[N],cur[N];
    void ins(int x,int y,LL f)
    {
    	len++;a[len]=node{x,y,f,last[x]};last[x]=len;
    	len++;a[len]=node{y,x,0,last[y]};last[y]=len;
    }
     
    int h[N],st,ed,n,m;
    bool bfs()
    {
        deque<int>q;q.push_back(st);
        memset(h,0,sizeof(h));h[st]=1;
        while(!q.empty())
        {
            int x=q.front();q.pop_front();
            for(int k=last[x];k>0;k=a[k].pre)if(a[k].f)
            {
                int y=a[k].y;
                if(h[y]==0)
                {
                    h[y]=h[x]+1;
                    q.push_back(y);
                }
            }
        }
        return h[ed]>0;
    }
     
    LL dinic(int x,LL f)
    {
        if(x==ed)return f;
        LL sx=0;
        for(int k=last[x];k;k=a[k].pre)if(a[k].f)
        {
            int y=a[k].y;
            if(h[y]==h[x]+1)
            {
                LL sy=dinic(y,min(a[k].f,f-sx));
                a[k].f-=sy;a[k^1].f+=sy;
                sx+=sy;if(sx==f) return f;
            }
        }
        if(sx==0)h[x]=0;
        return sx;
    }
    int main()
    {
        scanf("%d%d",&m,&n);
        len=1;memset(last,0,sizeof(last));
        for(int i=1;i<=m;i++)
        {
            int x,y;LL c;scanf("%d%d%lld",&x,&y,&c);
            ins(x,y,c);
        }
        st=1;ed=n;
        //dinic过程:如果存在层次图就探索存在的流
        LL s=0;
        while(bfs())
        {
            s+=dinic(st,(LL)1<<62);
        }
        printf("%lld",s);
        return 0;
    }
    
    • 1

    [USACO4.2] 草地排水 Drainage Ditches

    信息

    ID
    1501
    时间
    1000ms
    内存
    64MiB
    难度
    6
    标签
    递交数
    142
    已通过
    43
    上传者