Link

一眼题。

缩点然后dp。

注意一下计算一条边经过无限次可以获得多少价值这个东西要用到平方和公式。

\(\sum\limits_{i=1}^ni^2=\frac{i(i+1)(2i+1)}6\)

#include<bits/stdc++.h>
#define ll long long
#define pb push_back
#define pi pair<int,int>
using namespace std;
int read(){int x;scanf("%d",&x);return x;}
int min(int a,int b){return a<b? a:b;}
ll max(ll a,ll b){return a>b? a:b;}
const int N=1000007;
vector<int>G[N];vector<pi>E[N];
int Time,cnt,dfn[N],low[N],bel[N],stk[N],top,vis[N];ll val[N],f[N];
struct edge{int u,v,w;}e[N];
ll cal(int x){int n=(sqrt(1+x*8)-1)/2;return (n+1ll)*x-1ll*n*(n+1)*(n+2)/6;}
void tarjan(int u)
{
dfn[u]=low[u]=++Time,vis[stk[++top]=u]=1;
for(int v:G[u])
if(!dfn[v]) tarjan(v),low[u]=min(low[u],low[v]);
else if(vis[v]) low[u]=min(low[u],dfn[v]);
if(low[u]==dfn[u]) for(++cnt;stk[top+1]^u;--top) bel[stk[top]]=cnt,vis[stk[top]]=0;
}
int main()
{
int n=read(),m=read(),s;
for(int i=1;i<=m;++i) e[i]=(edge){read(),read(),read()},G[e[i].u].pb(e[i].v);
tarjan(s=read());
for(int i=1;i<=m;++i)
if(bel[e[i].u]&&bel[e[i].v])
if(bel[e[i].u]==bel[e[i].v]) val[bel[e[i].u]]+=cal(e[i].w);
else E[bel[e[i].u]].pb(pi(bel[e[i].v],e[i].w));
for(int u=1;u<=cnt;++u)
{
for(auto[v,w]:E[u]) f[u]=max(f[u],f[v]+w);
f[u]+=val[u];
}
printf("%lld",f[bel[s]]);
}

CF894E Ralph and Mushrooms的更多相关文章

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

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

  2. Codeforces 894.E Ralph and Mushrooms

    E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...

  3. 「CF894E」 Ralph and Mushrooms

    传送门 Luogu 解题思路 首先我们要发现:在同一个强连通分量里的所有边都是可以无限走的. 那么我们就有了思路:先缩点,再跑拓扑排序. 那么问题就是 \(\text{DP}\) 状态如何初始化. 我 ...

  4. Codeforces Round #447 (Div. 2)E. Ralph and Mushrooms

    Ralph is going to collect mushrooms in the Mushroom Forest. There are m directed paths connecting n  ...

  5. CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)

    题意:一张有向图,每条边上都有wi个蘑菇,第i次经过这条边能够采到w-(i-1)*i/2个蘑菇,直到它为0.问最多能在这张图上采多少个蘑菇. 分析:在一个强连通分量内,边可以无限次地走直到该连通块内蘑 ...

  6. 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP

    题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...

  7. CF894E Ralph and Mushrooms_强连通分量_记忆化搜索_缩点

    Code: #include<cstdio> #include<stack> #include<cstring> using namespace std; cons ...

  8. [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)

    题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...

  9. Codeforces Round #447 (Div. 2) 题解 【ABCDE】

    BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...

随机推荐

  1. 字典树Trie--实现敏感词过滤

    序言 Trie树 资料 https://blog.csdn.net/m0_37907797/article/details/103272967?utm_source=apphttps://blog.c ...

  2. Angular 应用的外壳

    你首先需要使用 Angular CLI 来创建一个初始化的应用.随后,你将对你已经初始化的应用进行修改来让你构建出 Tour of Heroes app(英雄指南) 这个应用. 在教程的本部分,你需要 ...

  3. SQLMAP自注入--INJECTION TECGBUQUES FINGERPRINT

    -p参数 指定扫描的参数 ,使--level失效 -p“user-agent,refer”这些参数也可以通过-p来指定 sqlmap.py -u "http://127.0.0.1/muti ...

  4. summernote(富文本编辑器)将附件与图片上传到自己的服务器(vue项目)

    1.上传图片至自己的服务器(这个官方都有例子,重点介绍附件上传)图片上传官方网址 // onChange callback $('#summernote').summernote({ callback ...

  5. 乌班图docker版本18.04升级到19.03

    # 关闭docker sudo systemctl stop docker # 卸载旧版本: sudo apt-get purge docker-ce # 安装新版本 sudo apt update ...

  6. 自动化框架Quantum Automation Framework+cucumber+perfecto

    名词解释 Quantum: 一款基于JAVA的自动化框架,支持手机和桌面WEB的自动化测试.与cucumber和perfecto实现了整合,用于BDD自动化. Refer: http://projec ...

  7. leetcode 29两数相除

    我理解本题是考察基于加减实现除法,代码如下: class Solution { public: //只用加减号实现除法, //不用加减号实现除法: int divide(int dividend, i ...

  8. save——model模块保存和载入使用简单例子

    https://www.w3xue.com/exp/article/201812/10995.html =====1====实践模型存入 import tensorflow as tf from te ...

  9. python接口测试之mock(二)

    上一篇对mock-server已经做了初步的介绍,今天这里继续接着之前的介绍进行,我们先看之前的mock-server部分,之前编写了一个登录的mock,具体json文件见如下的内容: 小王子1110 ...

  10. JavaScript日常学习6

    JavaScript的运算符.比较符.条件语句.循环语句.跳出循环(break.continue).标签 JavaScript的运算符.比较符.条件语句.循环语句.跳出循环(break.continu ...