「CF894E」 Ralph and Mushrooms
Posted zsbzsb
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了「CF894E」 Ralph and Mushrooms相关的知识,希望对你有一定的参考价值。
传送门
Luogu
解题思路
首先我们要发现:在同一个强连通分量里的所有边都是可以无限走的。
那么我们就有了思路:先缩点,再跑拓扑排序。
那么问题就是 ( ext{DP}) 状态如何初始化。
我们首先考虑一条原始边权为 (c) 的边,无限走可以刷出多少贡献:
假设我们走 (t) 次就可以把这条边刷完,那么 (t) 应该是满足下面这个式子的最大整数:
[frac{t(t+1)}{2}< c]
解得:
[t=leftlfloorsqrt{2t+frac{1}{4}}-frac{1}{2}
ight
floor]
那么我们的贡献就是:
[egin{aligned}sum&=sum_{i=0}^{t}left(c-sum_{j=0}^{i}j
ight)\&=(t+1)c-sum_{i=0}^{t}frac{i(i+1)}{2}\&=(t+1)c-frac{1}{2}left(sum_{i=0}^{t}i^2+sum_{i=0}^{t}i
ight)\&=(t+1)c-frac{1}{2}left(frac{t(t+1)(2t+1)}{6}+frac{t(t+1)}{2}
ight)\&=(t+1)c-frac{t(t+1)(t+2)}{6}end{aligned}]
于是我们就解决了这道题,最后输出 (maxlimits_{1le ile col}{f[i]}) 即可。
细节注意事项
- 由于 ( ext{tarjan}) 缩点时对边的操作不方便,可以在外部处理
参考代码
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <ctime>
#include <queue>
#define rg register
#define pii pair < int, int >
using namespace std;
template < typename T > inline void read(T& s) {
s = 0; int f = 0; char c = getchar();
while (!isdigit(c)) f |= (c == '-'), c = getchar();
while (isdigit(c)) s = s * 10 + (c ^ 48), c = getchar();
s = f ? -s : s;
}
typedef long long LL;
const int _ = 1000010;
const LL INF = 1ll << 60;
int tot, head[_], nxt[_], ver[_]; LL w[_];
inline void Add_edge(int u, int v, LL d)
{ nxt[++tot] = head[u], head[u] = tot, ver[tot] = v, w[tot] = d; }
int n, m, s, x[_], y[_]; LL c[_];
int num, dfn[_], low[_];
int top, st[_], col, co[_];
int dgr[_]; LL val[_], f[_];
inline void tarjan(int u) {
dfn[u] = low[u] = ++num, st[++top] = u;
for (rg int i = head[u]; i; i = nxt[i]) {
int v = ver[i];
if (!dfn[v])
tarjan(v), low[u] = min(low[u], low[v]);
else
if (!co[v]) low[u] = min(low[u], dfn[v]);
}
if (low[u] == dfn[u]) {
++col;
do co[st[top]] = col;
while (st[top--] != u);
}
}
inline LL calc(LL c) {
LL t = sqrt(c * 2 + 0.25) - 0.5;
return (t + 1) * c - t * (t + 1) * (t + 2) / 6;
}
inline void rebuild() {
for (rg int i = 1; i <= m; ++i)
if (co[x[i]] == co[y[i]])
val[co[x[i]]] += calc(c[i]);
memset(head, tot = 0, sizeof head);
for (rg int i = 1; i <= m; ++i)
if (co[x[i]] != co[y[i]])
Add_edge(co[x[i]], co[y[i]], c[i] + val[co[y[i]]]), ++dgr[co[y[i]]];
}
inline LL toposort() {
LL _max = 0;
static queue < int > Q;
for (rg int i = 1; i <= col; ++i) {
if (dgr[i] == 0) Q.push(i); f[i] = -INF;
}
f[co[s]] = val[co[s]];
while (!Q.empty()) {
int u = Q.front(); Q.pop();
for (rg int v, i = head[u]; i; i = nxt[i]) {
if (!--dgr[v = ver[i]]) Q.push(v);
f[v] = max(f[v], f[u] + w[i]);
}
}
for (rg int i = 1; i <= col; ++i) _max = max(_max, f[i]);
return _max;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.in", "r", stdin);
#endif
read(n), read(m);
for (rg int i = 1; i <= m; ++i)
read(x[i]), read(y[i]), read(c[i]), Add_edge(x[i], y[i], c[i]);
read(s);
for (rg int i = 1; i <= n; ++i) if (!dfn[i]) tarjan(i);
rebuild();
printf("%lld
", toposort());
return 0;
}
完结撒花 (qwq)
以上是关于「CF894E」 Ralph and Mushrooms的主要内容,如果未能解决你的问题,请参考以下文章
[2019CSP-S赛前训练][CF894E] Ralph and Mushrooms
CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)
Codeforces 894 B Ralph And His Magic Field
codeforces 894B - Ralph And His Magic Field - [数学题]