Codeforces 894.E Ralph and Mushrooms

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 ≤ n0 ≤ 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 = 2000010;

ll n, m, head[maxn], to[maxn], nextt[maxn], w[maxn], scc[maxn], pre[maxn], low[maxn], dfs_clock;
ll tot = 1, f[maxn], cnt, head2[maxn], tot2 = 1, 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 (1)
        {
            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 + 8 * x)) / 2);
    ll s = (t + 1) * x - t * (t + 1) * (t + 2) / 6;
    return s;
}

ll dfss(int u)
{
    if (dp[u] != -1)
        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 = 1; 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 = 1; i <= n; i++)
        if (!pre[i])
            tarjan(i);
    for (ll i = 1; 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 = 1; 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 = 1; i <= cnt; i++)
        dp[i] = -1;
    ans = dfss(scc[bg]);
    printf("%I64d\n", ans);

    return 0;
}

 

转载于:https://www.cnblogs.com/zbtrs/p/8040119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值