传送门

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. Redis09——事务(悲观锁、乐观锁)

    事务 定义: Redis事务是一个单独的隔离操作 ①事务中所有的命令都会被序列化.按照顺序执行 ②事务在执行过程中不会被其他客户端发送来的命令请求打断 作用: 串联多个命令防止别的命令插队 multi ...

  2. C# MVC扩展方法

    控制方法 : 在不修改类原来代码的情况下,给类扩展方法 https://www.cnblogs.com/jxsimon/articles/5043654.html https://www.cnblog ...

  3. AcWing 两个简单的位运算操作

    //是柱状数组的一个基操作 //返回n的最后一位1:lowbit(n) = n & -n //比如 x=1010 那么返回10 x=101000 返回1000 #include<bits ...

  4. 在一个formitem中多input的验证方法-antd的验证

    实现效果如下: 当点击按钮的时候 对一个FormItem里的多个input/或者是input和select进行校验  同时通过Rol/Col实现布局 Rselect/input组件封装的组件如下fie ...

  5. 【C语言】输入三个正整数a,b,c,求最大值,要求定义一个计算最大值的函数max(a,b),返回a,b的值

    #include<stdio.h> int max(int a, int b)/*定义函数*/ { if (a > b) return a; else return b; } int ...

  6. robots.txt 让搜索引擎不再收录网站

    有没有担心过自己的隐私会在强大的搜索引擎面前无所遁形?想象一下,如果要向世界上所有的人公开你的私人日记,你能接受吗?的确是很矛盾的问题,站长们大都忧虑“如何让搜索引擎收录的我的网站?”,而我们还是要研 ...

  7. PMP概略学习下--主体内容

    4  知识主体 4.1 主要知识简介 PMP所有的知识围绕五大过程组和十大知识领域展开.五大过程组包括启动.规划.执行.监控.结尾.启动的内容主要是定义项目或阶段.获得授权以及正式开始:规划的内容主要 ...

  8. 运行时错误'430': 类不支持自动化或不支持期望的接口。New ADODB.Connection问题

    运行时错误'430': 类不支持自动化或不支持期望的接口. 问题: 经调试发现,是运行到下面语句时出错:Set MyConn = New ADODB.Connection  运行到这里是提示本错误 这 ...

  9. 移动端 输入框 input 被弹出来的键盘 挡住

    给被挡住的input或者textarea加一个id,然后在click事件里调用下面的代码 document.querySelector('#xxx').scrollIntoView();

  10. vue 获得当前无素并做相应处理

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