Ralph and Mushrooms CodeForces - 894E(tarjan缩点+dp)

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 3 when 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 x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y with 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.

Sponsor

题意:
一个有向图,给定起点,可以多次经过一个边。
对于一条边,第i次经过的收益为边权-i - (i - 1) - (i - 2)…
问收益最多是多少

思路:
对于强连通分量内的点边权的收益肯定是能全部获得。
假设边权为w,需要计算收益,分成 w 0 , w 1 , w 1 2... w - 0,w - 1,w - 1 - 2...
假定有T项,那么满足 w = ( T 1 ) T / 2 w = (T-1)*T/2 ,解得 T = ( 1 + s q r t ( 1 + 8 w ) ) / 2 T= (1 + sqrt(1+8*w))/2
那么结果就是 n T ( x 1 ) x / 2 n*T - ∑(x-1)*x/2 , x x 的范围为 [ 1 , T ] [1,T]
简化得到 n T ( t 1 ) t ( t + 1 ) / 6 n*T - (t-1)*t*(t+1)/6

同时我们维护每一个强连通分量的收益和,缩点完成后变成了一个DAG图,再在这个DAG图上统计答案。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <stack>

using namespace std;

typedef long long ll;
const int maxn = 1e6 + 7;

int head[maxn],nex[maxn],me[maxn],to[maxn],val[maxn],tot;
void add1(int x,int y,int z) {
    to[++tot] = y;
    me[tot] = x;
    nex[tot] = head[x];
    val[tot] = z;
    head[x] = tot;
}

int head2[maxn],nex2[maxn],to2[maxn],val2[maxn],tot2;
void add2(int x,int y,int z) {
    to2[++tot2] = y;
    nex2[tot2] = head2[x];
    val2[tot2] = z;
    head2[x] = tot2;
}

ll get(ll n) {
    ll t = (ll)((1.0 + sqrt(1.0 + 8.0 * n)) / 2.0);
    ll res = n * t - t * (t + 1) * (t - 1) / 6;
    return res;
}

stack<int>stk;
int dfn[maxn],low[maxn],scc[maxn],cnt,num;

void tarjan(int u) {
    dfn[u] = low[u] = ++num;
    stk.push(u);
    for(int i = head[u];i;i = nex[i]) {
        int v = to[i],w = val[i];
        if(!dfn[v]) {
            tarjan(v);
            low[u] = min(low[u],low[v]);
        }
        else if(!scc[v]) {
            low[u] = min(low[u],dfn[v]);
        }
    }
    if(low[u] == dfn[u]) {
        cnt++;
        while(!stk.empty()) {
            int now = stk.top();stk.pop();
            scc[now] = cnt;
            if(now == u) break;
        }
    }
}

ll f[maxn],f2[maxn];

ll dfs(int u) {
    if(f[u] != -1) return f[u];
    f[u] = f2[u];
    for(int i = head2[u];i;i = nex2[i]) {
        int v = to2[i];ll w = val2[i];
        dfs(v);
        f[u] = max(f[u],f2[u] + w + f[v]);
    }
    return f[u];
}

int main() {
    int n,m;scanf("%d%d",&n,&m);
    for(int i = 1;i <= m;i++) {
        int x,y,z;scanf("%d%d%d",&x,&y,&z);
        add1(x,y,z);
    }
    int sta;scanf("%d",&sta);
    
    for(int i = 1;i <= n;i++) {
        if(!dfn[i]) tarjan(i);
    }
    
    for(int i = 1;i <= tot;i++) {
        int x = me[i],y = to[i],z = val[i];
        if(scc[x] == scc[y]) {
            f2[scc[x]] += get(z);
        }
    }
    
    for(int i = 1;i <= n;i++) {
        for(int j = head[i];j;j = nex[j]) {
            int v = to[j],w = val[j];
            if(scc[i] != scc[v]) {
                add2(scc[i],scc[v],w);
            }
        }
    }
    
    memset(f,-1,sizeof(f));
    
    printf("%lld\n",dfs(scc[sta]));
    return 0;
}

原创文章 931 获赞 38 访问量 6万+

猜你喜欢

转载自blog.csdn.net/tomjobs/article/details/106120315
今日推荐