【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

题解

寒假培训的时候讲过这题了,今天正好把切掉。
考虑对于每个 (x,y,z) 建立一个点。再建立附加源S,附加汇T。
如下建图:
1.S向 (x,y,1) 连一条容量为 f[x][y][1] 的边。
2. (x,y,R) 向T连一条容量为 inf 的边。
3. (x,y,z1) (x,y,z) 连一条容量为 f[x][y][z] 的边。
4.若 (x,y) (x,y) 和相邻,且 z>d ,则 (x,y,z) (x,y,zd) 连一条容量为 inf 的边。
在这个图上求出最小割即是答案。
【建模分析】
对于每个 (x,y) 使其层高为 z 可以视作在图上割掉[(x,y,z1),(x,y,z)]这条边。而再增加了 inf 边之后,相邻的两点高度如果相差超过d,那么割掉那两条边是不能使源、汇在这两条路径上不连通的。所以这个图的最小割一定满足限制。

#include<bits/stdc++.h>
using namespace std;

inline int read(){
    int x = 0, f = 1; char c = getchar();
    while(!isdigit(c)) { if(c == '-') f = -1; c = getchar(); }
    while(isdigit(c)) { x = x * 10 + c - '0'; c = getchar(); }
    return x * f;
}

const int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1}, inf = 0x3f3f3f3f;
const int N = 100000 + 10, M = 1000000 + 10;
int P, Q, R, D, f[50][50][50];
int s, t;
struct Edge{
    int fr, to, cap, flow;
    Edge(){fr = to = flow = cap = 0;}
    Edge(int u, int v, int w){fr = u, to = v, cap = w, flow = 0;}
}edg[M];
int hd[N], nxt[M], tot;
int d[N], q[N], vis[N], cur[N], dfn;
int ans;

void init(){
    memset(hd, -1, sizeof(hd));
    P = read(); Q = read(); R = read();
    D = read();
    t = P * Q * R + 1;
    for(int i = 1; i <= R; i++)
    for(int j = 1; j <= P; j++)
    for(int k = 1; k <= Q; k++)
        f[i][j][k] = read();

}

inline int p(int x, int y, int z){
    if(!z) return s;
    return ((x - 1) * Q + y - 1) * R + z;
}

inline void insert(int u, int v, int w){
    edg[tot] = Edge(u, v, w); nxt[tot] = hd[u]; hd[u] = tot++;
    edg[tot] = Edge(v, u, 0); nxt[tot] = hd[v]; hd[v] = tot++;
}

void build(){
    for(int i = 1; i <= P; i++)
        for(int j = 1; j <= Q; j++){
            for(int k = 1; k <= R; k++){
                insert(p(i, j, k-1), p(i, j, k), f[k][i][j]);
                if(k > D)
                for(int l = 0; l < 4; l++){
                    int x = i + dx[l], y = j + dy[l];
                    if(1 <= x && x <= P && 1 <= y && y <= Q)
                        insert(p(i, j, k), p(x, y, k - D), inf);
                }
            }
            insert(p(i, j, R), t, inf);
        }
}

bool bfs(){
    int head = 0, tail = 1;
    d[s] = 0, q[0] = s, vis[s] = ++dfn;
    while(head < tail){
        int u = q[head++];
        for(int i = hd[u]; i >= 0; i = nxt[i]){
            Edge &e = edg[i];
            if(vis[e.to] != dfn && e.cap > e.flow){
                d[e.to] = d[u] + 1;
                vis[e.to] = dfn;
                q[tail++] = e.to;
            }
        }
    }
    return vis[t] == dfn;
}

int dfs(int x, int a){
    if(x == t || a == 0) return a;
    int flow = 0, f;
    for(int &i = cur[x]; i >= 0; i = nxt[i]){
        Edge &e = edg[i];
        if(d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
            flow += f;
            e.flow += f;
            a -= f;
            edg[i^1].flow -= f;
            if(a == 0) break;
        }
    }
    return flow;
}

void work(){
    build();
    while(bfs()){
        for(int i = s; i <= t; i++) cur[i] = hd[i];
        ans += dfs(s, inf);
    }
    printf("%d\n", ans);
}

int main(){
    init();
    work();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值