Codeforces Round #447 (Div. 2) E DAG+scc (未敲)

来源:互联网 发布:淘宝客服有在家做的吗 编辑:程序博客网 时间:2024/05/17 22:08

参考:http://codeforces.com/blog/entry/55884和http://blog.csdn.net/Icefox_zhx/article/details/78590429
题目:http://codeforces.com/contest/894/problem/E

E. Ralph and Mushrooms

Ralph is going to collect mushrooms in the Mushroom Forest.

There are m directed paths connecting n trees in the Mushroom Forest. On each path grow some mushrooms. When Ralph passes a path, he collects all the mushrooms on the path. The Mushroom Forest has a magical fertile ground where mushrooms grow at a fantastic speed. New mushrooms regrow as soon as Ralph finishes mushroom collection on a path. More specifically, after Ralph passes a path the i-th time, there regrow i mushrooms less than there was before this pass. That is, if there is initially x mushrooms on a path, then Ralph will collect x mushrooms for the first time, x - 1 mushrooms the second time, x - 1 - 2 mushrooms the third time, and so on. However, the number of mushrooms can never be less than 0.

For example, let there be 9 mushrooms on a path initially. The number of mushrooms that can be collected from the path is 9, 8, 6 and 3 when Ralph passes by from first to fourth time. From the fifth time and later Ralph can’t collect any mushrooms from the path (but still can pass it).

Ralph decided to start from the tree s. How many mushrooms can he collect using only described paths?
Input

The first line contains two integers n and m (1 ≤ n ≤ 106, 0 ≤ m ≤ 106), representing the number of trees and the number of directed paths in the Mushroom Forest, respectively.

Each of the following m lines contains three integers x, y and w (1 ≤ x, y ≤ n, 0 ≤ w ≤ 108), denoting a path that leads from tree x to tree y with w mushrooms initially. There can be paths that lead from a tree to itself, and multiple paths between the same pair of trees.

The last line contains a single integer s (1 ≤ s ≤ n) — the starting position of Ralph.
Output

Print an integer denoting the maximum number of the mushrooms Ralph can collect during his route.
Examples

input
2 2
1 2 4
2 1 4
1
output
16
input
3 3
1 2 4
2 3 3
1 3 8
1
output
8
Note

In the first sample Ralph can pass three times on the circle and collect 4 + 4 + 3 + 3 + 1 + 1 = 16 mushrooms. After that there will be no mushrooms for Ralph to collect.

In the second sample, Ralph can go to tree 3 and collect 8 mushrooms on the path from tree 1 to tree 3.

题意

给你n个点m条边的有向图,每条边都会有蘑菇,一开始第i个边有a[i]个蘑菇,第一次经过能获得a[i]个,第二次能获得a[i]-1个,第三次能获得a[i]-1-2个,直到a[i]非负。

现在给你起点,问你最多能获得多少个蘑菇。

一个强连通内的边显然可以把它的价值完全压榨,其他的边只能过一次,所以我们tarjan求scc,缩成DAG,然后拓扑序dp求最长路。至于怎么算一条边的所有价值,数学搞吧。首先求出n∗(n+1)<=w的最大的n,然后价值就是n∗w−∑ni=1i∗(i+1)/2+w,也就是n∗w−n∗(n+1)∗(n+2)/6+w

http://blog.csdn.net/Dylan_Frank/article/details/78584076

#include <bits/stdc++.h>using namespace std;#define ms(x,v) (memset((x),(v),sizeof(x)))#define pb push_back#define mp make_pair#define fi first#define se second#define INF 0x3f3f3f3f#define INF64 0x3f3f3f3f3f3f3f3ftypedef long long LL;typedef pair<int,int > Pair;const int maxn = 1e6 +10;const int maxv = 1e8+10;const int MOD = 1e9+7;LL tmp[maxn],sum[maxn],t=0;inline LL weight(LL x){    int p = lower_bound(tmp,tmp+t,x) - tmp-1;    return x*(p+1) - sum[p];}int n,m,s;LL ans =0;bool vis[maxn];LL node[maxn],dp[maxn];//缩点后贡献//sccstd::vector<int> sc_node[maxn];std::vector<Pair> G[maxn];int dfn[maxn],low[maxn];int dfs_clock=0;int scc[maxn],scc_cnt=0;stack<int> S;//辅助栈void dfs(int u) {    low[u] = dfn[u] = ++dfs_clock;    S.push(u);    for(auto e:G[u]){         int v = e.fi;        if(!dfn[v]){            //未访问            dfs(v);            low[u] = min(low[v],low[u]);        }else if(!scc[v])low[u] = min(low[v],low[u]);    }    //计算出low值之后看是否满足起始条件    if(low[u] == dfn[u]){        //标记        scc_cnt++;        while (true) {            int v = S.top();S.pop();            scc[v] = scc_cnt;            if(v == u)break;        }    }}LL solve(int u) {    if(dp[u]!=-1)return dp[u];    dp[u] = node[u];    LL ans = 0;    for(auto v : sc_node[u]){        for(auto e : G[v])            if(scc[e.fi] != u){                ans = max(ans,solve(scc[e.fi])+e.se);            }    }    return dp[u]+=ans;}int main(int argc, char const *argv[]) {     for(t=0; tmp[t-1]<maxv ; ++t)tmp[t] = t+ tmp[t-1],sum[t]=tmp[t]+sum[t-1];     cin>>n>>m;     for(int i=0 ; i<m ; ++i){         int x,y,w;         scanf("%d%d%d",&x,&y,&w );         G[x].pb(mp(y,w));     }     cin>>s;     for(int i=1 ; i<=n ; ++i)        if(!dfn[i])dfs(i);     for(int i=1 ; i<=n ; ++i){         sc_node[scc[i]].pb(i);         for(auto e: G[i]){             if(scc[e.fi]==scc[i])node[scc[i]]+=weight(e.se);         }     }     ms(dp,-1);     std::cout << solve(scc[s]) << '\n';    return 0;}
阅读全文
0 0