# codeforces 894e Ralph and Mushrooms

http://www.elijahqi.win/archives/1627
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 3 when 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 x, y 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
Note
In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.

In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.

#include<cstdio>
#include<stack>
#include<cmath>
#include<queue>
#include<algorithm>
#define ll long long
#define inf 1LL<<60
#define N 1100000
using namespace std;
inline char gc(){
static char now[1<<16],*S,*T;
return *S++;
}
int x=0;char ch=gc();
while (ch<'0'||ch>'9') ch=gc();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
stack<int>q;
struct node{
int x,y,next;ll z;
}data[N<<1],data1[N<<1];
ll ans=-1,f[N],w[N];
int b[N],stackf[N],dfn[N],low[N],h1[N],h[N],num,s,n,m,in[N];
void tarjan(int x){
dfn[x]=low[x]=++num;stackf[x]=1;q.push(x);
for (int i=h[x];i;i=data[i].next){
int y=data[i].y;
if (!dfn[y]) tarjan(y),low[x]=min(low[x],low[y]);else if (stackf[y]) low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x]){
int y;++s;
do{
y=q.top();q.pop();
b[y]=s;stackf[y]=0;
}while (y!=x);
}
}
inline ll calc(ll x){
ll tt=sqrt(2*x+0.25)-0.5;
return x+tt*x-(tt+1)*(tt+2)*tt/6;
}
inline void insert1(int x,int y,ll z){
data1[++num].y=y;data1[num].z=z;data1[num].next=h1[x];h1[x]=num;data1[num].x=x;
}
int main(){
freopen("cf.in","r",stdin);
for (int i=1;i<=m;++i){
data[++num].y=y;data[num].z=z;data[num].next=h[x];h[x]=num;data[num].x=x;
}
for (int i=1;i<=n;++i) if (!dfn[i]) tarjan(i);
for (int i=1;i<=m;++i){
int x=data[i].x,y=data[i].y;
if (b[x]==b[y]) w[b[x]]+=calc(data[i].z);
}num=0;
for (int i=1;i<=n;++i){
for (int j=h[i];j;j=data[j].next){
int y=data[j].y;if (b[y]==b[i]) continue;
insert1(b[i],b[y],data[j].z+w[b[y]]);
}
}queue<int>q;
for (int i=1;i<=s;++i) f[i]=-inf;f[b[st]]=w[b[st]];
for (int i=1;i<=num;++i) in[data1[i].y]++;
for (int i=1;i<=s;++i)  if (!in[i]) q.push(i);
while(!q.empty()){
int x=q.front();q.pop();
for (int i=h1[x];i;i=data1[i].next){
int y=data1[i].y;ll z=data1[i].z;if (--in[y]==0) q.push(y);f[y]=max(f[y],f[x]+z);
}
}
for (int i=1;i<=s;++i) ans=max(ans,f[i]);
printf("%I64d",ans);
return 0;
}

• 评论

• 上一篇
• 下一篇