首页 > 代码库 > poj1149最大流

poj1149最大流

 #include <cstdio>#include <cstring>#include <algorithm>#include <climits>#include <string>#include <iostream>#include <map>#include <cstdlib>#include <list>#include <set>#include <queue>#include <stack>using namespace std;const int INF=0xfffffff;int n,m;const int maxn=1111;int level[maxn];int Map[maxn][maxn];int last[maxn];int s,e;int pig[maxn];bool bfs(){    memset(level,0,sizeof(level));    queue<int> q;    q.push(s);level[s]=1;    while(!q.empty()){        int cur=q.front();q.pop();        for(int i=0;i<=n+1;i++){            if(!level[i]&&Map[cur][i]){                level[i]=level[cur]+1;                q.push(i);            }        }    }    return level[e];}int Min(int a,int b){    return a>b?b:a;}int dfs(int x,int val){    int tem=val;    if(x==e) return val;    for(int i=0;i<=n+1;i++){        if(level[i]==level[x]+1&&Map[x][i]&&tem){            int t=dfs(i,Min(val,Map[x][i]));            Map[x][i]-=t;Map[i][x]+=t;           // cout<<tem<<endl;system("pause");        //   if(t==0) return 0;           tem-=t;        }    }    return val-tem;}int solve(){    int ans=0;int t;    while(bfs()){        while(t=dfs(s,INF)) ans+=t;    }    return ans;}int main(){    while(scanf("%d%d",&m,&n)!=EOF){    s=0;e=n+1;    int t;int k;    memset(last,0,sizeof(last));    memset(Map,0,sizeof(Map));    for(int i=1;i<=m;i++)        scanf("%d",&pig[i]);    for(int i=1;i<=n;i++){        scanf("%d",&t);        for(int j=0;j<t;j++){            scanf("%d",&k);            if(last[k]==0){                Map[s][i]+=pig[k];            }            else Map[last[k]][i]=INF;            last[k]=i;        }        scanf("%d",&Map[i][e]);    }    printf("%d\n",solve());    }    return 0;}