pku 1149 最大流

View Code
#include<stdio.h>
#include
<string.h>
#define min(a,b)(a<b?a:b)
#define INF 9999999
#define MAX 1005
int S,N,end,M;
int cap[MAX][MAX],flow[MAX],p[MAX];
int room[MAX],belong[MAX];
int q[10000];
int bfs()
{
int u,i;
for(i=0;i<MAX;i++)
{
flow[i]
=INF;
p[i]
=-1;
}
p[S]
=0;
int front=0,rear=1;
q[front]
=S;
while(front<rear)
{
u
=q[front++];
if(u==end)
break;
for(i=1;i<=end;i++)
{
if(p[i]!=-1||cap[u][i]==0) continue;
p[i]
= u;
flow[i]
= min(flow[u],cap[u][i]);
q[rear
++] = i;
}
}
if(flow[end]==INF) return -1;
return flow[end];
}
int E_K()
{
int res=0,now,temp;
while(1)
{
temp
=bfs();
if(temp==-1) break;
res
+=temp;
for(now=end;now!=S;now=p[now])
{
cap[p[now]][now]
-=temp;
cap[now][p[now]]
+=temp;
}
}
return res;

}
int main()
{
int i,j,t,num;
while(scanf("%d%d",&M,&N)!=EOF)
{
S
=0;end=N+1;
memset(cap,
0,sizeof(cap));
for(i=1;i<=M;i++)
{
scanf(
"%d",&room[i]);
belong[i]
=0;
}
for(i=1;i<=N;i++)
{
scanf(
"%d",&num);
for(j=1;j<=num;j++)
{
scanf(
"%d",&t);
if(belong[t]==0)
{
belong[t]
=i;
cap[S][i]
+=room[t];
}
else cap[belong[t]][i]=INF;
}
scanf(
"%d",&t);
cap[i][end]
=t;
}
int ans=E_K();
printf(
"%d\n",ans);
}
return 0;
}

  

posted @ 2011-09-01 12:27  Because Of You  Views(193)  Comments(0Edit  收藏  举报