poj 2289 (最大流+二分)
题意:把n个好友分到m个组。求每个组的最大人数最小可以是多少人,,,
最大流+二分,,,,isap跑得还是挺快的,,,
#include<stdio.h> #include<string.h> 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]<ans) maxflow+=dfs(start,inf); return maxflow; } void makemap(int D) { memset(head,-1,sizeof(head)); num=0; int i,j; for(i=1;i<=n;i++) { addedge(start,i,1); for(j=first[i];j!=-1;j=e[j].next) addedge(i,n+e[j].x,1); } for(i=1;i<=m;i++) addedge(i+n,end,D); } int main() { int i,j,k,flag,left,right,mid; char str[3000]; while(scanf("%d%d",&n,&m),n||m) { getchar(); memset(first,-1,sizeof(first)); nume=0; start=0;end=n+m+1;ans=end+1; for(i=1;i<=n;i++) { gets(str);k=0;flag=0; for(j=0;str[j];j++) { if(str[j]>='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<right) { mid=(left+right)/2;//每个组最多的人数 makemap(mid); if(isap()==n) right=mid; else left=mid+1; } printf("%d\n",right); } return 0; }
补充:软件开发 , C++ ,