1. 程式人生 > >hdu 4292 拆點 最大流

hdu 4292 拆點 最大流

具體思路見這裡:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std;
#define M 10100
int gap[M],dis[M],pre[M],cur[M];
int NE,NV;
int head[M],FF[402],DD[402];
char str[402];
struct Node
{
    int c,pos,next;
} E[999999];
#define FF(i,NV) for(int i=0;i<NV;i++)
int sap(int s,int t)
{
    //memset(pre,-1,sizeof(pre));
    memset(dis,0,sizeof(int)*(NV+1));
    memset(gap,0,sizeof(int)*(NV+1));
    FF(i,NV) cur[i] = head[i];
    int u = pre[s] = s,maxflow = 0,aug = 1<<29;
    gap[0] = NV;
    while(dis[s] < NV)
    {
loop:
        for(int &i = cur[u]; i != -1; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && dis[u] == dis[v] + 1)
            {
                aug=min(aug,E[i].c);
                pre[v] = u;
                u = v;
                if(v == t)
                {
                    maxflow += aug;
                    for(u = pre[u]; v != s; v = u,u = pre[u])
                    {
                        E[cur[u]].c -= aug;
                        E[cur[u]^1].c += aug;
                    }
                    aug = 1<<29;
                }

                goto loop;
            }
        }

        int mindis = NV;
        for(int i = head[u]; i != -1 ; i = E[i].next)
        {
            int v = E[i].pos;
            if(E[i].c && mindis > dis[v])
            {
                cur[u] = i;
                mindis = dis[v];
            }
        }
        if( (--gap[dis[u]]) == 0)break;
        gap[ dis[u] = mindis+1 ] ++;
        u = pre[u];
    }
    return maxflow;
}
void addEdge(int u,int v,int c )
{
    E[NE].c = c;
    E[NE].pos = v;
    E[NE].next = head[u];
    head[u] = NE++;
    E[NE].c = 0;
    E[NE].pos = u;
    E[NE].next = head[v];
    head[v] = NE++;
}
int N, F, D,idx;
int main()
{
    while (~scanf("%d %d %d", &N, &F, &D))
    {
        NV=F+2*N+D+2;
        FF(i,NV) head[i] = -1;
        NE = 0;
        memset(head, -1, sizeof(head));
        idx = 0;
        int a, b, f, d;
        for(int i=1;i<=F;i++)
        {
            scanf("%d",&FF[i]);
        }
        for(int i=1;i<=D;i++)
        {
            scanf("%d",&DD[i]);
        }
        for (int i = 1; i <= N; ++i)
        {
            scanf("%s",&str);
            int len=strlen(str);
            for(int j=0;j<len;j++)
            {
                if(str[j]=='Y')
                addEdge(j+1,F+i,1);
            }
        }
         for (int i = 1; i <= N; ++i)
        {
            scanf("%s",&str);
            int len=strlen(str);
            for(int j=0;j<len;j++)
            {
                if(str[j]=='Y')
                addEdge(F+N+i,F+N+N+j+1,1);
            }
        }
        int source = 0, sink = N + N + F + D + 1;
        //int n = sink + 1;
        for (int i = 1; i <= F; ++i)
            addEdge(source, i, FF[i]);
        for (int i = 1; i <= N; ++i)
            addEdge(F + i, F + N + i, 1);
        for (int i = 1; i <= D; ++i)
            addEdge(F + N + N + i, sink, DD[i]);
        printf("%d\n", sap(source, sink));
    }
    return 0;
}