poj 2289 (最大流+二分)

题意:把n个好友分到m个组。求每个组的最大人数最小可以是多少人,,,

最大流+二分,,,,isap跑得还是挺快的,,,







#include
#include
const int N=1510;
const int inf=0x3fffffff;
int dis[N],gap[N],head[N],num,start,ans,end,first[N],nume,n,m;
struct edge
{
	int st,ed,flow,next;
}E[510000];
struct node
{
	int x,next;
}e[510000];
void addedge(int x,int y,int w)
{
	E[num].st=x;E[num].ed=y;E[num].flow=w;E[num].next=head[x];head[x]=num++;
	E[num].st=y;E[num].ed=x;E[num].flow=0;E[num].next=head[y];head[y]=num++;
}
void Addedge(int x,int y)
{
	e[nume].x=y;e[nume].next=first[x];first[x]=nume++;
}
int dfs(int u,int minflow)
{
	if(u==end)return minflow;
	int i,v,f,flow=0,min_dis=ans-1;
	for(i=head[u];i!=-1;i=E[i].next)
	{
		if(E[i].flow>0)
		{
			v=E[i].ed;
			if(dis[v]+1==dis[u])
			{
				f=dfs(v,E[i].flow>minflow-flow?minflow-flow:E[i].flow);
				E[i].flow-=f;
				E[i^1].flow+=f;
				flow+=f;
				if(flow==minflow)break;
				if(dis[start]>=ans)return flow;
			}
			min_dis=min_dis>dis[v]?dis[v]:min_dis;
		}
	}
	if(flow==0)
	{
		if(--gap[dis[u]]==0)
		   dis[start]=ans;
		dis[u]=min_dis+1;
		gap[dis[u]]++;
	}
	return flow;
}
int isap()
{
	int maxflow=0;
	memset(dis,0,sizeof(dis));
	memset(gap,0,sizeof(gap));
	gap[0]=ans;
	while(dis[start]='0'&&str[j]<='9')
				{
					k=k*10+str[j]-'0';flag=1;
				}
				else if(flag==1)
				{
					Addedge(i,k+1);
					flag=0;k=0;
				}
			}
			if(flag==1)
			 Addedge(i,k+1);
		}
		left=0;right=n;
		while(left


你可能感兴趣的:(网络流)