CF894E Ralph and Mushrooms
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的更多相关文章
- 【题解】CF894E Ralph and Mushrooms (缩点)
[题解]CF894E Ralph and Mushrooms (缩点) 这是紫?给个解方程算法 考虑一条边若可以重复遍历说明一定有环,有环的话一定会把环上的蘑菇榨干,考虑一条边从全部到榨干的贡献是多少 ...
- 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 ...
- 「CF894E」 Ralph and Mushrooms
传送门 Luogu 解题思路 首先我们要发现:在同一个强连通分量里的所有边都是可以无限走的. 那么我们就有了思路:先缩点,再跑拓扑排序. 那么问题就是 \(\text{DP}\) 状态如何初始化. 我 ...
- 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 ...
- CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)
题意:一张有向图,每条边上都有wi个蘑菇,第i次经过这条边能够采到w-(i-1)*i/2个蘑菇,直到它为0.问最多能在这张图上采多少个蘑菇. 分析:在一个强连通分量内,边可以无限次地走直到该连通块内蘑 ...
- 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP
题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...
- CF894E Ralph and Mushrooms_强连通分量_记忆化搜索_缩点
Code: #include<cstdio> #include<stack> #include<cstring> using namespace std; cons ...
- [codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/894/E 题目大意: $n$个点$m$条边的有向图,每条边有一个权值,可以重复走. 第$i$次走过某条边权 ...
- Codeforces Round #447 (Div. 2) 题解 【ABCDE】
BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...
随机推荐
- java8中规范的四大函数式接口
java8中规范的四大函数式接口: 1.Consumer<T> :消费型接口 void accept(T t); 2.Supplier<T> :供给型接口 ...
- Visual Stdio C++ 编译器、链接器常用命令
概览: cmd常用命令配合使用: del 删除指定文件 同erase cls 清屏 rd 删除空目录文件夹 dir 显示目录 cd 在当前盘符跳转指定目录(不同盘符跳转用盘符号)(分别表示根目录 上一 ...
- TTTTTTTTTTTT 百度之星D map+hash
Problem D Accepts: 2806 Submissions: 8458 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 6 ...
- luoguP3203 [HNOI2010]BOUNCE 弹飞绵羊
P3203 [HNOI2010]BOUNCE 弹飞绵羊 题目描述 某天,Lostmonkey发明了一种超级弹力装置,为了在他的绵羊朋友面前显摆,他邀请小绵羊一起玩个游戏.游戏一开始,Lostmonke ...
- [BZOJ3786] 星系探索(括号序列+Splay)
3786: 星系探索 Time Limit: 40 Sec Memory Limit: 256 MBSubmit: 2191 Solved: 644[Submit][Status][Discuss ...
- java.lang.Integer.MAX_VALUE;这是什么意思?
这个是Integer类中的一个int类型的常量MAX_VALUE它代表int所能表示的最大值 0x7FFFFFFF 相对应的是Integer类中的另一个常量MIN_VALUE它代表int所能表示的最小 ...
- @RepositoryRestResource注解的使用
1.Build with Maven <?xml version="1.0" encoding="UTF-8"?> <project xmln ...
- LocalDateTime自动转化格式
开发中,遇到这样的问题.一个LocalDateTime类型的属性返回给前端是中间带T的格式,前端说转不了. 可以引入jackson包转换一下 <properties> <jackso ...
- ClientScriptManager 和 ScriptManager RegisterClientScriptBlock
ClientScriptManager.RegisterOnSubmitStatement(Type, String, String) Method Registers an OnSubmit sta ...
- awk命令2
提取文件后四行 注释:NR==FNR表示第一个文件,执行{a++},计算出第一个文件10的行数,NR!=FNR表示第二个文件10,执行{if(FNR<=a-4){print $0}},打印出第二 ...