题目链接: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. 读取excel模板填充数据 并合并相同文本单元格

    try             { string OutFileName = "北京市国控企业污染源废气在线比对监测数据审核表" + DateTime.Now.ToString(& ...

  2. ES内存持续上升问题定位

      https://discuss.elastic.co/t/memory-usage-of-the-machine-with-es-is-continuously-increasing/23537/ ...

  3. QEMU/KVM磁盘在线备份

    QEMU/KVM磁盘的在线完整及增量备份,是"打包"方案的一种具体实现,可实现基于时间点的备份,同时支持本地与远程2种备份方式,并可指定备份文件进行恢复. tag: qemu, k ...

  4. Fragment的理解

    1.生命周期    启动Fragment时: onAttachonCreateonCreateViewonViewCreatedonActivityCreatedonStartonResume 启动后 ...

  5. QQ自动登录里的一些控件知识

    在这个程序里面有个读取计算机指定文件的知识: private void button2_Click(object sender, EventArgs e) { openFileDialog1.Show ...

  6. UVa 11520 Fill in the Square

    题意:给出 n*n的格子,把剩下的格子填上大写字母,使得任意两个相邻的格子的字母不同,且从上到下,从左到右的字典序最小 从A到Z枚举每个格子填哪一个字母,再判断是否合法 #include<ios ...

  7. Windows下绿色版Tomcat部署Thingworx 7.4

    绿色版Tomcat部署Thingworx7.4和安装只有一个不同之处,安装版Tomcat需要在Configure Tomcat的Java标签下设置Java Options,但是绿色版并没有这个exe程 ...

  8. shell编程-1.字符截取命令-列截取awk+printf

  9. Dynamic dispatch mechanisms

    Normally, in a typed language, the dispatch mechanism will be performed based on the type of the arg ...

  10. zabbix部署监控端(server)以及页面优化

    实验环境准备 172.20.10.2 server.zabbix.com 172.20.10.3 agent.zabbix.com 172.20.10.8 windows10 Server 端 [ro ...