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. Spring实例化相关问题

    1.当Controller或者Service使用new来实例化时,能不能正常调用使用Resource声明的变量 不能,使用new来实例化时,所有使用Resource声明的变量均为null

  2. MongoDB下载以及安装

    一.下载与安装 1.安装Mongo MongoDB下载地址:https://www.mongodb.com/download-center?jmp=tutorials#community 运行安装程序 ...

  3. [洛谷P1501] [国家集训队]Tree II(LCT模板)

    传送门 这是一道LCT的板子题,说白了就是在LCT上支持线段树2的操作. 所以我只是来存一个板子,并不会讲什么(再说我也不会,只能误人子弟2333). 不过代码里的注释可以参考一下. Code #in ...

  4. 4000余字为你讲透Codis内部工作原理

    一.引言 Codis是一个分布式 Redis 解决方案,可以管理数量巨大的Redis节点.个推作为专业的第三方推送服务商,多年来专注于为开发者提供高效稳定的消息推送服务.每天通过个推平台下发的消息数量 ...

  5. 后盾网lavarel视频项目---lavarel使用模型进行增删改查操作

    后盾网lavarel视频项目---lavarel使用模型进行增删改查操作 一.总结 一句话总结: 使用模型操作常用方法 查一条:$model=Tag::find($id); 删一条:Tag::dest ...

  6. leetcode 215 第K个最大的元素

    此问题可转化为Top K问题进行考虑,当用小顶堆选出K个最大值时,堆顶的元素即为第k大的元素 class Solution { public: int findKthLargest(vector< ...

  7. leetcode 140 单词拆分2 word break II

    单词拆分2,递归+dp, 需要使用递归,同时使用记忆化搜索保存下来结果,c++代码如下 class Solution { public: //定义一个子串和子串拆分(如果有的话)的映射 unorder ...

  8. 自定义ListView实现下拉刷新,下拉加载的功能

    package com.loaderman.myrefreshlistviewdemo; import android.content.Context; import android.util.Att ...

  9. InputNumber计数器

    InputNumber 计数器 仅允许输入标准的数字值,可定义范围 要使用它,只需要在el-input-number元素中使用v-model绑定变量即可,变量的初始值即为默认值. <templa ...

  10. 问题:unknown error: call function result missing 'value' 解决方法

    问题:unknown error: call function result missing 'value' 页面也没有 填充信息 原因是:安装与chrome和对应的chromedriver版本问题 ...