现在的位置: 首页 > 综合 > 正文

POJ 1273 Drainage Ditches(Networks flow)

2013年12月07日 ⁄ 综合 ⁄ 共 1258字 ⁄ 字号 评论关闭

很水的网络流,要注意下建图的时候是+= 而不是直接赋值,考虑到有重复路径

#include <cstdio>
#include <string.h>
#include <queue>
#define min(a,b) ((a)>(b))?(b):(a)
using namespace std ;

const int maxn=205;
const int Inf=0x7fffffff;
int m,n;
int flow[maxn][maxn],cap[maxn][maxn],cf[maxn],p[maxn];

int Ford_Fulkerson(int s,int t)
{
    memset (flow , 0 , sizeof(flow));
    queue <int> q;
    int u,v;
    int f=0;
    while (1)
    {
        memset (cf , 0 , sizeof(cf));
        cf[s]=Inf;
        q.push(s);
        memset(p, 0 , sizeof(p));
        while (!q.empty())//bfs
        {
            u=q.front();
            q.pop();
            for(v=0;v<=t;v++)
            {
                if(!cf[v] && cap[u][v]>flow[u][v])
                {
                    p[v]=u;
                    q.push(v);
                    cf[v]=min(cf[u],cap[u][v]-flow[u][v]);
                }
            }
        }
        if(cf[t]==0)break;
        for (u=t ; u!= s ; u=p[u])
        {
            flow [p[u]][u]+=cf[t];
            flow [u][p[u]]-=cf[t];
        }
        f+=cf[t];
        //printf("%d  %d\n",f,cf[t]);
    }
    return f;
}

int main ()
{
    int u,v,w;
    while (~scanf("%d %d",&m,&n))
    {
        memset(cap , 0 , sizeof(cap));
        for (int i=0 ; i<m ; ++i)
        {
            scanf("%d%d%d",&u,&v,&w);
            cap[u-1][v-1]+=w;//输入路径可以重复 这样处理比较好
        }
        int ans=Ford_Fulkerson(0,n-1);
        printf("%d\n",ans);
    }
    return 0;
}

 

抱歉!评论已关闭.