zoukankan      html  css  js  c++  java
  • HDU 3549 Flow problem

    Flow Problem

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

    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:  1532 3572 3416 3081 3491 

    第一次做网络流的题目,求一个最大流

    用 Dinic 算法 ,有向图 , 反向弧的容量设为 0 。

    每次用 bfs 构建层次图, 若果 vis[ t ] == 1 的话证明图上还是有增广路 。

    然后用 递归dfs 寻找增广路 。

    dfs 过程除了当前结点  x 以外 , 还需要传入 一个表示“ 目前为止所有弧的最小残量 ” 的 a , 

    当 x 为汇点 或者 a = 0的时候终止dfs 过程 ,否则多路增广 。

    有一个重要的优化 就是保存每一个结点 x 正在考虑的 弧 cur[x] , 以避免重复计算 。( dfs 更新增广路之前把eh 复制给 cur 就ok 了)

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    const int INF = 1e9 ;
    const int N = 20;
    const int M = 2010;
    
    bool vis[N];
    int eh[N],et[M],nxt[M],ef[M],ec[M],etot;
    int s , t , n , m;
    int d[N],cur[N];
    void init(){
        memset( eh, -1 , sizeof eh) ;
        etot =0 ;
    }
    void addedge( int u , int v , int c ,int f){
        et[etot] = v ; nxt[etot] = eh[u]; ef[etot] = f; ec[etot] = c ; eh[u] = etot++;
        et[etot] = u ; nxt[etot] = eh[v]; ef[etot] = f; ec[etot] = 0 ; eh[v] = etot++;
    }
    
    bool bfs ()
    {
        memset( vis , 0 , sizeof vis);
        queue< int >que;
        que.push(s) ;
        d[s] = 0;
        vis[s] =1 ;
        while( !que.empty() ) {
            int u = que.front(); que.pop();
            for( int i = eh[ u ] ; ~i ; i = nxt[i] ){
                int v = et[i] , c = ec[i] , f = ef[i];
                if( !vis[v] && c > f){
                    vis[v] = 1 ;
                    d[v] = d[u] + 1;
                    que.push(v);
                }
            }
        }
        return vis[t];
    }
    
    int dfs (int x ,int a)
    {
        if ( x == t || a == 0 ){
            return a ;
        }
        int flow = 0 , F;
        for( int &i = cur[x] ; ~i ; i = nxt[i] ){
            int v = et[i] , c = ec[i] , &f = ef[i];
            if( d[x] + 1 == d[v] && ( F = dfs (v, min( a, c - f))) > 0) {
                f += F;
                ef[ i ^ 1 ] -= F;
                flow += F;
                a -= F;
                if( a == 0 )break;
            }
        }
        return flow;
    }
    
    int MF(){
        int flow = 0 ;
        while( bfs() ){
            memcpy( cur , eh , sizeof eh );
            flow += dfs(s , INF);
        }
        return flow;
    }
    
    void run()
    {
        int u,v,c;
    
        scanf("%d%d",&n,&m);
    
        init();
    
        while(m--){
            scanf("%d%d%d",&u, &v, &c);
            addedge( u ,v , c , 0 );
        }
        s = 1 , t = n;
        printf("%d
    ",MF());
    }
    
    int main()
    {
        #ifdef LOCAL
            freopen("in.txt","r",stdin);
        #endif // LOCAL
        int _ , cas = 1 ;
        scanf("%d",&_);
        while(_--){
            printf("Case %d: ",cas++);
            run();
        }
        return 0;
    }
    only strive for your goal , can you make your dream come true ?
  • 相关阅读:
    java里的分支语句--程序运行流程的分类(顺序结构,分支结构,循环结构)
    Java里的构造函数(构造方法)
    Java里this的作用和用法
    JAVA中的重载和重写
    从键盘接收字符类型的数据并实现剪刀石头布的规则
    使用Notepad++编码编译时报错(已解决?)
    云就是网络,云计算呢
    使用JavaMail创建邮件和发送邮件
    mysql锁机制
    java中几种常用的设计模式
  • 原文地址:https://www.cnblogs.com/hlmark/p/3940655.html
Copyright © 2011-2022 走看看