最大流的Ford-Fulkerson方法初步

const int SIZE_V //= 600;
const int SIZE_E //= 600;

typedef int weight_t;

int n,m;
int start //= 1;
int end //= m;
//边的结构
struct edge_t{
    int node;
    weight_t c;//c为容量
    edge_t* next;
    edge_t* redge;//指向反向边
}Edge[SIZE_E];
int ECnt;

//图的邻接表
edge_t* V[SIZE_V];

void init(){
    ECnt = 0;
    //fill(V+1,V+m+1,(edge_t*)0);
    MEM(V,0);
}
//生成双向边
void mkEdge(int a,int b,weight_t c){
    int t1 = ECnt++;
    int t2 = ECnt++;

    Edge[t1].node = b;
    Edge[t1].c = c;
    Edge[t1].next = V[a];
    Edge[t1].redge = Edge + t2;
    V[a] = Edge + t1;

    Edge[t2].node = a;
    Edge[t2].c = 0;
    Edge[t2].next = V[b];
    Edge[t2].redge = Edge + t1;
    V[b] = Edge + t2;
}
bool F[SIZE_V];
//u当前节点,f为当前流
int dfs(int u,int current){
    if ( u == end ) return current;

    F[u] = true;
    for(edge_t*p=V[u]; p ;p=p->next){
        int v = p->node;
        if( F[v] ) continue;
        weight_t c = p->c;

        if ( c > 0 ){
            int t = dfs(v,min(c,current));
            if ( 0 == t ) continue;

            p->c -= t;
            p->redge->c += t;
            return t;
        }
    }

    return 0;
}

int solve(){
    int ret = 0;
    while(1){
    	MEM(F,0);
        //fill(F+1,F+m+1,false);
        int t = dfs(start,INT_MAX);
        if ( 0 == t ) return ret;
        ret += t;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值