• bzoj3144 [Hnoi2013]切糕


    Description
    这里写图片描述
    Input
    第一行是三个正整数P,Q,R,表示切糕的长P、 宽Q、高R。第二行有一个非负整数D,表示光滑性要求。接下来是R个P行Q列的矩阵,第z个 矩阵的第x行第y列是v(x,y,z) (1≤x≤P, 1≤y≤Q, 1≤z≤R)。
    100%的数据满足P,Q,R≤40,0≤D≤R,且给出的所有的不和谐值不超过1000。

    Output
    仅包含一个整数,表示在合法基础上最小的总不和谐值。

    Sample Input
    2 2 2
    1
    6 1
    6 1
    2 6
    2 6

    Sample Output
    6

    HINT
    最佳切面的f为f(1,1)=f(2,1)=2,f(1,2)=f(2,2)=1

    分析:
    这里写图片描述

    tip

    stl的queu慢出天际,

    以后一定要手写队列

    空间不要开太大

    一开始的内存4Wkb,慢出天际,
    学长的只有3W-,改了一下内存,立马时间砍一半

    这里写代码片
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    
    using namespace std;
    
    const int INF=0x33333333;
    const int N=1000010;
    const int M=60010;
    struct node{
        int x,y,v,nxt;
    };
    node way[N<<1];
    int st[M],tot=-1,cur[M],deep[M],P,Q,R,z[41][41][41],s,t,D;
    int q[M],tou,wei;
    
    int get(int x,int y,int z){return (z-1)*P*Q+(x-1)*Q+y;}
    
    void add(int u,int w,int z)
    {
        tot++;
        way[tot].x=u;way[tot].y=w;way[tot].v=z;way[tot].nxt=st[u];st[u]=tot;
        tot++;
        way[tot].x=w;way[tot].y=u;way[tot].v=0;way[tot].nxt=st[w];st[w]=tot;
    }
    
    int bfs(int s,int t)
    {
        memset(deep,-1,sizeof(deep));
        tou=wei=0;
        q[++wei]=s;
        deep[s]=1;
        for (int i=s;i<=t;i++) cur[i]=st[i];
        do
        {
            int r=q[++tou];
            for (int i=st[r];i!=-1;i=way[i].nxt)
                if (way[i].v&&deep[way[i].y]==-1)
                {
                    deep[way[i].y]=deep[r]+1;
                    q[++wei]=way[i].y;
                }
        }while (tou<wei);
        return deep[t]!=-1;
    }
    
    int dfs(int now,int t,int limit)
    {
        if (!limit||now==t) return limit;
        int i,f,flow=0;
        for (i=cur[now];i!=-1;i=way[i].nxt)
        {
            cur[now]=i;
            if (deep[way[i].y]==deep[now]+1&&way[i].v&&(f=dfs(way[i].y,t,min(limit,way[i].v))))
            {
                flow+=f;
                limit-=f;
                way[i].v-=f;
                way[i^1].v+=f;
                if (!limit) break;
            }
        }
        return flow;
    }
    
    int doit()
    {
        int ans=0;
        while (bfs(s,t))
            ans+=dfs(s,t,INF);
        return ans;
    }
    
    int main()
    {
        memset(st,-1,sizeof(st));
        scanf("%d%d%d",&P,&Q,&R);
        scanf("%d",&D);
        s=0;t=P*Q*(R+1)+1;
        for (int k=1;k<=R;k++)  //
            for (int i=1;i<=P;i++)
                for (int j=1;j<=Q;j++)
                {
                    scanf("%d",&z[i][j][k]);
                }  
        for (int i=1;i<=P;i++)
            for (int j=1;j<=Q;j++)
            {
                add(s,get(i,j,1),INF);
                add(get(i,j,R+1),t,INF);
            }     
        for (int k=1;k<=R+1;k++)
            for (int i=1;i<=P;i++)
                for (int j=1;j<=Q;j++)
                {
                    if (k!=R+1) add(get(i,j,k),get(i,j,k+1),z[i][j][k]);
                    if (k>D)
                    {
                        int h=k-D;
                        if (i>1) add(get(i,j,k),get(i-1,j,h),INF);
                        if (j>1) add(get(i,j,k),get(i,j-1,h),INF);
                        if (i<P) add(get(i,j,k),get(i+1,j,h),INF);
                        if (j<Q) add(get(i,j,k),get(i,j+1,h),INF);
                    }
                }
        printf("%d",doit());
        return 0;
    }
  • 相关阅读:
    bzoj1303: [CQOI2009]中位数图
    bzoj1778: [Usaco2010 Hol]Dotp 驱逐猪猡(概率DP+高斯消元)
    bzoj1013: [JSOI2008]球形空间产生器sphere(高斯消元)
    bzoj1857: [Scoi2010]传送带(三分套三分)
    LibreOJ #6221. 幂数 !(数论+dfs+剪枝)
    bzoj1968: [Ahoi2005]COMMON 约数研究(数论)
    bzoj1015: [JSOI2008]星球大战starwar(并查集)
    SRM16 B-2(DP)
    数据库的增、删、改、查 (CURD)
    软件开发
  • 原文地址:https://www.cnblogs.com/wutongtong3117/p/7673441.html
Copyright © 2020-2023  润新知