#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. P4467 [SCOI2007]k短路

    题目描述 有 n 个城市和 m 条单向道路,城市编号为 1 到 n .每条道路连接两个不同的城市,且任意两条道路要么起点不同要么终点不同,因此 n 和 m 满足 m \le n(n-1)m≤n(n−1 ...

  2. BZOJ5322 [Jxoi2018]排序问题 【贪心】

    题目链接 BZOJ5322 题解 意思就是使有序的排列尽量少 就是使相同的数尽量少 然后大力贪心即可 #include<algorithm> #include<iostream> ...

  3. lvm扩容

    111 mkfs -t xfs /dev/sda3 112 pvcreate /dev/sda3 113 vgs 114 vgextend cl /dev/sda3 115 lvscan 116 vg ...

  4. eclipse web(Spring+SpringMVC+Hibernate)项目迁移至intellij idea

    1.导入Eclipseweb项目 跟着导航一直下一步 出现警告不要担心,先点击确认,到后面再进行设置jdk 成功导入项目后如下图 2.对导入的项目进行配置按Ctrl+shift+alt+s(或下图中的 ...

  5. 封装getByClass(JS获取class的方法封装为一个函数)

    获取方法一(普通版) 获取单一的class: function getByClass(oParent, sClass) {//两个形参,第一个对象oParent 第二个样式名class var aEl ...

  6. C#中file类的应用

    现在就其中几个常用的进行介绍: Create:一般使用此重载方法,File.Create (String) ,String是一个路径名,表示文件的完整路径,返回值是一个FileStream实例: Co ...

  7. ZOJ3872 Beauty of Array---规律 | DP| 数学能力

    传送门ZOJ 3872 Beauty of Array Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has an array A  ...

  8. POJ 1062 昂贵的聘礼 (最短路 迪杰斯特拉 )

    题目链接 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请 ...

  9. bzoj 1011 近似估计

    开始看这道题的时候没什么思路,后来忍不住看了题解,发现自己真是水啊... 自从学OI来第一次看到用约等的题 首先我们设w[i]为第i个星球的答案,g[i]为第i个星球受到1-g[i]个星球的引力 那么 ...

  10. 培训补坑(day8:树上倍增+树链剖分)

    补坑补坑.. 其实挺不理解孙爷为什么把这两个东西放在一起讲..当时我学这一块数据结构都学了一周左右吧(超虚的) 也许孙爷以为我们是省队集训班... 好吧,虽然如此,我还是会认真写博客(保证初学者不会出 ...