pku 1149 最大流

來源:互聯網
上載者:User
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;
}

  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.