题目链接: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)的更多相关文章

  1. 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 ...

  2. [JZOJ 5905] [NOIP2018模拟10.15] 黑暗之魂(darksoul) 解题报告 (拓扑排序+单调队列+无向图基环树)

    题目链接: http://172.16.0.132/senior/#main/show/5905 题目: oi_juruo热爱一款名叫黑暗之魂的游戏.在这个游戏中玩家要操纵一名有 点生命值的无火的余灰 ...

  3. 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 ...

  4. CodeForces 721C Journey(拓扑排序+DP)

    <题目链接> 题目大意:一个DAG图有n个点,m条边,走过每条边都会花费一定的时间,问你在不超过T时间的条件下,从1到n点最多能够经过几个节点. 解题分析:对这个有向图,我们进行拓扑排序, ...

  5. Codeforces Round #541 (Div. 2) D 并查集 + 拓扑排序

    https://codeforces.com/contest/1131/problem/D 题意 给你一个n*m二维偏序表,代表x[i]和y[j]的大小关系,根据表构造大小分别为n,m的x[],y[] ...

  6. codeforces C1. The Great Julya Calendar 解题报告

    题目链接:http://codeforces.com/problemset/problem/331/C1 这是第一次参加codeforces比赛(ABBYY Cup 3.0 - Finals (onl ...

  7. codeforces B. Eugeny and Play List 解题报告

    题目链接:http://codeforces.com/problemset/problem/302/B 题目意思:给出两个整数n和m,接下来n行给出n首歌分别的奏唱时间和听的次数,紧跟着给出m个时刻, ...

  8. codeforces 433C. Ryouko's Memory Note 解题报告

    题目链接:http://codeforces.com/problemset/problem/433/C 题目意思:一本书有 n 页,每页的编号依次从 1 到 n 编排.如果从页 x 翻到页 y,那么| ...

  9. codeforces 556B. Case of Fake Numbers 解题报告

    题目链接:http://codeforces.com/problemset/problem/556/B 题目意思:给出 n 个齿轮,每个齿轮有 n 个 teeth,逆时针排列,编号为0 ~ n-1.每 ...

随机推荐

  1. 0x32 约数

    bzoj1053: [HAOI2007]反素数ant bzoj1257: [CQOI2007]余数之和sum Hankson的趣味题 暴力枚举..约数 #include<cstdio> # ...

  2. Swift-UITextField用法

    文本框的创建,如下几种方式: UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRect: ...

  3. dnscat使用——整体感觉这个工具不完善,失败率很高,传文件时候没有完整性校验,我自己测试时通过域名转发失败,可能是其特征过于明显导致

    git clone https://github.com/iagox86/nbtool make 然后就可以按照下面的官方说明进行操作了. 我的感受:整体感觉这个工具不完善,失败率很高,传文件时候没有 ...

  4. python spark kmeans demo

    官方的demo from numpy import array from math import sqrt from pyspark import SparkContext from pyspark. ...

  5. TP5异常处理

    TP5异常处理 标签(空格分隔): php, thinkphp5 自定义异常处理 namespace app\common\exception; use think\Exception; class ...

  6. .NET CORE MVC网站体验

    安装SDK https://www.microsoft.com/net/download/core 运行命令行工具 mkdir coremvc cd coremvc dotnet new 文件建立成功 ...

  7. div position:fixed后,水平居中的问题

    .div{position:fixed;margin:auto;left:0; right:0; top:0; bottom:0;width:200px; height:150px;}

  8. sql导出到excel

    1.先查询数据ID,别名  导出到excel 2.增加标准名称,标识错误数据,导入sqlServer select distinct [StandardName] from [GMSOA].[dbo] ...

  9. Eclipse插件Lambok,实现自动生成Java代码

    1.下载Lombok.jar http://projectlombok.googlecode.com/files/lombok.jar 2.运行Lombok.jar: java -jar  D:\00 ...

  10. 列表查询组件代码, 简化拼接条件SQL语句的麻烦

    列表查询组件代码, 简化拼接条件SQL语句的麻烦 多条件查询