P3376 【模板】网络最大流

原题链接

题解

优秀博客

朴素贪心:

每次找一条路,易得一条路的最大流量为路径上的最小权值,因此多次bfs遍历找合法路径,然后dfs更新路径上的权值

缺点请看上面的优秀博客

因此建立反向边的作用,有点类似于二分匹配:这条路径的部分流量由我负责,现在你去负责其他路径

第一次学,还是有很多地方不懂

code

#include<bits/stdc++.h>
#define ll long long
#define lb long double
#define lowbit(x) ((x)&(-x))
using namespace std;
const ll inf=1e13;
const ll mod=1e9+7;

ll qpow(ll a,ll n)
{
    ll res=1;
    while(n)
    {
        if(n&1) res=res*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return res;
}
ll inv(ll x)
{
    return qpow(x,mod-2);
}
ll fa[2000005];
ll finds(ll now){return now==fa[now]?now:finds(fa[now]);}


ll depth[205]={0};
ll maxflow=0;
ll G[205][205]={0};

ll n,m,s,t;

bool bfs()
{
    queue<ll> q;
    q.push(s);
    memset(depth,0,sizeof depth);
    depth[s]=1;


    while(q.size())
    {
        ll now=q.front();
        q.pop();
        for(ll i=1;i<=n;i++)
        {
            if(G[now][i]&&!depth[i])
            {
                depth[i]=depth[now]+1;
                q.push(i);
                if(i==t) return 1;
            }
        }
    }

    return 0;
}


ll dfs(ll now,ll flow)
{
    if(now==t)
    {
        maxflow+=flow;
        return flow;
    }


    ll used=0;
    for(ll i=1;i<=n;i++)
    {
        if(G[now][i]&&depth[i]==depth[now]+1)
        {
            ll tem=dfs(i,min(G[now][i],flow-used));

            G[now][i]-=tem;
            G[i][now]+=tem;

            used+=tem;
        }
    }

    return used;
}

void solve()
{
    cin>>n>>m>>s>>t;

    for(ll i=1;i<=m;i++)
    {
        ll x,y,w;
        cin>>x>>y>>w;

        G[x][y]+=w;
    }

    while(bfs())
    {
        dfs(s,inf);
    }

    cout<<maxflow<<'\n';
}
int main()
{
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int TT=1;
    //cin>>TT;
    while(TT--) solve();
    return 0;
}


posted @   纯粹的  阅读(4)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~
点击右上角即可分享
微信分享提示