hdu 3549 Flow Problem(最大流 EK,sap)

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



题意:首先给定有多少组测试数据,然后给定一个顶点数,边数,然后给定一个单向的流量,求从第一个点到最后一个点的最大流量。注意该流网络是有向图;


最大流基础题,可以ek或者sap算法;

代码:

ek算法

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
using namespace std;
int n,m;
const int N=20;
int map[N][N];
int pre[N],vis[N];
int bfs(int s,int t)
{
    memset(pre,0,sizeof(pre));
    memset(vis,0,sizeof(vis));
    queue<int>que;
    que.push(s);
    while(que.size())
    {
        int q=que.front();
        que.pop();
        if(q==t)
            return 1;
        for(int i=1;i<=t;i++)
        {
            if(map[q][i]&&!vis[i])
            {
                pre[i]=q;
                vis[i]=1;
                que.push(i);
            }
        }
    }
    return 0;
}
int ek(int s,int t)
{
    int min=0;
    while(bfs(s,t))
    {
        int minn=0xfffffff;
        for(int i=t;i!=s;i=pre[i])
        {
            if(minn>map[pre[i]][i])
                minn=map[pre[i]][i];
        }
        min+=minn;
        for(int i=t;i!=s;i=pre[i])
        {
            map[pre[i]][i]-=minn;
            map[i][pre[i]]+=minn;
        }
    }
    return min;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        for(int i=1;i<=m;i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            map[a][b]+=w;
        }
        printf("Case %d: ",t);
        printf("%d\n",ek(1,n));
    }
}

sap


#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=20;
int leve[N];
int n,m;
int map[N][N],gap[N],pre[N];
int sap(int s,int t)
{
    memset(pre,0,sizeof(pre));
    memset(gap,0,sizeof(gap));
    memset(leve,0,sizeof(leve));
    int u;
    int min=0;
    u=pre[s]=s;
    gap[0]=t;
    while(leve[s]<t)
    {
        int v;
        for(v=1;v<=n;v++)
            if(map[u][v]&&leve[u]==leve[v]+1)
                break;
        if(v<=n)
        {
            pre[v]=u;
            u=v;
            int minn=0xfffffff;
            if(v==n)
            {
                for(int i=t;i!=s;i=pre[i])
                    if(minn>map[pre[i]][i])
                        minn=map[pre[i]][i];
                min+=minn;
                for(int i=t;i!=s;i=pre[i])
                {
                    map[pre[i]][i]-=minn;
                    map[i][pre[i]]+=minn;
                }
                u=s;
            }
        }
        else
        {
            int minleve=t;
            for(int i=1;i<=n;i++)
                 if(minleve>leve[i]&&map[u][i])
                     minleve=leve[i];
            gap[leve[u]]--;
            if(gap[leve[u]]==0)
                break;
            leve[u]=minleve+1;
            gap[leve[u]]++;
            u=pre[u];
        }
    }
    return min;
}
int main()
{
    int T;
    scanf("%d",&T);
    for(int t=1;t<=T;t++)
    {
        scanf("%d%d",&n,&m);
        memset(map,0,sizeof(map));
        for(int i=1;i<=m;i++)
        {
            int a,b,w;
            scanf("%d%d%d",&a,&b,&w);
            map[a][b]+=w;
        }
        printf("Case %d: ",t);
        printf("%d\n",sap(1,n));
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值