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

    Flow Problem

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

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

    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
     
     题解:裸地网络流求1到n
    #include <bits/stdc++.h>
    
    using namespace std;
    #define INF 100000008
    const int maxn = 20,maxm = 4005;
    
    
    struct Netflow{
        int S,T,tot,head[maxn],dis[maxn],hh[maxn];
        bool vis[maxn];
        struct edge{
            int to,f,nxt;
        }G[maxm];
        void init(int s, int t){
            tot = 1;
            memset(head,0,sizeof(head));
            memset(head,0,sizeof(head));
            S = s, T = t;
        }
        void add(int u,int v,int w){
            G[++tot].nxt = head[u];
            G[tot].to = v;
            G[tot].f = w;
            head[u] = tot;
            
            G[++tot].nxt = head[v];
            G[tot].to = u;
            G[tot].f = 0;
            head[v] = tot;
        }
        bool bfs(){
            memset(vis,0,sizeof(vis));
            memset(dis,0,sizeof(dis));
            vis[S] = 1;
            queue <int> q;
            q.push(S);
            while(!q.empty()){
                int u = q.front();
                q.pop();
                for(int i = head[u]; i; i = G[i].nxt){
                    int v = G[i].to;
                    if(!vis[v] && G[i].f){
                        vis[v] = 1;
                        dis[v] = dis[u] + 1;
                        q.push(v);
                    }  
                }
            } 
            return vis[T];
        }
        int dfs(int u,int v){
            if(u == T || !v )return v;
            int ret = 0;
            for(int i = hh[u]; i; i = G[i].nxt){
                int p = G[i].to;
                if(G[i].f && dis[p] == dis[u] + 1){
                    int dd = dfs(p, min(v, G[i].f));
                    G[i].f -= dd;
                    G[i^1].f += dd;
                    v-=dd;
                    ret += dd;
                   hh[u] = i;
                }
            }
            if(!ret)dis[u]=-1;
            return ret;
        }
        int dinic(){
            int ans = 0;
            while(bfs()){
                for(int i = S; i <= T; i++)hh[i] = head[i];
                ans += dfs(S,INF);
            }
            return ans;
        }    
    };
    Netflow Tr;
    int main(){
        int T;
        scanf("%d",&T);
        for(int k = 1; k <= T; k++){
            int n,m;
            scanf("%d%d",&n,&m);
            Tr.init(1,n);
            for(int i = 1; i <= m; i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                Tr.add(u,v,w);
            }
            printf("Case %d: %d
    ",k,Tr.dinic());
            
        }
    }
  • 相关阅读:
    ACM集训的第一题
    python作用域
    python——异常类型
    PycharmV2017 1.x使用说明
    python之路——面向对象(进阶篇)
    python之路——面向对象(基础篇)
    python学习之路——基础篇(3)模块(续)
    python之路——基础篇(2)模块
    Python学习之路——基础篇(1)字符串格式化
    初学设计模式:策略模式
  • 原文地址:https://www.cnblogs.com/EdSheeran/p/8458448.html
Copyright © 2011-2022 走看看