zoukankan      html  css  js  c++  java
  • 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 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 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

    先处理环,用Tarjan缩点然后用一个数组记录这个环的贡献值,然后对缩点后的有向无环图做一个最长路。
    环的贡献是每条边的贡献之和,可以预处理每次二分得到。
    #include <iostream>
    #include <cstdio>
    #include <stdio.h>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <queue>
    #include <stack>
    #include <set>
    #include <map>
    #define INF 0x3f3f3f3f
    #define ll long long
    #define lowbit(x) (x&(-x))
    #define eps 0.00000001
    #define pn printf("
    ")
    #define ms(x,y) memset(x,y,sizeof(x))
    using namespace std;
    
    const int maxn = 1e6+7;
    
    struct edge{
        int to, next, w;
    }e[maxn];
    int tot, head[maxn];
    int dfn[maxn], low[maxn], Stack[maxn];
    bool inStack[maxn];
    int top, Index, scc;
    int Belong[maxn];
    
    struct node{
        int v;
        ll w;
        node(int _v=0,ll _w=0):v(_v),w(_w){}
    };
    vector <node> E[maxn<<1];
    ll val[maxn << 1];
    
    ll sub[maxn], pre[maxn];
    int arr_cnt;
    
    ll binary_search(ll x)
    {
        ll l = 0, r = arr_cnt, mid;
        while(l < r)
        {
            mid = (l + r) >> 1;
            if(sub[mid] > x) r = mid;
            else l = mid + 1;
        }
        return l - 1;
    }
    
    void init()
    {
        tot = 0;
        memset(head,-1,sizeof head);
        for(arr_cnt=1; sub[arr_cnt-1] <= 1e8; arr_cnt++)
            pre[arr_cnt] = pre[arr_cnt-1] + (sub[arr_cnt] = sub[arr_cnt-1] + arr_cnt);
    }
    
    void addedge(int u,int v,int w)
    {
        e[tot].to = v;
        e[tot].w = w;
        e[tot].next = head[u];
        head[u] = tot++;
    }
    
    void Tarjan(int u)
    {
        int v;
        dfn[u] = low[u] = ++Index;
        Stack[top++] = u;
        inStack[u] = 1;
        
        for(int i=head[u];i!=-1;i=e[i].next)
        {
            v = e[i].to;
            if(!dfn[v])
            {
                Tarjan(v);
                if(low[v] < low[u]) low[u] = low[v];
            }else if(inStack[v] && dfn[v] < low[u])
                low[u] = dfn[v];
        }
        if(low[u] == dfn[u])
        {
            scc++;
            do
            {
                v = Stack[--top];
                inStack[v] = 0;
                Belong[v] = scc;
            } while(u != v);
        }
    }
    
    void solve(int N)
    {
        Index = scc = top = 0;
        for(int i=1;i<=N;i++)
            if(!dfn[i])
                Tarjan(i);
    
        //Belong[i] -> 新图中的标号
        for(int i=1;i<=N;i++)
            for(int j=head[i];j!=-1;j=e[j].next)
            {
                int u = Belong[i];
                int v = Belong[e[j].to];
                ll w = e[j].w;
                if(u == v)
                {
                    ll pos = binary_search(e[j].w);
                    if(pos >= 0)
                    {
                        w = (pos + 1) * w - pre[pos];
                        val[u] += w;
                    }
                }
                else
                {
                    E[u].push_back(node(v,w));
                }
            }
    }
    
    ll ans[maxn << 1];
    
    ll dfs(int u)
    {
        if(ans[u]) return ans[u];
        ll ret = 0;
        for(int i=0;i<E[u].size();i++)
            ret = max(ret, E[u][i].w + dfs(E[u][i].v));
        return ans[u] = ret + val[u];
    }
    
    int main()
    {
        init();
        int n,m;
        scanf("%d%d",&n,&m);
        int u_, v_, w_, s_;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u_,&v_,&w_);
            addedge(u_,v_,w_);
        }
        scanf("%d",&s_);
        solve(n);
       
        cout << dfs(Belong[s_]) << endl;
    }
    

      

  • 相关阅读:
    【NYOJ】[168]房间安排
    【NYOJ】[168]房间安排
    【POJ】[3253]Fence Repair
    【POJ】[3253]Fence Repair
    【杭电】[2111]Saving HDU
    【杭电】[2111]Saving HDU
    【杭电】[3635]Dragon Balls
    【杭电】[3635]Dragon Balls
    [leetcode]159. Longest Substring with At Most Two Distinct Characters至多包含两种字符的最长子串
    [leetcode]3. Longest Substring Without Repeating Characters无重复字母的最长子串
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8853722.html
Copyright © 2011-2022 走看看