POJ 1273 Drainage Ditches 网络流基础题

题目扫了一眼,直接EK求解,1A。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <stack>
#include <map>
#include <iomanip>
#define PI acos(-1.0)
#define Max 205
#define inf 1<<28
#define LL(x) (x<<1)
#define RR(x)(x<<1|1)
using namespace std;

int Map[Max][Max];
int flow[Max];
int start,end;
int q[Max*100];
int path[Max];
int bfs()
{
    flow[1]=inf;
    int num=0,cnt=0;
    q[num++]=1;
    memset(path,-1,sizeof(path));
    path[1]=1;
    int n=end;
    while(num>cnt)
    {
        int temp=q[cnt++];
        if(temp==end)break;
        for(int i=1;i<=n;i++)
        {
            if(path[i]==-1&&Map[temp][i])
            {
                flow[i]=min(flow[temp],Map[temp][i]);
                q[num++]=i;
                path[i]=temp;
            }
        }
    }
    if(path[end]==-1)return -1;
    return flow[end];
}
long long  Edmonds_Karp()
{
    int now,step,pre;
    long long max_flow=0;
    while((step=bfs())!=-1)
    {
        now=end;
        max_flow+=step;
        while(now!=start)
        {
            pre=path[now];
            Map[pre][now]-=step;
            Map[now][pre]+=step;
            now=pre;
        }
    }
    return max_flow;
}
int main()
{
    int i,j,k,l,n,m,a,b;
    while(scanf("%d%d",&m,&n)!=EOF)
    {
        memset(Map,0,sizeof(Map));
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&l);
            Map[a][b]+=l;
        }
        start=1,end=n;
        printf("%lld\n",Edmonds_Karp());
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值