Poj 1273 drainage ditches (My EK algorithm template)

Source: Internet
Author: User

Question: Give You n sides with the target position T. Next there are each side, including the start point, end point, and capacity;

Feeling: the first code of the largest stream. This question is a deep understanding of the ek algorithm. However, this question is a pitfall, that is, the case of duplicate borders =!

Idea: EK Algorithm

AC code:

#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;#define INF 100000000#define N 202int cap[N][N],flow[N][N];int p[N],a[N];int n,t;int Edmonds_Karp(int s){    int f=0;    queue<int >q;    while(1)    {        memset(a,0,sizeof(a));        a[s]=INF;        q.push(s);        while(!q.empty())        {            int u=q.front();            q.pop();            for(int v=1;v<=t;v++)            if(!a[v]&&cap[u][v]>flow[u][v])            {                p[v]=u;                q.push(v);                a[v]=min(a[u],cap[u][v]-flow[u][v]);            }        }        if(a[t]==0)break;        for(int u=t;u!=s;u=p[u])        {            flow[p[u]][u]+=a[t];            flow[u][p[u]]-=a[t];        }        f+=a[t];    }    return  f;}int main(){    int u,v,w,i,j;    while(scanf("%d %d",&n,&t)!=EOF)    {        memset(cap,0,sizeof(cap));        memset(flow,0,sizeof(flow));        for(i=0;i<n;i++)        {            scanf("%d %d %d",&u,&v,&w);            cap[u][v]+=w;        }        printf("%d\n",Edmonds_Karp(1));    }    return 0;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.