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)

    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
     
    套版就行了,用了2种方法,Edmonds_Karp 和 SAP
    /* ****************************************************
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<queue>
    using namespace std;
    //****************************************************
    //最大流模板Edmonds_Karp算法
    //初始化:G[][],st,ed
    //******************************************************
    const int MAXN = 30;
    const int INF = 0x3fffff;
    int G[MAXN][MAXN];
    int path[MAXN],flow[MAXN],st,ed;
    int n,m;
    
    queue<int> Q;
    int bfs()
    {
        int t;
        while(!Q.empty()) Q.pop();
        memset(path,-1,sizeof(path));
        path[st]=0;
        flow[st]=INF;
        Q.push(st);
        while(!Q.empty()){
            t=Q.front();
            Q.pop();
            if(t==ed) break;
            for(int i=1;i<=n;i++){
                if(i!=st&&path[i]==-1&&G[t][i]){
                    flow[i]=flow[t]<G[t][i]?flow[t]:G[t][i];
                    Q.push(i);
                    path[i]=t;
                }
            }
        }
        if(path[ed]==-1) return -1;
        return flow[ed];
    }
    
    int Edmonds_Karp()
    {
        int max_flow=0;
        int step,now,pre;
        while((step=bfs())!=-1){
            max_flow+=step;
            now=ed;
            while(now!=st){
                pre=path[now];
                G[pre][now]-=step;
                G[now][pre]+=step;
                now=pre;
            }
        }
        return max_flow;
    }
    
    int main()
    {
        int T;
        int iCase=0;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            iCase++;
            st=1,ed=n;
            memset(G,0,sizeof(G));
            for(int i=1;i<=m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                G[u][v]+=w;
            }
            printf("Case %d: %d
    ",iCase,Edmonds_Karp());
        }
    
        return 0;
    }
    
    ******************************************************* */
    
    /* *******************************************************
    #include<stdio.h>
    #include<string.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    //****************************************************
    //最大流模板  SAP算法
    //邻接表形式
    //******************************************************
    
    const int MAXN = 30;//点数的最大值
    const int MAXM = 5000;//边数的最大值
    const int INF = 0x3f3f3f3f;
    
    struct Node
    {
        int from,to,next;
        int cap;
    }edge[MAXM];
    int tol;
    int head[MAXN];
    int dep[MAXN];
    int gap[MAXN];//gap[x]=y :说明残留网络中dep[i]==x的个数为y
    
    int n;//n是总的点的个数,包括源点和汇点
    
    void init()
    {
        tol=0;
        memset(head,-1,sizeof(head));
    }
    
    void addedge(int u,int v,int w)
    {
        edge[tol].from=u;
        edge[tol].to=v;
        edge[tol].cap=w;
        edge[tol].next=head[u];
        head[u]=tol++;
        edge[tol].from=v;
        edge[tol].to=u;
        edge[tol].cap=0;
        edge[tol].next=head[v];
        head[v]=tol++;
    }
    
    void BFS(int start,int end)
    {
        memset(dep,-1,sizeof(dep));
        memset(gap,0,sizeof(gap));
        gap[0]=1;
        int que[MAXN];
        int front,rear;
        front=rear=0;
        dep[end]=0;
        que[rear++]=end;
        while(front!=rear)
        {
            int u=que[front++];
            if(front==MAXN)front=0;
            for(int i=head[u];i!=-1;i=edge[i].next)
            {
                int v=edge[i].to;
                if(dep[v]!=-1)continue;
                que[rear++]=v;
                if(rear==MAXN)rear=0;
                dep[v]=dep[u]+1;
                ++gap[dep[v]];
            }
        }
    }
    
    int SAP(int start,int end)
    {
        int res=0;
        BFS(start,end);
        int cur[MAXN];
        int S[MAXN];
        int top=0;
        memcpy(cur,head,sizeof(head));
        int u=start;
        int i;
        while(dep[start]<n)
        {
            if(u==end)
            {
                int temp=INF;
                int inser;
                for(i=0;i<top;i++)
                   if(temp>edge[S[i]].cap)
                   {
                       temp=edge[S[i]].cap;
                       inser=i;
                   }
                for(i=0;i<top;i++)
                {
                    edge[S[i]].cap-=temp;
                    edge[S[i]^1].cap+=temp;
                }
                res+=temp;
                top=inser;
                u=edge[S[top]].from;
            }
            if(u!=end&&gap[dep[u]-1]==0)//出现断层,无增广路
              break;
            for(i=cur[u];i!=-1;i=edge[i].next)
               if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)
                 break;
            if(i!=-1)
            {
                cur[u]=i;
                S[top++]=i;
                u=edge[i].to;
            }
            else
            {
                int min=n;
                for(i=head[u];i!=-1;i=edge[i].next)
                {
                    if(edge[i].cap==0)continue;
                    if(min>dep[edge[i].to])
                    {
                        min=dep[edge[i].to];
                        cur[u]=i;
                    }
                }
                --gap[dep[u]];
                dep[u]=min+1;
                ++gap[dep[u]];
                if(u!=start)u=edge[S[--top]].from;
            }
        }
        return res;
    }
    
    
    int main()
    {
        int st,ed;
        int T;
        int iCase=0;
        int m;
        scanf("%d",&T);
        while(T--){
            scanf("%d%d",&n,&m);
            iCase++;
            st=1,ed=n;
            init();
            for(int i=1;i<=m;i++){
                int u,v,w;
                scanf("%d%d%d",&u,&v,&w);
                addedge(u,v,w);
            }
            printf("Case %d: %d
    ",iCase,SAP(st,ed));
        }
        return 0;
    }
    * ****************************************************** */
  • 相关阅读:
    机器学习-数据与特征工程
    机器学习-聚类(clustering)算法:K-means算法
    机器学习-回归中的相关度和R平方值
    机器学习-非线性回归(Logistic Regression)及应用
    机器学习-多元线性回归(一)
    机器学习-简单线性回归(二)
    MVC,MVP,MVVM
    Git从入门到熟练
    NSNotificationCenter
    FMDB的线程安全
  • 原文地址:https://www.cnblogs.com/wangdongkai/p/5611134.html
Copyright © 2011-2022 走看看