HDU 3549 Flow Problem

Source: Internet
Author: User

N is the given vertex, M is the edge, and the following m rows are each edge.

Idea: I am still using the ek algorithm. Now I only know this algorithm. I use C ++ for more than 4000 Ms. If you are interested, please do not use this algorithm! Below is a multi-channel augmented code, which can be used for reference, 78 Ms

My AC code:

#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>using namespace std;#define INF 100000000#define N 1000int cap[N][N],flow[N][N];int p[N],a[N];int m,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,n,cnt=1;    scanf("%d",&n);    while(n--)    {        scanf("%d %d",&t,&m);        memset(cap,0,sizeof(cap));        memset(flow,0,sizeof(flow));        for(i=0;i<m;i++)        {            scanf("%d %d %d",&u,&v,&w);            cap[u][v]+=w;        }        printf("Case %d: %d\n",cnt++,Edmonds_Karp(1));    }    return 0;}

Multi-channel augmented:

#include <cstdio>#include <string.h>#include <algorithm>using namespace std;const int NMax=5000;struct edge{    int num,len;    edge *next,*rev;}*S[NMax],pool[NMax];int N,M,level[NMax],Q[NMax];bool makelevel(){    memset(level,-1,sizeof(level));    int tmp;    Q[0]=1;    level[1]=0;    for(int i=0,bot=1;i<bot;i++)    {        tmp=Q[i];        for(edge *p=S[tmp];p;p=p->next)            if(p->len>0 && level[p->num]==-1)                level[Q[bot++]=p->num]=level[tmp]+1;    }    return level[N]!=-1;}int DFS(int a,int alpha){    if(a==N) return alpha;    int tot=0,tmp;    for(edge *p=S[a];p && tot<alpha;p=p->next)    {        if(p->len>0 && level[p->num]==level[a]+1)        {            if(tmp=DFS(p->num,min(alpha-tot,p->len)))            {                tot+=tmp;                p->len-=tmp;                if(p->rev) p->rev->len+=tmp;            }        }    }    if(!tot) level[a]=-1;    return tot;}int main(){    int x,y,z,L,tmp;    int T;    scanf("%d",&T);    for(int I=1;I<=T;I++)    {        scanf("%d%d",&N,&M);        L=0;        memset(S,0,sizeof(S));        for(int i=1;i<=M;i++)        {            scanf("%d%d%d",&x,&y,&z);            edge *p=&pool[L++],*q=&pool[L++];            p->num=y; p->len=z; p->next=S[x];            q->num=x; q->len=0; q->next=S[y];            p->rev=q; q->rev=p;            S[x]=p; S[y]=q;        }        int ans=0;        while(makelevel())            while(tmp=DFS(1,(~0u>>1)))                ans+=tmp;        printf("Case %d: %d\n",I,ans);    }    getchar(); getchar();    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.