HDU-4292 Food 三分图

来源:互联网 发布:金蝶软件多少钱 编辑:程序博客网 时间:2024/06/07 11:35



#include "stdio.h"#include "queue"#include "algorithm"using namespace std;const int maxn = 810;const int inf = 1<<30;int n,m,pos;struct Node{int v,w,next;Node() {};Node( int v,int w,int next ):v(v),w(w),next(next) {}}node[maxn*maxn];int head[maxn];void add( int u,int v,int w ){node[pos] = Node(v,w,head[u]);head[u] = pos ++;node[pos] = Node(u,0,head[v]);head[v] = pos ++;}int ISAP( int st,int end ){int curedge[maxn],pre[maxn],h[maxn],gap[maxn];int u,neck,tmp,i;memset( h,0,sizeof(h) );memset( gap,0,sizeof(gap) );int cur_flow,flow_ans = 0;for( int i = 0; i <= n; i ++ )curedge[i] = head[i];gap[0] = n+1;u = st;while( h[st] < n+1 ){if( u == end ){cur_flow = inf;for( i = st; i != end; i = node[curedge[i]].v ){if( cur_flow > node[curedge[i]].w ){neck = i;cur_flow = node[curedge[i]].w;}}for( i = st; i != end; i = node[curedge[i]].v ){tmp = curedge[i];node[tmp].w -= cur_flow;node[tmp^1].w += cur_flow;}flow_ans += cur_flow;u = neck;}for( i = curedge[u]; i != -1; i = node[i].next ){if( node[i].w && h[u] == h[node[i].v] + 1 )break;}if( i != -1 ){curedge[u] = i;pre[node[i].v] = u;u = node[i].v;}else{if( 0 == --gap[h[u]] )break;curedge[u] = head[u];for( tmp = n,i = head[u]; i != -1; i = node[i].next )if( node[i].w )tmp = tmp < h[node[i].v]?tmp:h[node[i].v];h[u] = tmp + 1;gap[h[u]] ++;if( u != st )u = pre[u];}}return flow_ans;}int main(){//freopen("area1.in","r",stdin);freopen("data.txt","r",stdin);char ch;int M,F,D,m,f,d,temp;    while( scanf("%d%d%d",&m,&f,&d) == 3 ){n = f + 2*m + d + 1;pos = 0;memset(head,-1,sizeof(head));for( int i = 0; i <= n; i ++ )head[i] = -1;F = 0; M = f; D = f+2*m;for( int i = 1; i <= f; i ++ ){scanf("%d",&temp );add(0,i,temp);}for( int i = D+1; i <= D+d; i ++ ){scanf("%d",&temp);add(i,n,temp);}for( int i = M+1; i <= M+m; i ++ )add(i,i+m,1);for( int i = M+1; i <= M+m; i ++ ){getchar();for( int j = F+1; j <= F+f; j ++ ){scanf("%c",&ch);if( ch == 'Y' )add(j,i,1); }}for( int i = M+m+1; i <= M+2*m; i ++ ){getchar();for( int j = D+1; j <= D+d; j ++ ){scanf("%c",&ch);if( ch == 'Y' )add(i,j,1);}}printf("%d\n",ISAP(0,n));}    return 0;}


原创粉丝点击