hdu1532

/*
分析:
    网络流果题~~~
    秉承绝不抄袭、自己debug的原则,经过两个月的努力,终于
于今天这个值得纪念的日子里,检查出了那一~个小bug,ac掉了
俺第一个网络流。(我是该高兴还是该桑心~-、-III)
    泪奔~~~(可以安心吃午饭上体育课去了@。@。。。)

                               2012-10-09  15:23
*/








#include"stdio.h"  
#include"string.h"  
#include"queue"  
using namespace std;  
int v,e;  
int flow[211][211];  
int rest[211][211];  
int pre[211],hash[211];  

struct node  
{  
    int dot;  
};  
int min(int a,int b)  
{  
    return a>b?b:a;  
}
int BFS()  
{  
    queue<node>q;  
    node now,next;  
    int i;  

    memset(pre,-1,sizeof(pre));  
    memset(hash,0,sizeof(hash));
    now.dot=1;  
    hash[1]=1;
    q.push(now);  

    while(!q.empty())  
    {  
        now=q.front();  
        q.pop();  
        if(now.dot==v)  return 1;  
        for(i=1;i<=v;i++)  
        {
			if(!hash[i] && rest[now.dot][i])
			{
				pre[i]=now.dot;
				hash[i]=1;
				next.dot=i;
				q.push(next);
			}
        }
    }  
    return 0;  
}  
int update()  
{  
    int t,p;  

    p=111111111;
    t=v;
	while(t!=1)  
    {
		p=min(p,rest[pre[t]][t]);
        t=pre[t];  
    }  
    t=v;  
    while(t!=1)  
    {
		rest[pre[t]][t]-=p;
		rest[t][pre[t]]+=p;
        t=pre[t];  
    }
    return p;  
}  
int main()  
{  
    int a,b,c;  
    __int64 ans;  
    while(scanf("%d%d",&e,&v)!=-1)  
    {  
        memset(flow,0,sizeof(flow));  
        memset(rest,0,sizeof(rest));  
        while(e--)  
        {  
            scanf("%d%d%d",&a,&b,&c);  
			rest[a][b]+=c;
        }  
  
        ans=0;  
        while(BFS())    ans+=update();  
        printf("%I64d\n",ans);  
    }  
    return 0;  
}


你可能感兴趣的:(hdu1532)