zoukankan      html  css  js  c++  java
  • HDU 3549 Dinic

    Flow Problem

    Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 8946    Accepted Submission(s): 4205


    Problem Description
    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
     
    Author
    HyperHexagon
     
    Source
     
    Recommend
    zhengfeng   |   We have carefully selected several similar problems for you:  3572 3416 3081 3491 1533 
     跟上一道题一样。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cstring>
    #include<vector>
    #include<queue>
    #include<algorithm>
    using namespace std;
    const int inf=0x3fffffff;
    const int maxn=207;
    struct Edge
    {
        int cap,flow;
    };
    int n,m,s,t;
    Edge edges[maxn][maxn];
    bool vis[maxn];
    int d[maxn];
    int cur[maxn];
    void init()
    {
              memset(edges,0,sizeof(edges));
    }
    bool BFS()
    {
        memset(vis,0,sizeof(vis));
        queue<int>Q;
        Q.push(s);
        d[s]=0;
        vis[s]=1;
        while(!Q.empty()){
            int x=Q.front();Q.pop();
            for(int i=1;i<=n;i++){
                Edge &e=edges[x][i];
                if(!vis[i]&&e.cap>e.flow){
                    vis[i]=1;
                    d[i]=d[x]+1;
                    Q.push(i);
                }
            }
        }
        return vis[t];
    }
    int DFS(int u,int cp)
    {
        int tmp=cp;
        int v,t;
        if(u==n)
            return cp;
        for(v=1;v<=n&&tmp;v++)
        {
            if(d[u]+1==d[v])
            {
                if(edges[u][v].cap>edges[u][v].flow)
                {
                    t=DFS(v,min(tmp,edges[u][v].cap-edges[u][v].flow));
                    edges[u][v].flow+=t;
                    edges[v][u].flow-=t;
                    tmp-=t;
                }
            }
        }
        return cp-tmp;
    }
    int Maxflow()
    {
        int flow=0;
        while(BFS()){
            memset(cur,0,sizeof(cur));
            flow+=DFS(s,inf);
        }
        return flow;
    }
    int main()
    {
      // freopen("in.txt","r",stdin);
       int tt,cnt=1;
       scanf("%d",&tt);
        int a,b, c;
        while(tt--){
            scanf("%d%d",&n,&m);
            init();
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                edges[a][b].cap+=c;
            }
            s=1;t=n;
            int ans=Maxflow();
            printf("Case %d: %d
    ",cnt++,ans);
        }
        return 0;
    }
  • 相关阅读:
    jemter 90%line的解释
    JMeter 不同线程组间变量传递(亲测 ok)
    nginx的url规则小tips
    redis水平扩展实践,完全配置,无需代码改动
    基于nginx + lua实现的反向代理动态更新
    基于redis 3.x搭建集群环境
    简单监控脚本细节分析
    简单的应用服务器性能监控
    Elasticsearch的脚本化数据导入导出
    A* search算法
  • 原文地址:https://www.cnblogs.com/codeyuan/p/4262867.html
Copyright © 2011-2022 走看看