POJ 1273 Drainage Ditches 网络流基础题

来源:互联网 发布:安卓时间校准软件 编辑:程序博客网 时间:2024/05/29 17:29

题目扫了一眼,直接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;}