• hdoj 3549 Flow Problem 【最大流】


    题目:hdoj 3549 Flow Problem

    题意:给出一个图,让你求最大流

    分析:这个题目用dinci写的,因为点比较少,而dinci复杂度O(m*n^2),但是还是跑了160ms,不知道15的神牛怎么写的。

    dinci的写法要注意的地方就是存图的时候要考虑怎么存,因为要更新网络残量,即反向的流量,所以这里要注意一下。

    思想就不讲了,很多地方有讲。

    代码:

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    #define Del(a,b) memset(a,b,sizeof(a))
    const int N = 20;
    const int inf = 0x3f3f3f3f;
    int n,m;
    struct Node
    {
        int from,to,cap,flow;
    };
    vector v[N];
    vector e;
    int vis[N];  //
    void add_Node(int from,int to,int cap)
    {
        e.push_back((Node){from,to,cap,0});
        e.push_back((Node){to,from,0,0});
        int tmp=e.size();
        v[from].push_back(tmp-2);
        v[to].push_back(tmp-1);
    }
    bool bfs(int s,int t)
    {
        Del(vis,-1);
        queue q;
        q.push(s);
        vis[s] = 0;
        while(!q.empty())
        {
            int x=q.front();
            q.pop();
            for(int i=0;itmp.flow)  //第二个条件保证
                {
                    vis[tmp.to]=vis[x]+1;
                    q.push(tmp.to);
                }
            }
        }
        if(vis[t]>0)
            return true;
        return false;
    }
    int dfs(int o,int f,int t)
    {
        if(o==t || f==0)
            return f;
        int a = 0,ans=0;
        for(int i=0;i0)
            {
                tmp.flow+=a;
                e[v[o][i]^1].flow-=a; //存图方式
                ans+=a;
                f-=a;
            }
        }
        return ans;  //优化
    }
    int dinci(int s,int t)
    {
        int ans=0;
        while(bfs(s,t))
        {
            int tm=dfs(s,inf,t);
            //printf("%d\n",tm);
            ans+=tm;
        }
        return ans;
    }
    int main()
    {
        //freopen("Input.txt","r",stdin);
        int T;
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++)
        {
            scanf("%d%d",&n,&m);
            for(int i=0;i
                    
  • 相关阅读:
    Go 使用mencached缓存
    设计模式-行为型-迭代器模式
    Dubbo-Adaptive实现原理
    Centos7使用Nginx配置HTTPS正向代理
    Python 搭建虚拟环境
    MySQL8.0学习笔记
    Web自动化测试-Selenium语法入门到精通
    在Python中发送UDP数据包
    外包干了3个多月,技术退步明显。。。。。
    已有yarn集群部署spark
  • 原文地址:https://blog.csdn.net/G11176593/article/details/127855261