首页 > 其他分享 >P3227 [HNOI2013] 切糕

P3227 [HNOI2013] 切糕

时间:2023-12-10 21:00:32浏览次数:32  
标签:切糕 include int cnt st HNOI2013 add P3227 define

题意

link

Sol

考虑不戴限制的情况,那就是对于每一层连到下一层跑网络流。

考虑戴上添边,不难发现向相邻的点连一条 \(inf\) 边就行了。

Code

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <array>
#include <queue>
#define int long long
#define pii pair <int, int>
using namespace std;
#ifdef ONLINE_JUDGE

#define getchar() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
char buf[1 << 23], *p1 = buf, *p2 = buf, ubuf[1 << 23], *u = ubuf;

#endif
int read() {
    int p = 0, flg = 1;
    char c = getchar();
    while (c < '0' || c > '9') {
        if (c == '-') flg = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') {
        p = p * 10 + c - '0';
        c = getchar();
    }
    return p * flg;
}
void write(int x) {
    if (x < 0) {
        x = -x;
        putchar('-');
    }
    if (x > 9) {
        write(x / 10);
    }
    putchar(x % 10 + '0');
}

#define fi first
#define se second

const int N = 3e5 + 5, M = 2e6 + 5, inf = 2e18;

namespace G {

array <int, N> fir;
array <int, M> nex, to, cap;
int cnt = 1;
void add(int x, int y, int z) {
    cnt++;
    nex[cnt] = fir[x];
    to[cnt] = y;
    cap[cnt] = z;
    fir[x] = cnt;
}
void _add(int x, int y, int z) {
    add(x, y, z);
    add(y, x, 0);
}

}

namespace Mfl {

array <int, N> dis, cur;
queue <int> q;

bool bfs(pii st) {
    dis.fill(-1), dis[st.fi] = 0;
    q.push(st.fi);
    while (!q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = G::fir[u]; i; i = G::nex[i]) {
            if (G::cap[i] <= 0 || ~dis[G::to[i]]) continue;
            dis[G::to[i]] = dis[u] + 1; q.push(G::to[i]);
        }
    }
    return ~dis[st.se];
}

int dfs(int x, int augcd, pii st) {
    if (x == st.se) return augcd;
    int augc = augcd;
    for (int &i = cur[x]; i; i = G::nex[i]) {
        int flow = G::cap[i];
        if (flow <= 0 || dis[G::to[i]] <= dis[x]) continue;
        int del = dfs(G::to[i], min(augc, flow), st);
        augc -= del;
        G::cap[i] -= del, G::cap[i ^ 1] += del;
        if (augc <= 0) break;
    }
    return augcd - (augc < 0 ? 0 : augc);
}

int dinic(pii st) {
    int ans = 0;
    while (bfs(st)) {
        // copy(G::fir.begin(), G::fir.end(), cur.begin());
        cur = G::fir;
        ans += dfs(st.fi, inf, st);
    }
    return ans;
}

}

array <array <array <int, 55>, 55>, 55> mp;

int p, q, r;

int _dx[5] = {0, 1, 0, -1, 0}, _dy[5] = {0, 0, 1, 0, -1};

signed main() {
    p = read(), q = read(), r = read();
    int d = read();
    for (int k = 1; k <= r; k++)
        for (int i = 1; i <= p; i++)
            for (int j = 1; j <= q; j++)
                mp[i][j][k] = read();
    pii st = make_pair(1, p * q * (r + 1) + 2);

    for (int i = 1; i <= p; i++) {
        for (int j = 1; j <= q; j++) {
            int x = (i - 1) * q + j + 1;
            G::_add(st.fi, x, inf);
            for (int k = 1; k <= r; k++)
                G::_add(p * q * (k - 1) + x, p * q * k + x, mp[i][j][k]);
            for (int k = d + 1; k <= r + 1; k++) {
                for (int l = 1; l <= 4; l++) {
                    int _i = i + _dx[l], _j = j + _dy[l];
                    if (_i < 1 || _j < 1 || _i > p || _j > q) continue;
                    G::_add(p * q * (k - 1) + (i - 1) * q + j + 1, p * q * (k - d - 1) + (_i - 1) * q + _j + 1, inf);
                }
            }
            G::_add(p * q * r + x, st.se, inf);
        }
    }
    write(Mfl::dinic(st)), puts("");
    return 0;
}

标签:切糕,include,int,cnt,st,HNOI2013,add,P3227,define
From: https://www.cnblogs.com/cxqghzj/p/17893215.html

相关文章

  • P3227 [HNOI2013]切糕
    P3227[HNOI2013]切糕题意给定一个\(P\timesQ\)的平面,平面上每一个点上都有一个高度为\(R\)的竖条。竖条上每一个点都有一个不和谐度\(f(x,y,z)\),对于每一个竖条选一个点,要求与周围的点的高度差不超过\(d\)(四联通),求最小不和谐度。题解感觉这道题很神啊,首先我们考......
  • [THUPC2021 初赛] 切切糕
    个人思路:从小往大切,感性理解一下。由于每个人都足够聪明,博弈dp只有后效型而没有前效性,所以从固定的最终状态倒序往前dp,得到初始状态的答案。状态:\(dp_{i,j}\)表示还......
  • 网络流之广义切糕模型
    问题有\(n\)个整数变量\(x_i\)。\(x_i\)可以取\([1,m]\),取\(j\)需要\(a_{i,j}\)的代价。有若干个约束,形如\(x_{u_i}\lex_{v_i}+w_i\)。给变量赋值,最小化总......
  • P7137 [THUPC2021 初赛] 切切糕(博弈 概率)
    P7137[THUPC2021初赛]切切糕->双倍经验:GameonSum(HardVersion)有\(n\)块方蛋糕,绝顶聪明的Sight和Sirrel决定将每块蛋糕都分成两块各自品尝。Sight会依次......
  • P3232 [HNOI2013]游走
    Link可以将期望用边权\(w_1,w_2,\cdots,w_n\)表示,考虑分别求出其系数。当然,直接算的话复杂度会寄。考虑将边的期望放到点上。\(E(u,v)=\dfrac{E(u)}{d_u}+\dfrac{E(v)......
  • luogu P3232 [HNOI2013]游走 (期望, 高斯消元)
    https://www.luogu.com.cn/problem/P3232思路:算出每条边的期望访问次数,将期望访问次数多的赋予小的编号。一条边的期望访问次数=访问点u的期望/u的度+访问点v的期望......