博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Codeforces Round #447 (Div. 2)E. Ralph and Mushrooms
阅读量:5061 次
发布时间:2019-06-12

本文共 4033 字,大约阅读时间需要 13 分钟。

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 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#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
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

  

转载于:https://www.cnblogs.com/HazelNut/p/8853722.html

你可能感兴趣的文章
java编写提升性能的代码
查看>>
ios封装静态库技巧两则
查看>>
Educational Codeforces Round 46 (Rated for Div. 2)
查看>>
Abstract Factory Pattern
查看>>
C# 实现Bresenham算法(vs2010)
查看>>
基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装
查看>>
list 容器 排序函数.xml
查看>>
存储开头结尾使用begin tran,rollback tran作用?
查看>>
Activity启动过程中获取组件宽高的五种方式
查看>>
java导出Excel表格简单的方法
查看>>
SQLite数据库简介
查看>>
利用堆实现堆排序&amp;优先队列
查看>>
Mono源码学习笔记:Console类(四)
查看>>
Android学习路线(十二)Activity生命周期——启动一个Activity
查看>>
《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇03:暂停游戏》
查看>>
CPU,寄存器,一缓二缓.... RAM ROM 外部存储器等简介
查看>>
windows下编译FreeSwitch
查看>>
git .gitignore 文件不起作用
查看>>
Alan Turing的纪录片观后感
查看>>
c#自定义控件中的事件处理
查看>>