Codeforces Round #447 (Div. 2) E. Ralph and Mushrooms

来源:互联网 发布:让手机不能安装软件 编辑:程序博客网 时间:2024/05/19 05:03

E. Ralph and Mushrooms
time limit per test2.5 seconds
memory limit per test512 megabytes
inputstandard input
outputstandard 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 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

题意:每条边有一个权值,每次经过一条边这条边的权值都会减少它被经过次数的值,第一次减少1,第二次减少2,,,第n次减少n;问从某一个点出发,可以得到的最大的值。
做法:每一个强联通分量可以任意走,先保存每一个强联通分量的最大值,然后跑反向拓扑序,然后dp保存每一强联通分量的最大值。最后输出询问点所在的强联通分量的dp值就可以了。

#include<bits/stdc++.h>#define ll long longusing namespace std;const int N = 1e6+100;bool vis[N];struct edge{    int to,next,w;};struct edge2{    int to,w;};edge e[N];int head[N],tot;int n,m,cnt,components,componentdegree[N],DFN[N],que[N],top,low[N];ll cns[N],ret[N];ll dp[N];bool inque[N];ll ans = 0;vector<edge2> G[N];int son[N];void add_edge(int u,int v,int w){    e[tot].to = v;    e[tot].next = head[u];    e[tot].w = w;    head[u] = tot++;}ll judge(int x){    int l = 0, r= 1e5;    while(r - l > 1){        int mid = (l+r)>>1;        if(1ll*mid*(mid+1)/2 > x) r = mid;        else l = mid;    }    return 1ll*(l+1)*x - ret[l];}void tarjan(int now){    DFN[now] = low[now] = ++cnt;    que[++top] = now;    inque[now] = true;    for(int i = head[now];i != -1;i = e[i].next)    {        int j = e[i].to;            if(DFN[j] == -1)            {                tarjan(j);                low[now] = min(low[now],low[j]);            }            else if(inque[j]&&low[now] > low[j]) low[now] = low[j];    }    if(low[now] == DFN[now])    {        components ++;        componentdegree[now] = components;        while(que[top]!=now)        {            int x = que[top];            inque[x] = false;            componentdegree[x] = components;            top --;        }        inque[now] = false;        top --;    }    return ;}void solve(){    components = 0;top = 0;cnt = 0;    memset(inque,false,sizeof(inque));    memset(DFN,-1,sizeof(DFN));    for(int i= 1;i <= n;i ++)        if(DFN[i] == -1)        {            tarjan(i);        }}void init(){    ll now = 0;    for(int i = 1;i < N;i ++){        now += i;        ret[i] = ret[i-1]+now;    }}int main(){    init();    cin >> n >> m;    memset(head,-1,sizeof(head));    tot = 0;    for(int i = 1;i <= m;i ++){        int a,b,c;        scanf("%d %d %d",&a,&b,&c);        add_edge(a,b,c);    }    /*for(int i = 1;i <= n;i ++){        for(int j = head[i];j != -1;j = e[j].next){            cout << i << ' '<< e[j].to << ' ' << e[j].w << endl;        }    }*/    solve();    for(int i = 1;i <= n;i ++){        for(int j = head[i];j != -1;j = e[j].next){            int u = e[j].to;            int ci = componentdegree[i],cu = componentdegree[u];            if(ci == cu) {                cns[ci] += judge(e[j].w);                continue;            }            son[ci] ++;            G[cu].push_back((edge2){ci,e[j].w});        }    }    queue<int> que;    for(int i = 1;i <= components;i ++){        dp[i] = cns[i];        if(son[i] == 0) que.push(i);    }    while(!que.empty()){        int now = que.front();        que.pop();        for(int i = 0;i < G[now].size();i ++){            edge2 ee = G[now][i];            int cn = now,cu = ee.to;            //cns[cu] = max(cns[cu],cns[cn]+ee.w);            dp[cu] = max(dp[cu],cns[cu] + dp[cn]+ee.w);            son[ee.to] --;            if(son[ee.to] == 0) que.push(ee.to);        }    }    int qq;    cin >>qq;    cout << dp[componentdegree[qq]] << endl;    return 0;}
阅读全文
0 0
原创粉丝点击