「CF894E」 Ralph and Mushrooms
传送门
Luogu
解题思路
首先我们要发现:在同一个强连通分量里的所有边都是可以无限走的。
那么我们就有了思路:先缩点,再跑拓扑排序。
那么问题就是 \(\text{DP}\) 状态如何初始化。
我们首先考虑一条原始边权为 \(c\) 的边,无限走可以刷出多少贡献:
假设我们走 \(t\) 次就可以把这条边刷完,那么 \(t\) 应该是满足下面这个式子的最大整数:
\]
解得:
\]
那么我们的贡献就是:
\]
于是我们就解决了这道题,最后输出 \(\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的更多相关文章
- 【题解】CF894E Ralph and Mushrooms (缩点)
[题解]CF894E Ralph and Mushrooms (缩点) 这是紫?给个解方程算法 考虑一条边若可以重复遍历说明一定有环,有环的话一定会把环上的蘑菇榨干,考虑一条边从全部到榨干的贡献是多少 ...
- 「译」JUnit 5 系列:条件测试
原文地址:http://blog.codefx.org/libraries/junit-5-conditions/ 原文日期:08, May, 2016 译文首发:Linesh 的博客:「译」JUni ...
- 「译」JUnit 5 系列:扩展模型(Extension Model)
原文地址:http://blog.codefx.org/design/architecture/junit-5-extension-model/ 原文日期:11, Apr, 2016 译文首发:Lin ...
- JavaScript OOP 之「创建对象」
工厂模式 工厂模式是软件工程领域一种广为人知的设计模式,这种模式抽象了创建具体对象的过程.工厂模式虽然解决了创建多个相似对象的问题,但却没有解决对象识别的问题. function createPers ...
- 「C++」理解智能指针
维基百科上面对于「智能指针」是这样描述的: 智能指针(英语:Smart pointer)是一种抽象的数据类型.在程序设计中,它通常是经由类型模板(class template)来实做,借由模板(tem ...
- 「JavaScript」四种跨域方式详解
超详细并且带 Demo 的 JavaScript 跨域指南来了! 本文基于你了解 JavaScript 的同源策略,并且了解使用跨域跨域的理由. 1. JSONP 首先要介绍的跨域方法必然是 JSON ...
- 「2014-5-31」Z-Stack - Modification of Zigbee Device Object for better network access management
写一份赏心悦目的工程文档,是很困难的事情.若想写得完善,不仅得用对工具(use the right tools),注重文笔,还得投入大把时间,真心是一件难度颇高的事情.但,若是真写好了,也是善莫大焉: ...
- 「2014-3-18」multi-pattern string match using aho-corasick
我是擅(倾)长(向)把一篇文章写成杂文的.毕竟,写博客记录生活点滴,比不得发 paper,要求字斟句酌八股结构到位:风格偏杂文一点,也是没人拒稿的.这么说来,arxiv 就好比是 paper 世界的博 ...
- 「2014-3-17」C pointer again …
记录一个比较基础的东东-- C 语言的指针,一直让人又爱又恨,爱它的人觉得它既灵活又强大,恨它的人觉得它太过于灵活太过于强大以至于容易将人绕晕.最早接触 C 语言,还是在刚进入大学的时候,算起来有好些 ...
随机推荐
- IntelliJ IDEA 2017.3尚硅谷-----配置 Maven
- JavaScript——BOM和DOM
什么是BOM bom:浏览器对象模型 什么是DOM dom:文档对象模型 BOM操作: 调用windows浏览器窗口 windows对象可以通过点调用子对象 windows.navigator对象,可 ...
- BigInteger和BigDecimal的基本用法
整型大数 BigInteger: import java.math.BigInteger; import java.util.Scanner; public class Main { public s ...
- 1032 Sharing (25分)
1032 Sharing (25分) 题目 思路 定义map存储所有的<地址1,地址2> 第一set存放单词1的所有地址(通过查找map) 通过单词二的首地址,结合map,然后在set中查 ...
- gradle-技能保存
gradle编译java springboot,指定使用哪个环境配置文件 首先在build.gradle里面声明一个变量 def profileName = project.hasProperty(& ...
- 【C语言】用C语言输出“心形”图案
在你们的世界里,是不是觉得程序猿一点浪漫都不懂?其实不是的,程序猿的世界也是很浪漫滴! 傻瓜版 int main() { printf("❤"); ; } 高级版 //版本一:单个 ...
- 【C语言】指针函数例子
#include<stdio.h> char* getword(char); char* getword(char c) { switch (c) { case'A':return&quo ...
- 「模板」AC自动机
目录 说明 普通版本 询问更改版 拓扑优化版本 说明 这篇博客只挂模板,具体分析请膜拜大佬 hyfhaha 大佬. 普通版本 题目传送门 #include<cstdio> #include ...
- Caffe 笔记 (一)caffe的层与数据结构
Caffe是纯粹的C++/CUDA架构,支持命令行.Python和MATLAB接口:可以在CPU和GPU直接无缝切换: Caffe::set_mode(Caffe::GPU); Caffe的优势 1. ...
- composer 配置 切换中国镜像
composer 默认地址改为中国镜像地址,以及中国镜像地址还原成默认地址 一.查看当前镜像地址 在命令行输入如下命令,即可查看全局镜像地址: $ composer config -g repo.pa ...