#447 Div2 E

题意

给出一个由有向边构成的图,每条边上有蘑菇,假设有 \(n\) 个蘑菇,那么第一次走过这条边可以获得 \(n\) 个蘑菇,第二次 \(n-1\),第三次 \(n-1-2\),第四次 \(n-1-2-3\),后面类推,直至为 \(0\)。问从选定点出发最多可以获得几个蘑菇。

分析

Tarjan 算法缩点,重新给点标号(缩点),且保证了拓扑排序中靠后的点先标号,对于缩完点后的有向无环图,DP去求最长路。(对于拓扑排序后的序列,根据拓扑排序的性质,可以从后往前DP)

拓扑排序保证了:对于有向边 \(a-b\),\(a\) 一定在 \(b\) 前面。

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e6 + 10;
struct Edge {
int v, w, nxt;
}e[N];
int head[N], cnt;
void addEdge(int u, int v, int w) {
e[cnt].v = v;
e[cnt].w = w;
e[cnt].nxt = head[u];
head[u] = cnt++;
}
int n, m, c, nn, vis[N], dfn[N], low[N];
int f[N]; // 被缩成的新点的序号
ll sup[N]; // 这个新点能提供的贡献
stack<int> sta;
vector<int> G[N];
void tarjan(int u) { // 找强连通分量
sta.push(u);
dfn[u] = low[u] = ++c;
vis[u] = 1;
for(int i = head[u]; ~i; i = e[i].nxt) {
int v = e[i].v;
if(!dfn[v]) {
tarjan(v);
low[u] = min(low[u], low[v]);
} else if(vis[v] && low[u] > dfn[v]) {
low[u] = dfn[v];
}
}
if(low[u] == dfn[u]) {
++nn;
while(1) {
int id = sta.top();
G[nn].push_back(id);
f[id] = nn;
sta.pop();
vis[id] = 0;
if(id == u) break;
}
}
}
ll calc(int w) {
int d = sqrt(2 * w);
while(d * d + d > 2 * w) d--;
return 1LL * w * (d + 1) - (1LL * d * (d + 1) * (2 * d + 1) / 6 + d * (d + 1) / 2) / 2;
}
ll dp[N];
int main() {
memset(head, -1, sizeof head);
scanf("%d%d", &n, &m);
for(int i = 0; i < m; i++) {
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
addEdge(u, v, w);
}
nn = n;
for(int i = 1; i <= n; i++) {
if(!dfn[i]) tarjan(i);
}
// 计算每个强连通分量缩成的点能提供的贡献
for(int i = 1; i <= n; i++) {
for(int j = head[i]; ~j; j = e[j].nxt) {
int v = e[j].v;
if(f[i] == f[v]) sup[f[i]] += calc(e[j].w);
}
}
int s;
scanf("%d", &s);
s = f[s];
for(int i = n + 1; i <= nn; i++) {
for(int j = 0; j < G[i].size(); j++) {
int q = G[i][j];
for(int p = head[q]; ~p; p = e[p].nxt) {
int v = e[p].v;
if(f[q] != f[v])
dp[i] = max(dp[i], dp[f[v]] + sup[f[v]] + e[p].w);
}
}
}
cout << sup[s] + dp[s] << endl;
return 0;
}

Codeforces #447 Div2 E的更多相关文章

  1. Codeforces #447 Div2 D

    #447 Div2 D 题意 给一棵完全二叉树,每条边有权值为两点间的距离,每次询问 \(x, h\) ,从结点 \(x\) 出发到某一结点的最短路的距离 \(d\) 如果小于 \(h\) ,则答案加 ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  7. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  8. codeforces 447 A-E div2 补题

    A DZY Loves Hash 水题 #include<iostream> #include<cstdio> #include<cstdlib> #include ...

  9. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

随机推荐

  1. [洛谷P2174]小Z的神奇数列

    题目大意:有$n(n\leqslant10^6)$个数,$5$种操作: $D\;x:$从数列中删除$x$,相同的数只删除一个 $B:$最大值 $S:$最小值 $M:$输出$max^{min}\pmod ...

  2. Windows查看进程CMD命令和终止进程CMD命令

    将小米路由器3刷机成openwrt的方法,请参考上篇文章< 家庭宽带多运营商接入方案>这里介绍怎么在已经刷成openwrt系统的小米路由器3上安装私有云nextcloud openwrt开 ...

  3. There is an overlap in the region chain

    ERROR: (regions day_hotstatic,860010-2355010000_20140417_12_entry_00000000321,1398674475358.0dc20573 ...

  4. JS alert()、confirm()、prompt()的区别

    这三个都是属于弹框类型的 使用警告.提示和确认消息框来获得用户的输入.这些消息框是 window 对象的接口方法.由于 window 对象位于对象层次的顶层,因此实际应用中不必使用这些消息框的全名(例 ...

  5. MySQL 配置文件及逻辑架构

    配置文件: linux:/etc/my.cnf              默认配置文件:/usr/share/mysql/my-default.cnf windows:my.ini 主要日志文件: 二 ...

  6. [05]Git查看、删除、重命名远程分支和tag

    Git查看.删除.重命名远程分支和tag 2015-06-15:加入姊妹篇: 2013-11-06:加入重命名远程分支的内容: 2013-01-09:加入删除远程tag的内容. 姊妹篇:使用Git.G ...

  7. 浅析 nth-child(n) 和 nth-of-type(n)

    首先看一个例子 <div> <p>第一个段落</p> <p>第二个段落</p> </div> p:nth-child(2) { ...

  8. saltstack入门至放弃之salt安装部署

    学习了一段时间的saltstack,是时候记录下了.友提:学习环境是两台centos_7.2_x64机器 系统初始化: 两台机器执行以下脚本即可(友提:两台服务器的主机名配置在/etc/hosts中, ...

  9. bzoj 1706: [usaco2007 Nov]relays 奶牛接力跑——倍增floyd

    Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项目.至于进行接力跑的地点 自然是在牧场中现有的T(2 <= T < ...

  10. [转载]超赞!32款扁平化Photoshop PSD UI工具包(下)

    32款扁平化风格的UI工具包第二弹!上篇为大家分享了16款风格各异的UI Kits,下篇继续为大家呈上16款精美的UI工具包,全部都有Photoshop PSD文件可以下载哦,喜欢就赶紧收藏吧! 17 ...