hdu1532 当前弧优化的dinic算法实现

点击打开链接

裸题,主要练习手速!


#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int max_node = 205;
const int INF = 0x3f3f3f3f;
struct Edge{
    int to,cap,rev;
    Edge(int t,int c,int r):to(t), cap(c), rev(r) {}
};
vector<Edge> G[max_node];
int edge_num,sink;
int cur[max_node];
int dis[max_node];

void push_edge(int from,int to,int cap){
    G[from].push_back( Edge(to,cap,G[to].size()));
    G[to].push_back( Edge(from,0,G[from].size()-1));
}

void bfs(){
    memset(dis,-1,sizeof(dis));
    queue<int> q;
    q.push(1);
    dis[1]=0;
    while(!q.empty()){
        int t = q.front(); q.pop();
        for(int i = 0; i < G[t].size(); i++){
            Edge &e = G[t][i];
            if(e.cap > 0 && dis[e.to] == -1){
                dis[e.to] = dis[t] + 1;
                q.push(e.to);
            }
        }
    }
}

int dfs(int u,int v,int res){
    if(u == v) return res;
    for(int i = cur[u]; i< G[u].size(); i++){
        Edge &e = G[u][i];
        if(e.cap > 0 && dis[e.to] == dis[u] + 1){
            int x = dfs(e.to, v, min(res, e.cap));
            if(x > 0){
                e.cap -= x;
                G[e.to][e.rev].cap += x;
                cur[u] = i;
                return x;
            }
        }
    }
    return 0;
}
int max_flow(){
    int flow = 0;
    while(1){
        memset(cur, 0, sizeof(cur));
        bfs();
        if(dis[sink] == -1){
            return flow;
        }
        while(int x = dfs(1,sink,INF)){
            flow += x;
        }
    }
}

int main(){
#ifdef LOCAL_DEBUG
    freopen("input.txt" ,"r" ,stdin);
#endif // LOCAL_DEBUG
    while(scanf("%d%d" ,&edge_num, &sink)!=EOF){
        for(int i=0; i < max_node; i++) G[i].clear();
        int from,to,cap;
        for(int i=1; i<=edge_num; i++){
            scanf("%d%d%d" ,&from, &to, &cap);
            push_edge(from,to,cap);
        }
        printf("%d\n" ,max_flow());
    }
    return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值