• HDU 3549 Flow Problem (最大流ISAP)


    Flow Problem

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


     

    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的最大流

    思路:裸题,ISAP。。原来之前自己写的模版有点疏漏了。。

    #include 
    #include 
    #include 
    #include 
    #define FOR(i,n) for(i=1;i<=(n);i++)
    using namespace std;
    const int INF = 1e9;
    const int N = 1010;
    
    struct Edge{
        int from,to,cap,flow;
    };
    
    struct ISAP{
        int n,m,s,t;
        int p[N],num[N];
        vector edges;
        vector G[N];
        bool vis[N];
        int d[N],cur[N];
        void init(int _n,int _m)
        {
            n=_n; m=_m;
            int i;
            edges.clear();
            FOR(i,n)
            {
                G[i].clear();
                d[i]=INF;
            }
        }
        void AddEdge(int from,int to,int cap)
        {
            edges.push_back((Edge){from,to,cap,0});
            edges.push_back((Edge){to,from,0,0});
            m = edges.size();
            G[from].push_back(m-2);
            G[to].push_back(m-1);
        }
        bool BFS()
        {
            memset(vis,0,sizeof(vis));
            queue Q;
            Q.push(t);
            d[t]=0;
            vis[t]=1;
            while(!Q.empty())
            {
                int x = Q.front(); Q.pop();
                for(unsigned i=0;ie.flow)
                    {
                        vis[e.from]=1;
                        d[e.from] = d[x]+1;
                        Q.push(e.from);
                    }
                }
            }
            return vis[s];
        }
        int Augment()
        {
            int x=t, a=INF;
            while(x!=s)
            {
                Edge& e = edges[p[x]];
                a = min(a,e.cap-e.flow);
                x = edges[p[x]].from;
            }
            x = t;
            while(x!=s)
            {
                edges[p[x]].flow+=a;
                edges[p[x]^1].flow-=a;
                x=edges[p[x]].from;
            }
            return a;
        }
        int Maxflow(int _s,int _t)
        {
            s=_s; t=_t;
            int flow = 0, i;
            BFS();
            if(d[s]>=n) return 0;
            memset(num,0,sizeof(num));
            memset(p,0,sizeof(p));
            FOR(i,n) if(d[i]e.flow && d[x]==d[e.to]+1)
                    {
                        ok=1;
                        p[e.to]=G[x][i];
                        cur[x]=i;
                        x=e.to;
                        break;
                    }
                }
                if(!ok)
                {
                    int m=n-1;
                    for(unsigned i=0;ie.flow) m=min(m,d[e.to]);
                    }
                    if(--num[d[x]]==0) break;
                    num[d[x]=m+1]++;
                    cur[x]=0;
                    if(x!=s) x=edges[p[x]].from;
                }
            }
            return flow;
        }
    };
    
    ISAP isap;
    
    void run()
    {
        int n,m,u,v,c;
        scanf("%d%d",&n,&m);
        isap.init(n,m);
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&c);
            isap.AddEdge(u,v,c);
            //isap.AddEdge(v,u,c);
        }
        static int cas = 1;
        printf("Case %d: %d\n",cas++,isap.Maxflow(1,n));
    }
    
    int main()
    {
        freopen("case.txt","r",stdin);
        int _;
        scanf("%d",&_);
        while(_--)
            run();
        return 0;
    }

  • 相关阅读:
    Tablesaw——Java数据框架和可视化库,Java中的 “Pandas”
    web与Shiro整合(RBAC模型+C3p0+Servlet)
    行情分析——加密货币市场大盘走势(10.20)
    《你的第一本哲学书》- 他人的意识
    多语言多商户多货币跨境电商商城源码(一键铺货\订单返现商城源码搭建开发)
    基于BS架构考试系统的设计与分析
    从0到1,申请cos服务器并上传图片到cos文件服务器
    .NET 全能 Cron 表达式解析库(支持 Cron 所有特性)
    skywalking部署
    Java反射机制
  • 原文地址:https://blog.csdn.net/xxpr_ybgg/article/details/127854858