poj 1149 PIGS_网络流
#include<iostream> #include<queue> #include<cstdio> #include<cstring> using namespace std; #define INF 0xfffffff #define mMax 1010 #define nMax 110 int cap[nMax][nMax],flow[nMax][nMax]; int pre[nMax],dist[nMax]; int Edmonds_Karp(int sta,int end){ int i,curr,max=0; memset(flow,0,sizeof(flow)); while(1){ memset(pre,0,sizeof(pre)); memset(dist,0,sizeof(dist)); queue<int> q; q.push(sta); dist[sta]=INF; pre[sta]=sta; while(!q.empty()){ curr=q.front(); q.pop(); for(i=0;i<=end;i++) if(!dist[i]&&flow[curr][i]<cap[curr][i]){ dist[i]=min(dist[curr],cap[curr][i]-flow[curr][i]); pre[i]=curr; q.push(i); } } if(dist[end]==0) break; for(i=end;i!=sta;i=pre[i]){ curr=pre[i]; flow[curr][i]+=dist[end]; flow[i][curr]-=dist[end]; } max+=dist[end]; } return max; } int main(){ int m,n,k,i,j,key; int pighouse[mMax]={0}; int visit[mMax]={0}; memset(cap,0,sizeof(cap)); scanf("%d%d",&m,&n); for(i=1;i<=m;i++) scanf("%d",&pighouse[i]); for(i=1;i<=n;i++){ scanf("%d",&k); for(j=1;j<=k;j++){ scanf("%d",&key); if(!visit[key]) cap[0][i]+=pighouse[key]; else cap[visit[key]][i]=INF; visit[key]=i; } scanf("%d",&key); cap[i][n+1]=key; } printf("%d\n",Edmonds_Karp(0,n+1)); return 0; }
不怕路长,只怕心老.