传送门

Luogu

解题思路

首先我们要发现:在同一个强连通分量里的所有边都是可以无限走的。

那么我们就有了思路:先缩点,再跑拓扑排序。

那么问题就是 \(\text{DP}\) 状态如何初始化。

我们首先考虑一条原始边权为 \(c\) 的边,无限走可以刷出多少贡献:

假设我们走 \(t\) 次就可以把这条边刷完,那么 \(t\) 应该是满足下面这个式子的最大整数:

\[\frac{t(t+1)}{2}< c
\]

解得:

\[t=\left\lfloor\sqrt{2t+\frac{1}{4}}-\frac{1}{2}\right\rfloor
\]

那么我们的贡献就是:

\[\begin{aligned}sum&=\sum_{i=0}^{t}\left(c-\sum_{j=0}^{i}j\right)\\&=(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\right)\\&=(t+1)c-\frac{1}{2}\left(\frac{t(t+1)(2t+1)}{6}+\frac{t(t+1)}{2}\right)\\&=(t+1)c-\frac{t(t+1)(t+2)}{6}\end{aligned}
\]

于是我们就解决了这道题,最后输出 \(\max\limits_{1\le i\le col}\{f[i]\}\) 即可。

细节注意事项

  • 由于 \(\text{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\n", toposort());
return 0;
}

完结撒花 \(qwq\)

「CF894E」 Ralph and Mushrooms的更多相关文章

  1. 【题解】CF894E Ralph and Mushrooms (缩点)

    [题解]CF894E Ralph and Mushrooms (缩点) 这是紫?给个解方程算法 考虑一条边若可以重复遍历说明一定有环,有环的话一定会把环上的蘑菇榨干,考虑一条边从全部到榨干的贡献是多少 ...

  2. 「译」JUnit 5 系列:条件测试

    原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...

  3. 「译」JUnit 5 系列:扩展模型(Extension Model)

    原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...

  4. JavaScript OOP 之「创建对象」

    工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...

  5. 「C++」理解智能指针

    维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...

  6. 「JavaScript」四种跨域方式详解

    超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...

  7. 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management

    写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...

  8. 「2014-3-18」multi-pattern string match using aho-corasick

    我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...

  9. 「2014-3-17」C pointer again …

    记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...

随机推荐

  1. mysql数据库事务的操作与理解

    --------------------事务----------------------------------------------查询mysql事务隔离级别1.查看当前会话隔离级别select ...

  2. 【算法学习记录-排序题】【PAT A1012】The Best Rank

    To evaluate the performance of our first year CS majored students, we consider their grades of three ...

  3. caffe.bin用法

    $ ./build/tools/caffe.bin caffe.bin: command line brew usage: caffe <command><aegs> comm ...

  4. windows10打开switchHost,提示无修改权限

    1.在C盘找到hsot文件,点击属性,去掉只读,去掉勾选. 点击编辑 点击Users,选择完全控制,这回降低电脑安全! 确定.

  5. AAC huffman decoding

    在AAC编码器内部,使用huffman coding用于进一步减少scalefactor和量化频谱系数的冗余. 从individual_channel_stream层提取码流进行huffman解码,码 ...

  6. ubuntu 切换用户

    app切换root ubuntu: sudo su - app sudo su - root centos : sudo su ############ root 切换app sudo su - ap ...

  7. 题解 P2146 [NOI2015]软件包管理器

    P2146 [NOI2015]软件包管理器 感觉代码比其他题解更简洁qwq 树链剖分模板题 install x:将1~x的路径上的节点全部变成1(安装x需要先安装1~x) uninstall x:将x ...

  8. Python六剑客

    1.切片 切片:截取可迭代对象的部分内容(list,tuple,dict,set,str) 2.列表解析式 列表解析式可以快速的生成一个列表 不带if条件的: 格式:[expression for i ...

  9. thinkphp的模型操作

    先开个坑 WHERE篇 1, 模糊查询    where['keyword'] = [ 'like' , '%test%'] 2,   不等于,大于 ,小于 EQ 等于(=)NEQ 不等于(<& ...

  10. php一些实用的自制方法

    时间戳转多久之前 function mdate($time = NULL) { $text = ''; $time = $time === NULL || $time > time() ? ti ...