• 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;
    }
    

      

  • 相关阅读:
    C#设计模式(23)——备忘录模式(Memento Pattern)
    C#设计模式(22)——访问者模式(Vistor Pattern)
    C#设计模式(21)——责任链模式
    C#设计模式(20)——策略者模式(Stragety Pattern)
    C#设计模式(19)——状态者模式(State Pattern)
    C#设计模式(18)——中介者模式(Mediator Pattern)
    C#设计模式(17)——观察者模式(Observer Pattern)
    C#设计模式(16)——迭代器模式(Iterator Pattern)
    C#设计模式(15)——命令模式(Command Pattern)
    [转发]UML类图符号 各种关系说明以及举例
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8853722.html
Copyright © 2020-2023  润新知