dinic模板

#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?

气死

posted @ 2017-08-10 08:18  dancer16  阅读(126)  评论(0编辑  收藏  举报
描述