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. 洛谷 P1383 codevs 3333 高级打字机

    题目描述 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序,支持如下3种操作: 1.T x:在文章末尾打下一个小写字母x.(t ...

  2. foj 2173 floyd+矩阵快速幂

     Problem 2173 Nostop Accept: 52    Submit: 210 Time Limit: 3000 mSec    Memory Limit : 32768 KB  Pro ...

  3. 反射常用API

    反射所有功能都是通过class API来实现的 class常用API有: 1.class.GETINTERFACES():获得这个类实现的接口. 2.class.getMethods() Method ...

  4. 这篇文章关于两阶段提交和Paxos讲的很好

    http://blog.chinaunix.net/uid-16723279-id-3803058.html <两阶段提交协议与paxos投票算法> 点评:2PC绝对是CP的死党,是分布式 ...

  5. POJ 3225

    基本参考http://blog.csdn.net/metalseed/article/details/8039326 总的来说,敲完一遍理解会更加好一点,标记下传法. U:把区间[l,r]覆盖成1I: ...

  6. TCP/IP解析(一):TCP/IP的工作方式

    本文包括下面内容: 1.TCP/IP协议系统 2.OSI模型 3.数据包 4.TCP/IP的交互方式 1.TCP/IP模型的协议层 分为四层: 网络訪问层:提供与物理网络连接的接口.依据硬件的物理地址 ...

  7. MRv1到MRv2

    概述 引入YARN作为通用资源调度平台后.Hadoop得以支持多种计算框架,如MapReduce.Spark.Storm等. MRv1是Hadoop1中的MapReduce,MRv2是Hadoop2中 ...

  8. 0x59 单调队列优化DP

    倍增DP太难啦心情好再回去做 poj1821 先让工匠按s排序,f[i][j]表示枚举到第i个工匠涂了j个木板(注意第j个木板不一定要涂) 那么f[i][j]可以直接继承f[i-1][j]和f[i][ ...

  9. Swift - 将Data数据转换为[UInt8](bytes字节数组)

    有时上传或者发送图片.文字时,需要将数据转换为 bytes 字节数组.下面介绍两种将 Data 转换为 [UInt8] 的方法. 假设我们有如下 Data 数据要转换: 1 let data = &q ...

  10. The Triangle--nyoj 18

    The Triangle 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 7 3 8 8 1 0 2 7 4 4 4 5 2 6 5 (Figure 1) Figure ...