费用流。
把每个方格拆成 $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. 给定一个数组,求如果排序后,相邻两个元素的最大差值,要求时间复杂度为O(N)

    第一种方法: 计数排序后,然后找出两两之间的最大差值 计数排序的时间复杂度是O(N) public class CountSort { public static void main(String[] ...

  2. java自带的xml解析工具类

    public class JaxbUtil { /** * java对象转换为xml文件 * * @param xmlPath xml文件路径 * @param load java对象.Class * ...

  3. markdown使用emoji

    前几日写博客的时候在想是否能够在markdown中使用emoji呢

  4. Bootstrap-treeView 实际操作总结

    由于功能性需求:需要展示一个树状结构的导航界面 1.进行资源引用 <!-- Required Stylesheets --> <link href="bootstrap.c ...

  5. 详解JS与Jquery获得的对象的区别与联系

    世上无难事只怕有心人,敲代码也一样只要你用心去搞懂一件事,即使一个小小的用法对你以后也会有很大的作用: 项目虽然赶得紧但是有些问题百度找完答案解决之后,也要自己梳理一遍做到心领神会!!!今天就直接来上 ...

  6. Jquery源码解析及案例分析

    本人刚学先上链接(别人写的挺好的)后期同步补上

  7. RabbitMQ学习之Routing(4)

    上一节,是广播日志message到很多的receivers. 这节,我们讲订阅其中的一个子集.例如,我们想可以把危机的error message导到log file.而仍然可以打印所有的log mes ...

  8. 33、vue中的事件修饰符.stop、.prevent、.self、.capture、.once

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  9. React 之 Redux 的使用

    Redux可以简单概况为:将需要修改的state都存入到store里,发起一个action用来描述发生了什么,用reducers描述action如何改变state tree .创建store的时候需要 ...

  10. 基于 Vue + Element 的响应式后台模板

    项目地址 https://github.com/caochangkui/vue-element-responsive-demo 主要功能 响应式侧边栏 面包屑导航(结合router.js) 路由动效 ...