「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 语言,还是在刚进入大学的时候,算起来有好些 ...
随机推荐
- SpringBoot整合WEB开发--(四)@ControllerAdvice
1.全局异常处理: @ControllerAdvice处理全局数据,一般搭配@ExceptionHandler,@ModelAttribute以及@InitBinder使用. @ControllerA ...
- crowdfunding项目03——mapper映射错误
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'TAd ...
- python之路之线程,进程,协程2
一.线程 1.创建线程 2.主线程是否等待子线程 t.setDaemon(Ture/False):默认是false,等待子线程完成,ture,表示不等待子线程结束 3.主线程等待,子线程执行 join ...
- 矩阵快速幂 裸 hdu1575
裸题,求A^n次后的对角线数字之和 #include<cstdio> #include<algorithm> #include<string.h> using na ...
- Docker常用命令及脚本
1.常用命令 docker search centos #搜索镜像 pull centos ...
- 【转载】SpringMVC框架介绍
转自:http://com-xpp.iteye.com/blog/1604183 SpringMVC框架图 SpringMVC接口解释 DispatcherServlet接口: Spring提 ...
- curl模板----php发送post,get请求
function _grab($curl,$ip='',$referer='',$postInfo='',$cookie=''){ $ch = curl_init(); curl_setopt($ch ...
- 很多win10系统用户都遇见了开机发现任务管理器中有个系统中断进程占用cpu99%的问题,
很多win10系统用户都遇见了开机发现任务管理器中有个系统中断进程占用cpu99%的问题,尝试了网上提供的方法都不能得到有效的解决.下面小编就为大家详细的介绍电脑工程师提供的正确的解决姿势. 出现系统 ...
- m大子段和 hdu1024
给出n个数,m个区间: 求选区m个区间的最大值: #include<cstdio> #include<algorithm> #include<math.h> #in ...
- 3.CRUD(增删改查)
Select 选择,查询语句 id:就是对应的namespace中的方法名: resultType:Sql语句执行的返回值: parameterType:参数类型 我们想使用查询语句首先要在UserM ...