E. Ralph and Mushrooms
time limit per test

2.5 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Ralph is going to collect mushrooms in the Mushroom Forest.

There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially x mushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.

For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3when Ralph passes by from first to fourth time. From the fifth time and later Ralph can't collect any mushrooms from the path (but still can pass it).

Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?

Input

The first line contains two integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.

Each of the following m lines contains three integers xy and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree ywith w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.

The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.

Output

Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.

Examples
input
2 2
1 2 4
2 1 4
1
output
16
input
3 3
1 2 4
2 3 3
1 3 8
1
output
8
Note

In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.

In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.

大致题意:n个点m条有向边,每条边有一个权值,每条边可以经过多次,但是权值都会减少i,i是已经经过当前这条边的次数,不能减到0以下,现在给定起点,问能得到的最大的权值是多少?

分析:这道题算法比较好想,数学式子不大好推.

一个强连通分量里的所有边都是可以将权值取完的,将原图缩点后,图就变成了DAG,每条边只能经过1次,那么做一次dp,在加上经过的点内部可以得到的权值和就是答案.

关键就是给定一个n,如何计算这个n能够有多少贡献.最后的答案为n + (n - 1) + (n - 1 - 2) + (n - 1 - 2 - 3) +......因为是求和,从整体上来看,先把每一项变成n,项数为t,那么贡献为nt,剩下的t-1个数都要-1,t-2个数都要-2,根据这个来得到问题的解.首先考虑如何求出t来,因为式子减到0以下就不减了,后面的是一个等差数列,可以利用等差数列求和公式,设t' = t - 1,那么n = (t' + 1)*t' / 2,t'可以直接解一元二次方程得到,也可以二分得到,这里选用解方程的方法.之所以设t' = t-1是因为第一项-0,会多出一项.求出t之后,后面要减去的贡献可以表示为Σi*(t-i+1) (1 ≤ i ≤ t),利用乘法分配律展开可以得到t*(t+1)*(t+2)/6.那么贡献就是nt - t*(t+1)*(t+2)/6.

#include <cstdio>
#include <cmath>
#include <stack>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; const int maxn = ; ll n, m, head[maxn], to[maxn], nextt[maxn], w[maxn], scc[maxn], pre[maxn], low[maxn], dfs_clock;
ll tot = , f[maxn], cnt, head2[maxn], tot2 = , nextt2[maxn], to2[maxn], w2[maxn], tott;
ll bg, ans, dp[maxn];
bool vis[maxn]; struct node
{
ll x, y, z;
}e[maxn]; void add(ll x, ll y, ll z)
{
to[tot] = y;
w[tot] = z;
nextt[tot] = head[x];
head[x] = tot++;
} void Add(ll x, ll y, ll z)
{
w2[tot2] = z;
to2[tot2] = y;
nextt2[tot2] = head2[x];
head2[x] = tot2++;
} stack <ll> s; void tarjan(int u)
{
pre[u] = low[u] = ++dfs_clock;
s.push(u);
for (int i = head[u]; i; i = nextt[i])
{
int v = to[i];
if (!pre[v])
{
tarjan(v);
low[u] = min(low[u], low[v]);
}
else
if (!scc[v])
low[u] = min(low[u], pre[v]);
}
if (pre[u] == low[u])
{
cnt++;
while ()
{
int t = s.top();
s.pop();
scc[t] = cnt;
if (t == u)
break;
}
}
} ll solve(ll x)
{
ll t = (ll)floor((-1.0 + sqrt(1.0 + * x)) / );
ll s = (t + ) * x - t * (t + ) * (t + ) / ;
return s;
} ll dfss(int u)
{
if (dp[u] != -)
return dp[u];
ll res = f[u];
for (ll i = head2[u]; i; i = nextt2[i])
{
ll v = to2[i];
res = max(res, f[u] + w2[i] + dfss(v));
}
dp[u] = res;
return res;
} int main()
{
scanf("%I64d%I64d", &n, &m);
for (int i = ; i <= m; i++)
{
ll a, b, c;
scanf("%I64d%I64d%I64d", &a, &b, &c);
e[++tott].x = a;
e[tott].y = b;
e[tott].z = c;
add(a, b, c);
}
scanf("%I64d", &bg);
for (int i = ; i <= n; i++)
if (!pre[i])
tarjan(i);
for (ll i = ; i <= tott; i++)
{
ll x = e[i].x, y = e[i].y, z = e[i].z;
if (scc[x] == scc[y])
f[scc[x]] += solve(z);
}
for (ll i = ; i <= n; i++)
for (ll j = head[i]; j; j = nextt[j])
{
ll v = to[j];
if (scc[i] != scc[v])
Add(scc[i], scc[v], w[j]);
}
for (ll i = ; i <= cnt; i++)
dp[i] = -;
ans = dfss(scc[bg]);
printf("%I64d\n", ans); return ;
}

Codeforces 894.E Ralph and Mushrooms的更多相关文章

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

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

  2. Codeforces 894.D Ralph And His Tour in Binary Country

    D. Ralph And His Tour in Binary Country time limit per test 2.5 seconds memory limit per test 512 me ...

  3. Codeforces 894.B Ralph And His Magic Field

    B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...

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

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

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

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

  6. 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  ...

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

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

  8. Codeforces 894.C Marco and GCD Sequence

    C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. CodeForces - 1016C Vasya And The Mushrooms

    题面在这里! 好久没有体会这种A题的快感了23333 一开始看错了,以为权值是从1开始的,不过这样不要紧,最后把算的答案减去两行数的和就是正确的答案了. 然后发现位于一个角上的时候,我们其实只有两种选 ...

随机推荐

  1. 牛客网暑期ACM多校训练营(第四场):A Ternary String(欧拉降幂)

    链接:牛客网暑期ACM多校训练营(第四场):A Ternary String 题意:给出一段数列 s,只包含 0.1.2 三种数.每秒在每个 2 后面会插入一个 1 ,每个 1 后面会插入一个 0,之 ...

  2. 【snmp】Linux开启snmp及查询

    1.Linux snmp 1.安装snmp yum install -y net-snmp* 2.备份snmp配置 cp /etc/snmp/snmpd.conf /etc/snmp/snmpd.co ...

  3. tomcat 最大并发连接数设置

    转自: http://blog.csdn.net/qysh123/article/details/11678903 这是个很简单的问题,但是搜了一圈,发现大家都写错了.所以这里总结一下: 几乎所有的中 ...

  4. Python坑系列:可变对象与不可变对象

    在之前的文章 http://www.cnblogs.com/bitpeng/p/4748148.html 中,大家看到了ret.append(path) 和ret.append(path[:])的巨大 ...

  5. 作业 20181023-11 Alpha发布

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2283 队名:可以低头,但没必要 组长:付佳 组员:张俊余 李文涛 孙赛佳 ...

  6. MathExam第二次作业(升级版)

    MathExamLv2——林华伟 211506319 陈珍 211406263   一.预估与实际 PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实 ...

  7. 20172321 2018-2019《Java软件结构与数据结构》第三周学习总结

    教材学习内容总结 第五章 5.1概述 队列是一种线性集合,其元素从一端加入,从另一端删除:队列的处理方式是先进先出(First in First out). 与栈的比较(LIFO) 栈是一端操作,先进 ...

  8. .net组件和com组件&托管代码和非托管代码

    com组件和.net组件: COM组件是非托管对象,可以不需要.NET框架而直接运行,.NET框架组件是托管对象,必须有.NET框架的支撑才能运行. COM组件有独立的类型库文件,而.NET组件是通过 ...

  9. SpringMVC相关的面试题

    1.什么是springMVC springmvc是spirng框架的一个模块,是一个基于MVC框架的web框架 2.springmvc的流程 a.客户端发送请求 b.前端控制器DispatcherSe ...

  10. HDU 5159 Card

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5159 题解: 考虑没一个数的贡献,一个数一次都不出现的次数是(x-1)^b,而总的排列次数是x^b, ...