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?
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.
Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
2 2
1 2 4
2 1 4
1
16
3 3
1 2 4
2 3 3
1 3 8
1
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的更多相关文章
- 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 ...
- Codeforces Round #447 (Div. 2) B. Ralph And His Magic Field 数学
题目链接 题意:给你三个数n,m,k;让你构造出一个nm的矩阵,矩阵元素只有两个值(1,-1),且满足每行每列的乘积为k,问你多少个矩阵. 解法:首先,如果n,m奇偶不同,且k=-1时,必然无解: 设 ...
- Codeforces Round #447 (Div. 2) 题解 【ABCDE】
BC都被hack的人生,痛苦. 下面是题解的表演时间: A. QAQ "QAQ" is a word to denote an expression of crying. Imag ...
- Codeforces Round #447 (Div. 2)
我感觉这场CF还是比较毒的,虽然我上分了... Problem A QAQ 题目大意:给你一个由小写字母构成的字符串,问你里面有多少个QAQ. 思路:找字符串中的A然后找两边的Q即可,可以枚举找Q, ...
- 【Codeforces Round #447 (Div. 2) B】Ralph And His Magic Field
| [链接] 我是链接,点我呀:) [题意] 给你一个n*m矩阵,让你在里面填数字. 使得每一行的数字的乘积都为k; 且每一列的数字的乘积都为k; k只能为1或-1 [题解] 显然每个位置只能填1或- ...
- Codeforces Round #447 (Div. 2) 题解
A.很水的题目,3个for循环就可以了 #include <iostream> #include <cstdio> #include <cstring> using ...
- Codeforces Round #447 (Div. 2) C 构造
现在有一个长度为n的数列 n不超过4000 求出它的gcd生成set 生成方式是对<i,j> insert进去(a[i] ^ a[i+1] ... ^a[j]) i<=j 然而现在给 ...
- 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 ...
- 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 ...
随机推荐
- HDU 4359 Easy Tree DP?
Easy Tree DP? Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- [bzoj2938][Poi2000]病毒_AC自动机
病毒 bzoj-2938 Poi-2000 题目大意:给你n个01串,问是否存在一个无限长的01串使得这个01的任意子串都不等于给出的01串. 注释:All_length<=30,000 想法: ...
- pl/sql developer 自带汉化选项
pl/sql developer 自带汉化选项 版本:11.0.2 工具 -> 选项 -> 用户界面 ->外观, 第一项就是选择语言: 选择Chinese.lang,如果有的话:
- hdu4861 Couple doubi---2014 Multi-University Training Contest 1
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4861 Couple doubi Time Limit: 2000/1000 MS (Java/Othe ...
- Effective JavaScript Item 31 优先使用Object.getPrototypeOf,而不是__proto__
本系列作为Effective JavaScript的读书笔记. 在ES5中引入了Object.getPrototypeOf作为获取对象原型对象的标准API.可是在非常多运行环境中.也提供了一个特殊的_ ...
- NPOI文件导入操作
using EntMSM.SmsDbContext; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserM ...
- EularProject 36:2进制和10进制回文数
华电北风吹 天津大学认知计算与应用重点实验室 完毕日期:2015/7/29 Double-base palindromes Problem 36 The decimal number, 585 = 1 ...
- SDUT 1500-Message Flood(set)
Message Flood Time Limit: 1500ms Memory limit: 65536K 有疑问?点这里^_^ 题目描写叙述 Well, how do you feel abo ...
- 国外物联网平台初探(三) ——IBM Watson IoT
平台定位 提供全面管理的云托管服务,旨在简化并从 IoT 设备中获得价值. Watson IoT Platform 提供对 IoT 设备和数据的强大应用程序访问,可快速编写分析应用程序.可视化仪表板和 ...
- Powershell 渗透测试工具-Nishang
Powershell 渗透测试工具-Nishang 分享到: 作者:V1ct0r 稿费:500RMB(不服你也来投稿啊!) 投稿方式:发送邮件至linwei#360.cn,或登陆网页版在线投稿 传送门 ...