题目链接: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. oracle 11gR2 如何修改 private ip

    1.1    修改 private ip1.1.1  确保crs集群是打开的可以用olsnodes –s 检查集群的状态./olsnodes -sP570a ActiveP570b Active1.1 ...

  2. DBS-Oracle:表的连接查询

    ylbtech-DBS-Oracle:表的连接查询 链接查询是指基于两个或两个以上表或试图的查询.在实际应用中,查询单个表可能无法满足应用程序的实际需求(例如显示雇员的部门名称以及雇员名),在这种情况 ...

  3. Java线程池原理与架构分析

    /** * 一.线程池:提供了一个线程队列,队列中保存着所有等待状态的线程.避免了创建与销毁额外开销,提高了响应速度 * 二.线程池的体系结构 * java.util.concurrent.Execu ...

  4. 4.QList

    #include "mainwindow.h" #include <QApplication> #include <QLabel> #include < ...

  5. init&initWithFrame&initWithCoder

    //当我们所写的程序里没用用Nib文件(XIB)时,用代码控制视图内容,需要调用initWithFrame去初始化 - (id)initWithFrame:(CGRect)frame { if (se ...

  6. MVC中添加模块区域,并设置RedirectToAction跳转

    废话少说,直接上图:

  7. sql获得某个时间段的数据

    CONVERT(Date, 时间字符串变量 ) between CONVERT(Date,'2015/2/10') and CONVERT(Date,'2015-3-10')

  8. Usaco 滑雪比赛 Bobsledding, 2009 Dec(dp)

    Description 滑雪比赛bobsled 贝西参加了一场高山急速滑雪比赛,滑道总长度为 L.出发时,她的初速度为 1,贝西可以加速 或减速,每过 1 米,她能将速度增加 1.减少 1 或保持不变 ...

  9. (转)Django学习之 第二章:Django快速上手

    安装Python  安装Django非常容易.因为Django可以运行在任何可以运行Python的环境中,所以可以以多种方式进行配置. 在本章我们将尝试覆盖几种常见的Django安装场景. Djang ...

  10. Spinner与适配器模式总结

    今天开始编辑我的第一篇博客. ------------------------------------------------------------------------------------- ...