CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)
题意:一张有向图,每条边上都有wi个蘑菇,第i次经过这条边能够采到w-(i-1)*i/2个蘑菇,直到它为0。问最多能在这张图上采多少个蘑菇。
分析:在一个强连通分量内,边可以无限次地走直到该连通块内蘑菇被采完为止,因此每个强连通分量内的结果是确定的。
设一条边权值为w,最大走过次数为t,解一元二次方程得 t = (int)(1+sqrt(1+8w));则该边对所在连通块的贡献为w*t - (t-1)*t*(t+1)/6。
而不在任何一个强连通分量内的边,最多只能走一次。所以在缩点后的DAG上进行dp即可。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn =1e6+;
struct Edge{
int v,next;
LL val;
}edges[maxn],E[maxn];
int head[maxn],tot,H[maxn],tt;
stack<int> S;
int pre[maxn],low[maxn],sccno[maxn],dfn,scc_cnt;
LL W[maxn];
LL dp[maxn];
void init()
{
tot = dfn = scc_cnt=tt=;
memset(H,-,sizeof(H));
memset(W,,sizeof(W));
memset(dp,,sizeof(dp));
memset(pre,,sizeof(pre));
memset(sccno,,sizeof(sccno));
memset(head,-,sizeof(head));
} void AddEdge(int u,int v,LL val) {
edges[tot] = (Edge){v,head[u],val};
head[u] = tot++;
} void Tarjan(int u)
{
int v;
pre[u]=low[u]=++dfn;
S.push(u);
for(int i=head[u];~i;i=edges[i].next){
v= edges[i].v;
if(!pre[v]){
Tarjan(v);
low[u]=min(low[u],low[v]);
}
else if(!sccno[v]){
low[u]=min(low[u],pre[v]);
}
}
if(pre[u]==low[u]){
int x;
++scc_cnt;
for(;;){
x = S.top();S.pop();
sccno[x]=scc_cnt;
if(x==u)break;
}
}
} void nAddEdge(int u,int v,LL w)
{
E[tt] = (Edge){v,H[u],w};
H[u] = tt++;
} LL dfs(int u)
{
if(dp[u]) return dp[u];
for(int i=H[u];~i;i=E[i].next){
int v = E[i].v;
dp[u] = max(dp[u],dfs(v)+E[i].val);
}
dp[u]+=W[u];
return dp[u];
} int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int N,M;
while(scanf("%d%d",&N,&M)==){
init();
int st,u,v; LL w;
while(M--){
scanf("%d%d%lld",&u,&v,&w);
AddEdge(u,v,w);
}
scanf("%d",&st);
for(int i=;i<=N;++i){
if(!pre[i]){
Tarjan(i);
}
} for(int u =;u<=N;++u){
for(int i =head[u];~i;i = edges[i].next){
v = edges[i].v;
LL w = edges[i].val;
if(sccno[u]!=sccno[v]){
nAddEdge(sccno[u],sccno[v],w);
}
else{
int t = (int)(+sqrt(+*w))/;
W[sccno[u]] += (LL)t*w - (LL)(t-)*t*(t+)/;
}
}
}
for(int i=;i<=scc_cnt;++i){
if(!dp[i]){
dfs(i);
}
}
printf("%lld\n",dp[sccno[st]]);
}
return ;
}
CodeForces - 894E Ralph and Mushrooms (强连通缩点+dp)的更多相关文章
- 【Codeforces】894E.Ralph and Mushrooms Tarjan缩点+DP
题意 给定$n$个点$m$条边有向图及边权$w$,第$i$次经过一条边边权为$w-1-2.-..-i$,$w\ge 0$给定起点$s$问从起点出发最多能够得到权和,某条边可重复经过 有向图能够重复经过 ...
- UVA11324 The Largest Clique (强连通缩点+DP最长路)
<题目链接> 题目大意: 给你一张有向图 G,求一个结点数最大的结点集,使得该结点集中的任意两个结点 u 和 v 满足:要么 u 可以达 v,要么 v 可以达 u(u,v相互可达也行). ...
- ZOJ 3795 Grouping (强连通缩点+DP最长路)
<题目链接> 题目大意: n个人,m条关系,每条关系a >= b,说明a,b之间是可比较的,如果还有b >= c,则说明b,c之间,a,c之间都是可以比较的.问至少需要多少个集 ...
- UVA - 11324 The Largest Clique (强连通缩点+dp)
题目链接 题意:从有向图G中找到一个最大的点集,使得该点集中任意两个结点u,v满足u可达v或v可达u. 解法:先把同处于一个强连通分量中的结点合并(缩点),得到一张DAG图,在DAG上dp即可. 感觉 ...
- Gym - 101170B British Menu (强连通缩点+dp)
题意:求一个有向图上的最长路(每个强连通分量的点不超过5个) 首先对强连通分量缩点,暴力预处理出len[k][i][j]表示第k个强连通分量里的第i个点和第j个点之间的最长路径,设状态(k,i,f)表 ...
- Codeforces 1137C Museums Tour (强连通分量, DP)
题意和思路看这篇博客就行了:https://www.cnblogs.com/cjyyb/p/10507937.html 有个问题需要注意:对于每个scc,只需要考虑进入这个scc的时间即可,其实和从哪 ...
- Codeforces 894.E Ralph and Mushrooms
E. Ralph and Mushrooms time limit per test 2.5 seconds memory limit per test 512 megabytes input sta ...
- Codeforces B. Mouse Hunt(强连通分解缩点)
题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- 【题解】CF894E Ralph and Mushrooms (缩点)
[题解]CF894E Ralph and Mushrooms (缩点) 这是紫?给个解方程算法 考虑一条边若可以重复遍历说明一定有环,有环的话一定会把环上的蘑菇榨干,考虑一条边从全部到榨干的贡献是多少 ...
随机推荐
- jquery Fancybox使用教程
Fancybox是一款基于jquery的对图片展示播放的插件,当然,它html文本.flash动画.iframe以及ajax也予以支持.还可以通过css自定义外观,阴影效果超级赞! 演示效果:http ...
- weblogic配置oracle数据源
在weblogic配置oracle数据源还是挺简单的,网上也有很多关于这方面的文章,写给自己也写给能够得到帮助的人吧.weblogic新建域那些的就不说了哈.点击startWebLogic文件,会弹出 ...
- Engineer in the White Spaces
 Engineer in the White Spaces Michael Nygard A SySTEM ConSiSTS oF inTERdEpEndEnT pRogRAMS. We call ...
- SR领域文献资源汇总(链接地址)
DRCN http://www.drcn.org/ The International Workshop on Design of Reliable Communication Networks ...
- VSCode调试配置
http://code.visualstudio.com/docs/editor/debugging#_launch-configurations VSCode内置Node.js运行时, 能调试jav ...
- ModelShowDialog缓存上次浏览的URL
1. 一种解决方法设置每次清楚浏览的页面. In IE7, go to Tools | Internet Options. Click the Browsing History "Se ...
- python之进制转换
Python中二进制是以0b开头的: 例如: 0b11 则表示十进制的3 8进制是以0开头的: 例如: 011则表示十进制的9 16进制是以0x开头的: 例如: 0x11则表示十进制 ...
- 【BZOJ4513】[Sdoi2016]储能表 数位DP
[BZOJ4513][Sdoi2016]储能表 Description 有一个 n 行 m 列的表格,行从 0 到 n−1 编号,列从 0 到 m−1 编号.每个格子都储存着能量.最初,第 i 行第 ...
- 【Git和GitHub】学习笔记
1. 书籍推荐: 先看一本比较简单并且好的入门书籍 Git - Book https://git-scm.com/book/zh/v2 2. 书籍理解: Git 有三种状态,你的文件可能处于其中之一: ...
- C++ 调用webservice 出现 函数返回值为 3 (SOAP_TAG_MISMATCH) 的解决方案
最近在用C++ gsoap做webservice服务时,函数返回值为SOAP_TAG_MISMATCH (==3)错误码,原因是我传入wsdl地址时连同后面的?wsdl都传入了,如下: http:// ...