hdu 1532 最简单的最大流

哥第一次写dinic,参考了一下sphinx的模版。。哥菜,写了两天了。。。不过总算是写出来了~优化中~~注意这个是多重图~~
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<cstdio>
#include<iomanip>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=222;
int c[maxn][maxn];
int f[maxn][maxn];
bool you[maxn][maxn];
int cen[maxn];
int n,m;
int tx,ty,tc;
vector<int>g[maxn];
queue<int>q;
bool bfs()
{
    memset(cen,-1,sizeof(cen));
    while (!q.empty()) q.pop();
    q.push(1);
    cen[1]=0;   
    int now;
    while(!q.empty()) 
    {
        now=q.front();
        q.pop();
        for(int i=0;i<g[now].size();i++)
        {
            if(you[now][g[now][i]] && cen[g[now][i]] == -1)
            {   
                cen[g[now][i]]= cen[now] + 1;
                q.push(g[now][i]);
            }
        }    
    }  
    return cen[n]!=-1; 
}
int dfs(int flow=inf,int now=1)
{
    if(cen[now] == cen[n] )
    {       
        if(now == n)
        {
            return flow;
        }
        else
        {   
            return 0;
        }
    }
    int temp,sum;
    sum = 0;
    for(int i=0;i<g[now].size();i++)
    {
        if(cen[g[now][i]] - cen[now]  !=1 || !you[now][g[now][i]] || cen[g[now][i]] > cen[n] )
        {
            continue;
        }
        temp = dfs (min(c[now][g[now][i]] - f[now][g[now][i]] , flow) , g[now][i]);        
        f[now][g[now][i]] += temp;
        f[g[now][i]][now] -= temp;
        if(f[now][g[now][i]] == c[now][g[now][i]]) 
        {
            you[now][g[now][i]] = false;
        }            
        you[g[now][i]][now] = true;
        sum += temp;
        flow -= temp;
    }            
    return sum;
}
int dinic()
{
    int ans=0;
    while( bfs() )
    {
        ans+=dfs();    
    }      
    return ans;
}
int main()
{
    while(cin>>m>>n)
    {                           
        memset(c,0,sizeof(c));
        memset(f,0,sizeof(f));
        memset(you,false,sizeof(you));
        for(int i=1;i<=n;i++)
        {   
            g[i].clear();
        }
        for(int i=1;i<=m;i++)
        {
            cin>>tx>>ty>>tc;
            c[tx][ty]+=tc;
            you[tx][ty]=true;
            g[tx].push_back(ty);
            g[ty].push_back(tx);
        }
        cout<<dinic()<<endl;       
    }   
    return 0; 
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值