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 ≤ 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

先处理环,用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("\n")
#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;
}

  

Codeforces Round #447 (Div. 2)E. Ralph and Mushrooms的更多相关文章

  1. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field【数论/组合数学】

    B. Ralph And His Magic Field time limit per test 1 second memory limit per test 256 megabytes input ...

  2. Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学

    题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...

  3. Codeforces Round #447 (Div. 2) 题解 【ABCDE】

    BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...

  4. Codeforces Round #447 (Div. 2)

    我感觉这场CF还是比较毒的,虽然我上分了... Problem A  QAQ 题目大意:给你一个由小写字母构成的字符串,问你里面有多少个QAQ. 思路:找字符串中的A然后找两边的Q即可,可以枚举找Q, ...

  5. 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field

    | [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...

  6. Codeforces Round #447 (Div. 2) 题解

    A.很水的题目,3个for循环就可以了 #include <iostream> #include <cstdio> #include <cstring> using ...

  7. Codeforces Round #447 (Div. 2) C 构造

    现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...

  8. Codeforces Round #447 (Div. 2) C. Marco and GCD Sequence【构造/GCD】

    C. Marco and GCD Sequence time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #447 (Div. 2) A. QAQ【三重暴力枚举】

    A. QAQ time limit per test 1 second memory limit per test 256 megabytes input standard input output ...

随机推荐

  1. elasticsearch 文档阅读笔记(三)

    文档 elasticsearch是通过document的形式存储数据的,个人理解文档就是一条数据一个对象 我们添加索引文档中不仅包含了数据还包含了元数据 比如我们为一个数据添加索引 文档中不仅有jso ...

  2. CODEVS——T 1004 四子连棋

    http://codevs.cn/problem/1004/  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 黄金 Gold 题解  查看运行结果     题目描述 Descr ...

  3. 【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动

    採用双层背景.这样效果更好 .h class BackgroundMove : public Layer { public: BackgroundMove(); ~BackgroundMove(); ...

  4. luogu2606 排列计数

    题目大意 求满足下列条件的排列$P$的数量:$\forall P_i, P_i>P_{\lfloor \frac{i}{2}\rfloor}$. 思路 从下标入手 反过来想,也就是对$\fora ...

  5. ReflectionSugar 通用反射类

    http://www.cnblogs.com/sunkaixuan/p/4635710.html

  6. Node.js:Strea

    ylbtech-Node.js:Stream 1.返回顶部 1. Node.js Stream(流) Stream 是一个抽象接口,Node 中有很多对象实现了这个接口.例如,对http 服务器发起请 ...

  7. wampserver配置多站点

    1.打开C:\wamp\bin\apache\apache2.2.22\conf\httpd.conf(因安装的路径而异),查找listen 80 下面加上listen 8080 2.然后加上 < ...

  8. 第7章 性能和可靠性模式 Failover Cluster(故障转移群集)

    上下文 您已经决定在设计或修改基础结构层时使用群集以提供高度可用的服务. 问题 您应该如何设计一个高度可用的基础结构层,来防止因单台服务器或它所运行的软件出现故障而导致的服务丢失? 影响因素 在设计高 ...

  9. 使用DbVisualizer变量

    变量可用于构建参数化SQL语句,并让DbVisualizer在执行SQL时提示您输入值.如果您重复执行相同的SQL,只是希望在同一个SQL语句中传递新数据,这很方便. 变量语法 变量格式支持设置默认值 ...

  10. css3伸缩布局中justify-content详解

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...