[上下界网络流] BZOJ 2502 清理雪道

意淫一个最大流出来 1000ms+

然而大神题解最小流跑得飞快 http://blog.csdn.net/popoqqq/article/details/48467349

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

inline char nc()
{
	static char buf[100000],*p1=buf,*p2=buf;
	if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
	return *p1++;
}

inline void read(int &x)
{
	char c=nc(),b=1;
	for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
	for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc());
}

namespace DINIC{
	#define oo 1<<30
	#define V G[p].v
	#define cl(x) memset(x,0,sizeof(x))
	#define M 200000+5
	#define N 200+5
	struct edge{
		int u,v,f;
		int next;
	};
	edge G[M];
	int head[N],num=1;
	inline void add(int u,int v,int f,int p){
		G[p].u=u; G[p].v=v; G[p].f=f; G[p].next=head[u]; head[u]=p;
	}
	inline void link(int u,int v,int f){
		add(u,v,f,num+=2); add(v,u,0,num^1);
	}
	int S,T;
	int Que[N],l,r;
	int dis[N];
	inline bool bfs(){
		int u;
		memset(dis,-1,sizeof(dis)); cl(Que); l=r=-1;
		dis[S]=1; Que[++r]=S;
		while (l<r)
		{
			u=Que[++l];
			for (int p=head[u];p;p=G[p].next)
				if (G[p].f && dis[V]==-1)
				{
					dis[V]=dis[u]+1;
					Que[++r]=V;
					if (V==T) return true;
				}
		}
		return false;
	} 
	int minimum;
	int cur[N];
	inline bool dfs(int u,int mini){
		if (u==T) { minimum=mini; return true; }
		for (int p=cur[u];p;p=G[p].next)
		{
			cur[u]=p;
			if (G[p].f && dis[V]==dis[u]+1 && dfs(V,min(mini,G[p].f)))
			{
				G[p].f-=minimum; G[p^1].f+=minimum;
				return true;
			}
		}
		dis[u]=-1;
		return false;
	}
	int ret=0;
	inline int Dinic(){
		while (bfs())
		{
			memcpy(cur,head,sizeof(cur));
			while (dfs(S,oo)) 
				ret+=minimum;
		}
		return ret;
	}
	inline void clear(){
		cl(G); cl(head); num=1;
	}
}

int n,m;

int main()
{
	using namespace DINIC;
	int x,y,z,s,t;
	freopen("t.in","r",stdin);
	freopen("t.out","w",stdout);
	read(n);
	s=n+1; t=n+2;
	S=n+3; T=n+4;
	for (int i=1;i<=n;i++)
	{
		read(z); x=i; m+=z;
		link(x,T,z);
		for (int j=1;j<=z;j++)
		{
			read(y);
			link(x,y,oo);
			link(S,y,1);
		}
	}
	for (int i=1;i<=n;i++)
		link(s,i,oo),link(i,t,oo);
	for (int i=1;;i++)
	{
		link(t,s,1);
		if (Dinic()==m)
			return printf("%d\n",i),0;	
	}
	return 0;
}


你可能感兴趣的:([上下界网络流] BZOJ 2502 清理雪道)