网络流板子(Dicnic)

HDU-1532

#include<bits/stdc++.h>
#define FIN freopen("input.txt","r",stdin)
#define ll long long
#define mod 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 2e9
const int maxn = 405;
using namespace std;
int cnt,n,m;
int head[maxn],Next[maxn*200],to[maxn*200];
int flow[maxn*200],dep[maxn],cur[maxn];
inline void add(int u,int v,int w){
    Next[cnt]=head[u];
    head[u]=cnt;
    to[cnt]=v;
    flow[cnt++]=w;
}
int dfs(int u,int cost){
    if(u==m) return cost;
    for(int i=head[u];i!=-1;i=Next[i]){
        if(dep[to[i]]==dep[u]+1&&flow[i]>0){
            int dis=dfs(to[i],min(flow[i],cost));
            if(dis>0){
                flow[i]-=dis;
                flow[i^1]+=dis;
                return dis;
            }
        }
    }
    return 0;
}
int bfs(){
    queue<int> q;
    q.push(1);
    memset(dep,0,sizeof(dep));
    dep[1]=1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=Next[i]){
            if(flow[i]>0&&dep[to[i]]==0){
                dep[to[i]]=dep[u]+1;
                q.push(to[i]);
            }
        }
    }
    if(dep[m]>0) return 1;
    return 0;
}
int dicnic(){
    int ans=0;
    while(bfs()){
        int cost;
        for(int i=1;i<=m+2;i++){
            cur[i]=head[i];
        }
        while(cost=dfs(1,inf))
            ans+=cost;
    }
    return ans;
}
void init(){
    memset(head,-1,sizeof(head));
    cnt;
}
int main()
{
    #ifndef ONLINE_JUDGE
        FIN;
    #endif

        while(~scanf("%d %d",&n,&m)){
            init();
        for(int i=1;i<=n;i++){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            add(u,v,w);
            add(v,u,0); 
        }
        printf("%d\n",dicnic());
    }
    
}
View Code

 

转载于:https://www.cnblogs.com/MengX/p/10574838.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值