首页 > 其他 > 详细

dinic模板

时间:2017-08-10 09:40:23      阅读:184      评论:0      收藏:0      [点我收藏+]
#include<iostream>
#include<cstdio>
#include<cstring>
#define inf 0x7fffffff
#define ll long long 
using namespace std;
ll head[205],q[205],T,h[205];
ll cnt=1,ans,n,m;
struct data{ll to,next,v;}e[501];
void ins(ll u,ll v,ll w)
{e[++cnt].to=v;e[cnt].next=head[u];e[cnt].v=w;head[u]=cnt;}
void insert(ll u,ll v,ll w)
{ins(u,v,w);ins(v,u,0);}
bool bfs()
{
     ll t=0,w=1,i,now;
     memset(h,-1,sizeof(h));
     q[0]=0;h[0]=0;
     while(t<w)
     {
               now=q[t];t++;i=head[now];
               while(i)
               {
                       if(e[i].v&&h[e[i].to]==-1)
                       {
                                                 h[e[i].to]=h[now]+1;
                                                 q[w++]=e[i].to;
                                                 }
                       i=e[i].next;
                       }
               }                
    return h[T]==-1? 0:1;
 }
ll dfs(ll x,ll f)
{
    if(x==T)return f;
    ll w,used=0,i;
    i=head[x];
    while(i)
    {
            if(e[i].v&&h[e[i].to]==h[x]+1)
            {
                                          w=f-used;
                                          w=dfs(e[i].to,min(w,e[i].v));
                                          e[i].v-=w;
                                          e[i^1].v+=w;
                                          used+=w;
                                          if(used==f)return f;
                                          }
            i=e[i].next;
            }
    if(!used)h[x]=-1;
    return used;
}
void dinic(){while(bfs())ans+=dfs(0,inf);}
void ini()
{
     scanf("%lld%lld",&m,&n);
     T=n-1; 
     for(ll i=1;i<=m;i++)
     {
         ll u,v,c;
         scanf("%lld %lld %lld",&u,&v,&c);
         u--,v--;
         insert(u,v,c);
    } 
}
int main()
{
    ini();
    dinic();
    printf("%lld",ans);
    return 0;
    }

草地那道题我把数据范围从1000改到200就从2016ms到了8ms?

技术分享

气死

dinic模板

原文:http://www.cnblogs.com/dancer16/p/7337250.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!