zoukankan      html  css  js  c++  java
  • HDU

    Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph.
    Input
    The first line of input contains an integer T, denoting the number of test cases.
    For each test case, the first line contains two integers N and M, denoting the number of vertexes and edges in the graph. (2 <= N <= 15, 0 <= M <= 1000)
    Next M lines, each line contains three integers X, Y and C, there is an edge from X to Y and the capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)
    Output
    For each test cases, you should output the maximum flow from source 1 to sink N.
    Sample Input
    2
    3 2
    1 2 1
    2 3 1
    3 3
    1 2 1
    2 3 1
    1 3 1
    Sample Output
    Case 1: 1
    Case 2: 2

    题意:输入t组数据,每组数据输入n个节点。m条边。输出从源点1到n的最大流量

    简单最大流模板题。套个模板即可。

    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=1e4+10;
    const int inf=0x7fffffff;
    struct edge///链式前向星建图
    {
        int to,val,rev;///rev用于取反边
        edge(){}
        edge(int a,int b,int c)
        {
            to=a;
            val=b;
            rev=c;
        }
    };
    vector<edge>mp[maxn];
    void add(int from,int to,int val)///加边
    {
        mp[from].push_back(edge(to,val,mp[to].size()));///正边,rev为to的最新边
        mp[to].push_back(edge(from,0,mp[from].size()-1));///反边,与正边的rev为正边的标号
    }
    int t,n,m;
    int dep[20];///dep为dinic算法中的分层图的深度,层数为从开始到达该节点最短距离
    int bfs()
    {
        memset(dep,-1,sizeof dep);
        queue<int >q;
        while(!q.empty())q.pop();
        dep[1]=0;
        q.push(1);
        while(!q.empty())///广搜分层
        {
            int tmp=q.front();
            q.pop();
            if(tmp==n)return 1;///若广搜还能走到汇点说明可以继续増广
            for(int i=0;i<mp[tmp].size();i++)
            {
                int &to=mp[tmp][i].to,flow=mp[tmp][i].val;
                if(dep[to]==-1&&flow)///若该点还没有被分层,没有走到的标记,且有容量,那么走下去
                {
                    dep[to]=dep[tmp]+1;
                    q.push(to);
                }
            }
        }
        return 0;
    }
    int dfs(int s,int t,int flow)///多次増广
    {
        if(s==t)return flow;///返回流量
        int pre=0;
        for(int i=0;i<mp[s].size();i++)
        {
            int &to=mp[s][i].to,val=mp[s][i].val;
            if(dep[s]+1==dep[to]&&val>0)///下一次増广的节点层数应在该点基础上+1
            {
                int tmp=min(flow-pre,val);///流量和剩余容量取最小值
                int sub=dfs(to,t,tmp);///不断通过bfs向下流,且过滤流量
                mp[s][i].val-=sub;///正向边-
                mp[to][mp[s][i].rev].val+=sub;///反向边+
                pre+=sub;
                if(pre==flow)return pre;///多次増广总流量
            }
        }
        return pre;
    }///分层后每次増广多条链
    int dinic()
    {
        int ans=0;
        while(bfs())ans+=dfs(1,n,inf);
        return ans;
    }
    int main()
    {
        int cas=1;
        scanf("%d",&t);
        while(t--)
        {
            for(int i=0;i<=n;i++)mp[i].clear();
            int from,to,val;
            scanf("%d%d",&n,&m);
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&from,&to,&val);
                add(from,to,val);
            }
            printf("Case %d: %d
    ",cas++,dinic());
        }
    }
    
    
  • 相关阅读:
    解决 Windows 下的 :所选择的任务“{0}”不再存在。若要查看当前任务,请单击“刷新”。
    学习编译更好的 DAO 的技巧
    宇宙的起源演讲全文(斯蒂芬·霍金)
    java對象序列化的兩種使用方法
    Ubuntu硬盘安装与配置(3D效果)
    java異常處理
    [zt]JDBC对数据库的事务操作
    [zt]spring本地事务与JTA事务实现解析
    Debian溫習
    在oracle中增大session數量
  • 原文地址:https://www.cnblogs.com/kuronekonano/p/11135765.html
Copyright © 2011-2022 走看看