费用流。
把每个方格拆成 $T$ 个点,$t$ 时刻一个方格向周围四个方格的 $t + 1$ 的点连一条容量为 $1$ 费用为 $0$ 的边,向自身的 $t + 1$ 连一条容量为 $1$ 费用为该方格最大幸福值的边。
源点向方格为 'S' 的0时刻连一条容量为 $1$ 费用为 $0$ 的边。所有点的 $T - 1$ 时刻向汇点连一条容量为 $1$ 费用为该方格最大幸福值的边。
还有每个格子同时刻不能有多条蛇呆在上面,再把每个点每个时刻拆成两个点,容量为 $1$ 费用为 $0$。跑最大费用最大流即可。

#include <bits/stdc++.h>
using namespace std; const int N = ;
const int NN = 1e5 + , M = 5e5 + ;
const int INF = 0x3f3f3f3f; template<class T>
inline void checkmax(T &a, T b) {
if (a < b) a = b;
}
template<class T>
inline void checkmin(T &a, T b) {
if (a > b) a = b;
} struct E {
int v, ne, f, c;
} e[M];
int head[NN], cnt, tol;
int id[N][N][], mp[N][N][];
int dis[NN], path[NN], n, m, z, t;
bool inq[NN];
char str[N][N];
const int dx[] = {, , , -}, dy[] = {-, , , }; inline void add(int u, int v, int f, int c) {
e[cnt].v = v; e[cnt].f = f; e[cnt].c = c; e[cnt].ne = head[u]; head[u] = cnt++;
e[cnt].v = u; e[cnt].f = ; e[cnt].c = -c; e[cnt].ne = head[v]; head[v] = cnt++;
} bool spfa(int s, int t) {
for (int i = ; i <= t; i++)
dis[i] = INF, inq[i] = , path[i] = -;
dis[s] = ;
inq[s] = ;
queue<int> que;
que.push(s);
while (!que.empty()) {
int u = que.front(); que.pop();
inq[u] = ;
for (int i = head[u]; ~i; i = e[i].ne) {
int v = e[i].v, c = e[i].c;
if (e[i].f && dis[v] > dis[u] + c) {
dis[v] = dis[u] + c;
path[v] = i;
if (!inq[v]) {
inq[v] = ;
que.push(v);
}
}
}
}
return dis[t] != INF;
} int mcf(int s, int t) {
int ans = ;
while (spfa(s, t)) {
for (int i = path[t]; ~i; i = path[e[i ^ ].v]) e[i].f--, e[i ^ ].f++;
ans += dis[t];
}
return ans;
} int main() {
//freopen("in.txt", "r", stdin);
memset(head, -, sizeof(head));
scanf("%d%d%d%d", &n, &m, &z, &t);
for (int i = ; i <= n; i++)
scanf("%s", str[i] + );
for (int i = ; i <= z; i++) {
int x, y, p, q, h;
scanf("%d%d%d%d%d", &x, &y, &p, &q, &h);
for (int j = p; j < q; j++)
checkmax(mp[x][y][j], h);
}
tol = ;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++) if (str[i][j] != '#')
for (int k = ; k < t; k++) {
add(tol, tol + , , );
//cout << tol << ' ' << tol + 1 << endl;
id[i][j][k] = tol;
tol += ;
}
int S = tol + , T = tol + ;
for (int i = ; i <= n; i++)
for (int j = ; j <= m; j++) if (id[i][j][]) {
for (int d = ; d < ; d++) if (id[i + dx[d]][j + dy[d]][])
for (int k = ; k < t - ; k++)
add(id[i][j][k] + , id[i + dx[d]][j + dy[d]][k + ], , );
for (int k = ; k < t - ; k++)
add(id[i][j][k] + , id[i][j][k + ], , -mp[i][j][k]);
add(id[i][j][t - ] + , T, , -mp[i][j][t - ]);
if (str[i][j] == 'S') add(S, id[i][j][], , );
}
printf("%d\n", -mcf(S, T));
return ;
}

Codechef July Challenge 2019 Snake and Apple Tree的更多相关文章

  1. Codechef July Challenge 2019 Division 1题解

    题面 \(CIRMERGE\) 破环成链搞个裸的区间\(dp\)就行了 //quming #include<bits/stdc++.h> #define R register #defin ...

  2. Codechef July Challenge 2019 Hit the Coconuts

    假设现在有一堆数,我想要保证能取出一个,至少需要敲 (数的个数)*(这些数里的最小值)那么把这些数从大到小排序,$dp[i][j]$ 表示前 $i$ 个里面保证能取出 $j$ 个需要敲的次数.$dp[ ...

  3. Codechef April Challenge 2019 游记

    Codechef April Challenge 2019 游记 Subtree Removal 题目大意: 一棵\(n(n\le10^5)\)个结点的有根树,每个结点有一个权值\(w_i(|w_i\ ...

  4. [codechef July Challenge 2017] Pishty and tree

    PSHTTR: Pishty 和城堡题目描述Pishty 是生活在胡斯特市的一个小男孩.胡斯特是胡克兰境内的一个古城,以其中世纪风格的古堡和非常聪明的熊闻名全国.胡斯特的镇城之宝是就是这么一座古堡,历 ...

  5. Codechef December Challenge 2014 Chef and Apple Trees 水题

    Chef and Apple Trees Chef loves to prepare delicious dishes. This time, Chef has decided to prepare ...

  6. CodeChef April Challenge 2019题解

    传送门 \(Maximum\ Remaining\) 对于两个数\(a,b\),如果\(a=b\)没贡献,所以不妨假设\(a<b\),有\(a\%b=a\),而\(b\%a<a\).综上, ...

  7. Codechef April Challenge 2019 Division 2

    Maximum Remaining 题意:给n个数,取出两个数$a_{i}$,$a_{j}$,求$a_{i}\% a_{j}$取模的最大值 直接排个序,第二大(严格的第二大)模第一大就是答案了. #i ...

  8. Codechef November Challenge 2019 Division 1

    Preface 这场CC好难的说,后面的都不会做QAQ 还因为不会三进制位运算卷积被曲明姐姐欺负了,我真是太菜了QAQ PS:最后还是狗上了六星的说,期待两(三)场之内可以上七星 Physical E ...

  9. Codechef October Challenge 2019 Division 1

    Preface 这次CC难度较上两场升高了许多,后面两题都只能借着曲明姐姐和jz姐姐的仙气来做 值得一提的是原来的F大概需要大力分类讨论,结果我写了一大半题目就因为原题被ban了233 最后勉强涨了近 ...

随机推荐

  1. [转帖]什么是IOC(控制反转)、DI(依赖注入)

    什么是IOC(控制反转).DI(依赖注入) 2018-08-22 21:29:13 Ming339456 阅读数 20642   原文地址(摘要了部分内容):https://blog.csdn.net ...

  2. [转帖]在 Kubernetes 离线部署 KubeSphere

    在 Kubernetes 离线部署 KubeSphere https://kubesphere.io/docs/v2.0/zh-CN/installation/install-ks-offline/ ...

  3. Nginx配置REWRITE隐藏index.php

    server { listen 80; server_name localhost; root D:\workspace\PHP\Atromic; location / { index index.p ...

  4. memcpy() 实现循环缓冲区数据的读写

    使用memcpy()函数做循环缓冲区的读写 首先对mencpy函数做个简单的介绍 下面是 memcpy() 函数的声明 void *memcpy(void *str1, const void *str ...

  5. 啊哈!算法(第二章)C#实现

    第 1 节 解密 QQ 号——队列   新学期开始了,小哈是小哼的新同桌(小哈是个小美女哦~),小哼向小哈询问 QQ 号,小哈当然不会直接告诉小哼啦,原因嘛你懂的. 所以小哈给了小哼一串加密过的数字, ...

  6. 用shell脚本批量进行xss跨站攻击请求

    由于执行的xss攻击请求他多了,初步估计要执行83次,而且还要执行3篇,如果手工一个一个去执行,说出去,我还配叫自动化大师吗: 有鉴于此,边打算自己编写一个脚本进行批量执行: 而短脚本的编写,非she ...

  7. 不用Pageant告别Pageant Windows10下TortoiseGit和Git配置使用同一SSH密钥

    关于Git使用SSH免密连接参考:https://blog.csdn.net/qq_32786873/article/details/80570788 关于Windows10下TortoiseGit使 ...

  8. Java的Stream流式操作

    前言 最近在实习,在公司看到前辈的一些代码,发现有很多值得我学习的地方,其中有一部分就是对集合使用Stream流式操作,觉得很优美且方便.所以学习一下Stream流,在这里记录一下. Stream是什 ...

  9. Java自学-类和对象 属性初始化

    如何进行Java的属性初始化 步骤 1 : 对象属性初始化 对象属性初始化有3种 声明该属性的时候初始化 构造方法中初始化 初始化块 . public class Hero { public Stri ...

  10. 【转】Webpack 快速上手(下)

    由于文章篇幅较长,为了更好的阅读体验,本文分为上.中.下三篇: 上篇介绍了什么是 webpack,为什么需要 webpack,webpack 的文件输入和输出 中篇介绍了 webpack 在输入和输出 ...