[BZOJ3144] [Hnoi2013] Cut cake (min cut)

Source: Internet
Author: User
Topic Description

Transfer Door solving

It's a typical minimal cut.
Cut cake Each small piece is a point, and then on the top layer of cut cake to build a layer of p*q virtual point
s to the top of the point, the bottom layer of the point to the T-edge
For the first limit, each point is connected to the point below it, and the capacity is the weight of the following point
For the second limit, each point is adjacent to the four dots above the D point on it, and the capacity is INF, which ensures that if the cut is not within the distance of D, the source and meeting points are connected, it must not be a minimal cutting scheme.
And then you do the minimum cut. Code

#include <algorithm> #include <iostream> #include <cstring> #include <cstdio> #include <
Cmath> #include <queue> using namespace std;
#define N 70005 #define E 700005 #define INF 2000000000 int p,q,r,d,s,t,maxflow;
int val[45][45][45];
int tot,point[n],nxt[e],v[e],remain[e];
int deep[n],last[n],num[n],cur[n];

Queue <int> q;
    void Addedge (int x,int y,int cap) {++tot; nxt[tot]=point[x]; point[x]=tot; v[tot]=y; remain[tot]=cap; ++tot; Nxt[tot]=point[y]; Point[y]=tot; V[tot]=x;
remain[tot]=0;
    } void BFs (int t) {for (int i=1;i<=t;++i) deep[i]=t;
    deep[t]=0;
    for (int i=1;i<=t;++i) cur[i]=point[i];
    while (!q.empty ()) Q.pop ();
    Q.push (t);
        while (!q.empty ()) {int Now=q.front (); Q.pop (); for (int i=point[now];i!=-1;i=nxt[i]) if (deep[v[i]]==t&&remain[i^1]) {de
                ep[v[i]]=deep[now]+1;
            Q.push (V[i]); int addflow (int s,int t) {int now=t,ans=inf;
        while (now!=s) {ans=min (Ans,remain[last[now]);
    NOW=V[LAST[NOW]^1];
    } now=t;
        while (now!=s) {Remain[last[now]]-=ans;
        Remain[last[now]^1]+=ans;
    NOW=V[LAST[NOW]^1];
return ans;
    } void Isap (int s,int t) {BFS (t);

    for (int i=1;i<=t;++i) ++num[deep[i]];
    int now=s;
            while (deep[s]<t) {if (now==t) {maxflow+=addflow (s,t);
        Now=s;
        BOOL Has_find=false;
                for (int i=cur[now];i!=-1;i=nxt[i]) if (Deep[v[i]]+1==deep[now]&&remain[i]) {
                Has_find=true;
                Cur[now]=i;
                Last[v[i]]=i;
                Now=v[i];
            Break
            } if (!has_find) {int minn=t-1;
            for (int i=point[now];i!=-1;i=nxt[i]) if (remain[i)) Minn=min (Minn,deep[v[i]); if (!) ( --num[deep[Now]])
            Break
            ++NUM[DEEP[NOW]=MINN+1];
            Cur[now]=point[now];
        if (now!=s) now=v[last[now]^1];
    int main () {Tot=-1;memset (point,-1,sizeof (point));
    scanf ("%d%d%d", &p,&q,&r);
    scanf ("%d", &d);
    s=p*q* (r+1) +1,t=s+1; for (int k=1;k<=r;++k) for (int i=1;i<=p;++i) for (int j=1;j<=q;++j) scanf ("%
    D ", &val[i][j][k]);
                for (int k=r+1;k>1;--k) for (int i=1;i<=p;++i) for (int j=1;j<=q;++j) {
                int up= (k-1) *p*q+ (i-1) *q+j;
                int down=up-p*q;
            Addedge (Up,down,val[i][j][k-1]);
        for (int k=1;k<=r;++k) {if (k+d>r) break; for (int i=1;i<=p;++i) for (int j=1;j<=q;++j) {int down= (k-1) *p*q+ (i-1) *q+j
                ;
                int up=down+d*p*q;
                if (i!=1) Addedge (Down,up-q,inf); IF (j!=1) Addedge (Down,up-1,inf);
                if (i!=p) Addedge (Down,up+q,inf);
            if (j!=q) Addedge (Down,up+1,inf); for (int i=1;i<=p;++i) for (int j=1;j<=q;++j) Addedge (S, (i-1) *q+j+p*q*r,inf), Addedge
    ((i-1) *q+j,t,inf);
    ISAP (s,t);
printf ("%d\n", Maxflow); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.