hdu 3987 Harry Potter and the Forbidden Forest//边数最小的最小割 2种解题方法


每条边的权值改为w*(M+1)+1

最后得到的和sum

sum div (M+1)  得到的是最小割的值,用(M+1)是为了不被后面的1影响到

sum mod (M+1)得到的是最小割的边数,这个是在上面的最小割前提下求出来的(YY一下,在原题中可能存在多组割边,边数不同,但是

值相同。现在同乘以(M+1)的话,值不变。但是+1的话,就会按照边的数量得出边数最小的最小割)


#include <cstdio>
#include <cstring>
const int N=2000 + 123;
const long long M=100000*4+12345;
const long long inf=(1ll)<<60;
int head[N];
struct Edge
{
    int v,next;
    long long w;
} edge[M];
int cnt,n,s,t;
void addedge(int u,int v,long long w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
int pre[N],cur[N], gap[N], dis[N];
long long sap()
{
    long long flow=0,aug=inf;
    int u;
    bool flag;
    for(int i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0) break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}
int main()
{
    int T, cas=1;
    int m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        s=0;
        t=n -1;
        cnt=0;
        memset(head,-1,sizeof(head));

        while(m--)
        {
            int a, b,d;
            long long c;
            scanf("%d%d%I64d%d", &a, &b, &c, &d);
            if(d == 0)
            {
                addedge(a, b, M * c + 1);
            }
            else
            {
                addedge(a, b, M * c + 1);
                addedge(b, a, M * c + 1);
            }
        }

        long long ans = sap() % M;
        printf("Case %d: %I64d\n", cas++, ans);
    }
    return 0;
}


这个方法比较传统,不多说了

#include <cstdio>
#include <cstring>
const int N=2000 + 123;
const long long M=100000*4+12345;
const long long inf=(1ll)<<60;
int head[N];
struct Edge
{
    int v,next;
    long long w;
} edge[M], ed[M];
struct bian
{
    int u, v, d, id[2];
    long long c;
}bian[M];
int cnt,n,s,t;
void addedge(int u,int v,long long w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].v=u;
    edge[cnt].w=0;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void addedge2(int u,int v,long long w)
{
    ed[cnt].v=v;
    ed[cnt].w=w;
    ed[cnt].next=head[u];
    head[u]=cnt++;
    ed[cnt].v=u;
    ed[cnt].w=0;
    ed[cnt].next=head[v];
    head[v]=cnt++;
}
int pre[N],cur[N], gap[N], dis[N];
long long sap(Edge * edge)
{
    long long flow=0,aug=inf;
    int u;
    bool flag;
    for(int i=0; i<n; i++)
    {
        cur[i]=head[i];
        gap[i]=dis[i]=0;
    }
    gap[s]=n;
    u=pre[s]=s;
    while(dis[s]<n)
    {
        flag=0;
        for(int &j=cur[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[u]==dis[v]+1)
            {
                flag=1;
                if(edge[j].w<aug) aug=edge[j].w;
                pre[v]=u;
                u=v;
                if(u==t)
                {
                    flow+=aug;
                    while(u!=s)
                    {
                        u=pre[u];
                        edge[cur[u]].w-=aug;
                        edge[cur[u]^1].w+=aug;
                    }
                    aug=inf;
                }
                break;
            }
        }
        if(flag) continue;
        int mindis=n;
        for(int j=head[u]; j!=-1; j=edge[j].next)
        {
            int v=edge[j].v;
            if(edge[j].w>0&&dis[v]<mindis)
            {
                mindis=dis[v];
                cur[u]=j;
            }
        }
        if((--gap[dis[u]])==0) break;
        gap[dis[u]=mindis+1]++;
        u=pre[u];
    }
    return flow;
}
int main()
{
    int T, cas=1;
    int m;
    scanf("%d", &T);
    while(T--)
    {
        scanf("%d%d", &n, &m);
        s=0;
        t=n -1;
        cnt=0;
        memset(head,-1,sizeof(head));

        for(int i = 0; i < m; i++)
        {
            int a, b,d;
            long long c;
            scanf("%d%d%I64d%d", &bian[i].u, &bian[i].v, &bian[i].c, &bian[i].d);
            if(bian[i].d == 0)
            {
                bian[i].id[0] = cnt;
                addedge(bian[i].u, bian[i].v, bian[i].c);
            }
            else
            {
                bian[i].id[0] = cnt;
                addedge(bian[i].u, bian[i].v, bian[i].c);
                bian[i].id[1] = cnt;
                addedge(bian[i].v, bian[i].u, bian[i].c);
            }
        }

        long long ans = sap(edge) ;
        //printf("Case %d: %I64d\n", cas++, ans);

        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i = 0; i < m; i++)
        {
            if(bian[i].d == 0)
            {
                if(edge[bian[i].id[0]].w == 0)  //满流
                    addedge2(bian[i].u, bian[i].v, 1);
                else
                    addedge2(bian[i].u, bian[i].v, inf);
            }
            else
            {
                 if(edge[bian[i].id[0]].w == 0)  //满流
                    addedge2(bian[i].u, bian[i].v, 1);
                else
                    addedge2(bian[i].u, bian[i].v, inf);

                 if(edge[bian[i].id[1]].w == 0)  //满流
                    addedge2(bian[i].v, bian[i].u, 1);
                else
                    addedge2(bian[i].v, bian[i].u, inf);
            }
        }

        printf("Case %d: %I64d\n", cas++, sap(ed));
    }
    return 0;
}


你可能感兴趣的:(hdu 3987 Harry Potter and the Forbidden Forest//边数最小的最小割 2种解题方法)