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

     Flow Problem HDU - 3549 

    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. 

    InputThe 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)OutputFor 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的结点的最大的流
    思路:用dinic
    #include<stdio.h>
    #include<iostream>
    #include<map>
    #include<string.h>
    #include<queue>
    #include<vector>
    #include<math.h>
    #include<algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    int dis[20];
    int flow[500][500];
    int n,m;
    int bfs()
    {
        memset(dis,-1,sizeof(dis));
        queue<int>Q;
        dis[1]=1;
        Q.push(1);
        while(!Q.empty())
        {
            int top=Q.front();
            Q.pop();
            for(int i=1;i<=n;i++)
            {
                if(flow[top][i]>0&&dis[i]<0)
                {
                    dis[i]=dis[top]+1;
                    Q.push(i);
                }
            }
        }
        if(dis[n]>0)return 1;
        return 0;
    }
    int dinic(int x,int k)
    {
        if(x==n)
            return k;
        int y;
        for(int i=1;i<=n;i++)
        {
            if(flow[x][i]>0&&dis[i]==dis[x]+1&&(y=dinic(i,min(k,flow[x][i]))))
            {
                flow[x][i]-=y;
                flow[i][x]+=y;
                return y;
            }
        }
        return 0;
     
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        int cas=1;
        while(t--)
        {
            memset(flow,0,sizeof(flow));
            scanf("%d%d",&n,&m);
            int a,b,c;
            for(int i=0;i<m;i++)
            {
                scanf("%d%d%d",&a,&b,&c);
                flow[a][b]+=c;
            }
            int ans=0;
            while(bfs())
            {
                //cout<<"++"<<endl;
                int res;
                while(res=dinic(1,inf))ans+=res;
            }
            printf("Case %d: %d
    ",cas++,ans);
        }
    }
    View Code

    用sap加邻接矩阵,maze那里是叠加,不是覆盖

    #include<cstdio>
    #include<iostream>
    #include<algorithm>
    #include<cstring>
    #include<sstream>
    #include<cmath>
    #include<stack>
    #include<cstdlib>
    #include <vector>
    #include<queue>
    using namespace std;
    
    #define ll long long
    #define llu unsigned long long
    #define INF 0x3f3f3f3f
    #define PI acos(-1.0)
    const int maxn =  1e3+5;
    const int  mod = 1e9+7;
    
    int maze[maxn][maxn];
    int gap[maxn],dis[maxn],pre[maxn],cur[maxn];
    
    int sap(int start,int end ,int nodenum)
    {
        memset(cur,0,sizeof cur);
        memset(dis,0,sizeof dis);
        memset(gap,0,sizeof gap);
        int u = pre[start] = start,maxflow = 0,aug = -1;
        gap[0] = nodenum;
        while(dis[start] < nodenum)
        {
            loop:
            for(int v = cur[u];v < nodenum; v++)
                if(maze[u][v] && dis[u] == dis[v] + 1){
                    if(aug == -1 || aug > maze[u][v])
                        aug = maze[u][v];
                    pre[v] = u;
                    u = cur[u] = v;
                    if(v == end)
                    {
                        maxflow += aug;
                        for(u = pre[u]; v!=start;v=u,u=pre[u])
                        {
                            maze[u][v] -= aug;
                            maze[v][u] += aug;
                        }
                        aug = -1;
                    }
                    goto loop;
                }
            int mindis = nodenum - 1;
            for(int v = 0;v<nodenum;v++)
                if(maze[u][v] && mindis > dis[v])
                {
                    cur[u] = v;
                    mindis = dis[v];
                }
            if((--gap[dis[u]]) == 0)
                break;
            gap[dis[u] = mindis + 1]++;
            u = pre[u];
    
        }
        return maxflow;
    }
    
    int main()
    {
        int t;
        scanf("%d",&t);
        for(int ca = 1;ca <= t; ca++)
        {
            memset(maze,0,sizeof maze);
            int n,m;
            scanf("%d%d",&n,&m);
            for(int i=0;i<m;i++)
            {
                int a,b,l;
                scanf("%d%d%d",&a,&b,&l);
                maze[a-1][b-1] += l;
            }
            int res = sap(0,n-1,n);
            printf("Case %d: %d
    ",ca,res);
        }
    }
    View Code
  • 相关阅读:
    MT【38】与砝码有关的两个题
    MT【37】二次函数与整系数有关的题
    MT【36】反函数有关的一道题
    MT【35】用复数得到的两组恒等式
    MT【34】正余弦的正整数幂次快速表示成正余弦的线性组合
    MT【33】证明琴生不等式
    MT【32】内外圆(Apollonius Circle)的几何证明
    MT【31】傅里叶级数为背景的三角求和
    MT【30】椭圆的第二定义解题
    MT【29】介绍向量的外积及应用举例
  • 原文地址:https://www.cnblogs.com/smallhester/p/10295624.html
Copyright © 2011-2022 走看看