• [模板][最大流]dinic


     最大流 = 最小割

    #pragma GCC optimize(2)
    #include <bits/stdc++.h>
    #define m(x) memset(x, 0, sizeof(x))
    using namespace std;
    typedef long long ll;
    
    const int MAXN = 1e6 + 10;
     
    ll inf = ll(1) << 29;
    
    struct MF
    {
    	const static int maxn = 1e6 + 10;
    	ll n, m, s, t, maxflow;
    	ll head[maxn], ver[maxn << 1], edge[maxn << 1], Next[maxn << 1], d[maxn], tot;
    	
    	void init()
    	{
    		tot = 1;
    		memset(head, 0, sizeof(head));
    	}
    	
    	void add(ll x, ll y, ll z)
    	{
    		ver[++tot] = y, edge[tot] = z, Next[tot] = head[x], head[x] = tot;
    		ver[++tot] = x, edge[tot] = 0, Next[tot] = head[y], head[y] = tot;
    	}
    	
    	bool bfs() 
    	{
    		memset(d, 0, sizeof(d));
    		queue <int> q;
    		q.push(s); d[s] = 1;
    		while(!q.empty())
    		{
    			int x = q.front(); q.pop();
    			for(int i = head[x]; i; i = Next[i])
    			{
    				if(edge[i] && !d[ver[i]])
    				{
    					q.push(ver[i]);
    					d[ver[i]] = d[x] + 1;
    					if(ver[i] == t)
    						return true;
    				}
    			}
    		}
    		return false;
    	}
    	
    	ll dinic(ll x, ll flow)
    	{
    		if(x == t) return flow;
    		ll rest = flow, k;
    		for(ll i = head[x]; i && rest; i = Next[i])
    		{
    			if(edge[i] && d[ver[i]] == d[x] + 1)
    			{
    				k = dinic(ver[i], min(rest, edge[i]));
    				if(!k) d[ver[i]] = 0;
    				edge[i] -= k;
    				edge[i ^ 1] += k;
    				rest -= k;
    			}
    		}
    		return flow - rest;
    	}
    	
    	ll cal()
    	{
    		ll flow = 0, maxflow = 0;
    		while(bfs())
    			while(flow = dinic(s, inf)) maxflow += flow;
    		return maxflow;
    	}
    };
     
    int main()
    {
    
        ios::sync_with_stdio(false);
    
        cin.tie(0);     cout.tie(0);
        
        MF M;
        M.init();
        
    	cin >> M.n >> M.m >> M.s >> M.t;
    	
        ll a, b, c;
    	
        for(int i = 0; i < M.m; ++i)
        {
        	cin >> a >> b >> c;
        	M.add(a, b, c);
    	}
    	
    	cout << M.cal() << '
    ';		
    
        return 0;
    }
  • 相关阅读:
    APC 注入
    远程线程注入突破SESSION 0
    .Net审计之.Net Json反序列化
    PHP审计之BEESCMS审计案例
    Thinkphp5学习笔记
    mysql 必知必会整理—触发器[十五]
    mysql 必知必会整理—游标[十四]
    mysql 必知必会整理—存储过程[十三]
    mysql 必知必会整理—视图[十二]
    mysql 必知必会整理—表[十一]
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270355.html
Copyright © 2020-2023  润新知