[codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)
题目链接:http://codeforces.com/problemset/problem/894/E
题目大意:
$n$个点$m$条边的有向图,每条边有一个权值,可以重复走。
第$i$次走过某条边权为$w$的边后这条边的边权变成$w-i$,但不能小于等于$0$。
给定起点,询问任意走最多能获得多少的边权
题解:
显然一个强联通分量的边可以全部走到$0$为止。
考虑强连通分量中一条边权为w的边对答案的贡献,$s=w+w-1+w-1-2+w-1-2-3\ldots$
设这个式子有$t+1$项,显然有$\frac{(t+1)t}{2}<=w$,解得$t=\sqrt{2w+0.25}-0.5$,t下取整
那么$s=(t+1)w-\frac{(t+2)(t+1)t}{6}$
得到每个强连通分量之后剩下的部分拓扑排序后DP即可
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<vector>
#include<queue>
#include<cmath>
typedef long long ll;
using namespace std; const int N=1e6+;
const int MAX=1e8+;
const ll inf=1ll<<;//这个要足够大,卡了2h...
int n,m,tot,top,tim,scc,t;
int sta[N],dfn[N],low[N],belong[N],deg[N],head[N];
bool ins[N];
ll dp[N],val[N],sum[N],tmp[N];
struct EDGE
{
int from,to,nxt;ll w;
}edge[N];
struct node{int v;ll w;};
vector <node> g[N];
inline ll read()
{
char ch=getchar();
ll s=,f=;
while (ch<''||ch>'') {if (ch=='-') f=-;ch=getchar();};
while (ch>=''&&ch<='') {s=(s<<)+(s<<)+ch-'';ch=getchar();}
return s*f;
}
void add(int u,int v,int w)
{
edge[++tot]=(EDGE){u,v,head[u],w};
head[u]=tot;
}
void tarjan(int x)
{
dfn[x]=low[x]=++tim;
ins[x]=;sta[++top]=x;
for (int i=head[x];i;i=edge[i].nxt)
{
int y=edge[i].to;
if (!dfn[y])
{
tarjan(y);
low[x]=min(low[x],low[y]);
}
else if (ins[y]) low[x]=min(low[x],dfn[y]);
}
if (dfn[x]==low[x])
{
++scc;int k;
do
{
k=sta[top--];
belong[k]=scc;
ins[k]=;
}
while(k!=x);
}
}
inline ll calc(ll x)
{
ll tt=sqrt(*x+0.25)-0.5;
return x+tt*x-(tt+)*(tt+)*tt/;
}
int main()
{
//for (t=1;tmp[t-1]<MAX;t++) {tmp[t]=tmp[t-1]+t;sum[t]=sum[t-1]+tmp[t];}
n=read();m=read();
for (int i=,u,v,w;i<=m;i++)
{
u=read();v=read();w=read();
add(u,v,w);
}
for (int i=;i<=n;i++) if (!dfn[i]) tarjan(i);
for (int i=;i<=m;i++)
{
int u=edge[i].from,v=edge[i].to;
u=belong[u],v=belong[v];
if (u==v) val[u]+=calc(edge[i].w);
}
for (int u=;u<=n;u++)
{
for (int i=head[u];i;i=edge[i].nxt)
{
int v=edge[i].to;
int bu=belong[u],bv=belong[v];
if (bu==bv) continue;
g[bu].push_back((node){bv,edge[i].w+val[bv]});
deg[bv]++;
}
}
int st=read();st=belong[st];
queue <int> q;
for (int i=;i<=scc;i++) dp[i]=-inf;dp[st]=val[st];
for (int i=;i<=scc;i++) if (!deg[i]) q.push(i);
while (!q.empty()){
int k=q.front();q.pop();
for (int i=;i<g[k].size();i++){
int y=g[k][i].v;
if (--deg[y]==) q.push(y);
dp[y]=max(dp[y],dp[k]+g[k][i].w);
}
}
ll mx=;
for (int i=;i<=scc;i++) mx=max(mx,dp[i]);
printf("%lld\n",mx);
return ;
}
[codeforces 894 E] Ralph and Mushrooms 解题报告 (SCC+拓扑排序+DP)的更多相关文章
- 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 ...
- [JZOJ 5905] [NOIP2018模拟10.15] 黑暗之魂(darksoul) 解题报告 (拓扑排序+单调队列+无向图基环树)
题目链接: http://172.16.0.132/senior/#main/show/5905 题目: oi_juruo热爱一款名叫黑暗之魂的游戏.在这个游戏中玩家要操纵一名有 点生命值的无火的余灰 ...
- Codeforces Round #292 (Div. 1) B. Drazil and Tiles 拓扑排序
B. Drazil and Tiles 题目连接: http://codeforces.com/contest/516/problem/B Description Drazil created a f ...
- CodeForces 721C Journey(拓扑排序+DP)
<题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...
- Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序
https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...
- codeforces C1. The Great Julya Calendar 解题报告
题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...
- codeforces B. Eugeny and Play List 解题报告
题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...
- codeforces 433C. Ryouko's Memory Note 解题报告
题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...
- codeforces 556B. Case of Fake Numbers 解题报告
题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...
随机推荐
- vue入门--简单路由配置
在初始化vue init webpack <工程名>时,有一步是询问是否安装vue-router,选择yes,如果没有安装的话,后面需要自己安装.然后在目录中可以看到有个文件夹叫route ...
- Android和IOS等效MD5加密
最近在Android和IOS上都需要对用户的某些输入进行简单的加密,于是采用MD5加密方式. 首先将目的字符串加密一次,获得32位字符串 然后将32位字符串拆为2段,分别加密1次 最后将加密后的2段拼 ...
- python爬虫:爬取读者某一期内容
学会了怎么使用os模块 #!/usr/bin/python# -*- encoding:utf-8 -*- import requestsimport osfrom bs4 import Beauti ...
- Win8 Windows Defender default behaviour
Problem Description ********************* Is it possible, to change the default behaviour when findi ...
- ajax返回值
前端: <html> <head> <meta name="viewport" content="width=device-width&qu ...
- firefox工具
1.XPath 查看元素的xpath https://addons.mozilla.org/zh-CN/firefox/addon/xpath-checker/ 2. Tamper Data 查看页面 ...
- ZBrush中平滑笔刷介绍
平滑笔刷在ZBrush®中的使用颇多,它可以在ZBrush®模型的多层细分下工作,并且能够控制对模型的平滑效果,而且还能将模型的细节完整保留.默认情况下,按住Shift键就会切换到平滑笔刷,根据调整不 ...
- Java基础之Colloction
0 引言 以下是介绍Java有关集合类,以及对应每个类的用途,同时进行比较集合类的不同特点来让我们深入了解. 1 Collction接口 Collection是最基本的集合接口,一个Collectio ...
- webpack安装,npm WARN optional SKIPPING OPTIONAL DEPENDENCY,npm WARN notsup SKIPPING OPTIONAL DEPENDENCY警告
npm install webpack -g//全局安装webpack 电脑上安装完后: 其中有两个警告: npm WARN optional SKIPPING OPTIONAL DEPENDENCY ...
- BZOJ 2342 [SHOI2011]双倍回文 (回文自动机)
题目大意:略 先建出$PAM$ 因为双倍回文串一定是4的倍数,所以找出$PAM$里所有$dep$能整除4的节点 看这个串是否存在一个回文后缀,长度恰好为它的一半,沿着$pre$链往上跳就行了 暴跳可能 ...