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

      

  • 相关阅读:
    BZOJ 4805: 欧拉函数求和 杜教筛
    BZOJ 2694: Lcm 莫比乌斯反演 + 积性函数 + 线性筛 + 卡常
    BZOJ 2693: jzptab 莫比乌斯反演 + 积性函数 + 筛法
    BZOJ 2154: Crash的数字表格 莫比乌斯反演
    BZOJ 3884: 上帝与集合的正确用法 扩展欧拉定理 + 快速幂
    BZOJ 3595: [Scoi2014]方伯伯的Oj Splay + 动态裂点 + 卡常
    GitHub的使用
    Selenium模块的安装
    关于__new__和__init__
    博客一键保存本地exe可视化界面文件
  • 原文地址:https://www.cnblogs.com/HazelNut/p/8853722.html
Copyright © 2011-2022 走看看