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. USACO2018DEC GOLD

    好简单啊.. T1裸分层图最短路. T2裸容斥. T3更水的DP. 代码 T1 #include <bits/stdc++.h> #define rin(i,a,b) for(regist ...

  2. BootStrap之X-editable插件使用

    项目背景 刚加入公司的新项目,主要在做开发工作.由于是新手,本周的工作是配合另外一个同事写前台页面.前台框架是Bootstrap,本文主要介绍一下项目需求的一个功能——表格行内编辑事件. 使用X-ed ...

  3. 20道HTML基础面试题(附答案)

    以下是我整理的一些HTML的基础面试体,并自己整理了答案. 1 DOCTYPE有什么作用?标准模式与混杂模式如何区分?它们有何意义? 告诉浏览器使用哪个版本的HTML规范来渲染文档.DOCTYPE不存 ...

  4. 如何限制修改IP地址;如何禁止显示的本地连接属性

    现在很多单位都配置了局域网,为了便于进行网络管理,同时为了提高的登录网络的速度,网管人员一般都为局域网中的每台电脑都指定了IP地址.但是在windows环境下其他用户很容易修改IP地址配置,这样就很容 ...

  5. @RequestHeader和@CookieValue的使用

    /** * 了解: * * @CookieValue: 映射一个 Cookie 值. 属性同 @RequestParam */ @RequestMapping("/testCookieVal ...

  6. C++ Map实践

    实践如下: #include <iostream> #include <map> #include <string> #include <typeinfo&g ...

  7. Electron对JQuery的支持问题

    最近在了解Electron框架写应用,偶然发现在html中使用<script src="./jquery.js"></script>这种方式引入JQuery ...

  8. react native props上存在的属性,显示不存在

    问题:类型“Readonly<{}> & Readonly<{ children?: ReactNode; }>”上不存在属性“navigation”.ts(2339) ...

  9. python问题笔记

    1.for...in...:和while...:循环末端都可以有一个else:语句,但他仅在循环不是由break语句退出时才会被运行 2.input raw input区别 一. 可以看到:这两个函数 ...

  10. Android6.0运行时权限的处理Demo

    MainActivity.java package com.loaderman.permissionsdemo; import android.Manifest; import android.con ...