Eating Everything Efficiently(树形DP)

题目描述
Margriet A. is in pizza heaven! She has bought a one-day access pass to Pizza World. Pizza World is a food festival, where all stands have their own special type of pizza. Margriet would really like to try many different types of pizza, but she thinks that she can only eat two pizzas in total. Therefore, she has come up with a cunning plan: at each stall she visits she decides whether she wants to buy this pizza or not. At the first stall where she decides to make a purchase, she will buy and eat exactly one pizza.
At the second one, she will buy and eat half a pizza, and at the third she will eat one quarter of a pizza, etc… Therefore, at the kth stall where she decides to buy some pizza, she will eat part of a pizza. This way she makes sure that she will never get full!
In order to ensure that the flow of people in the park is adequate, the pizza stalls are connected by one-way paths, and to make sure that everyone will leave the festival, it is made impossible to visit a pizza stall more than once. However, every stall can be reached from the stall at the entrance, which is the stall with number 0.
Of course, Margriet has her own taste: she will like some pizzas more than others. Eating pizza from a stall will give her a certain amount of satisfaction which is equal to Margriet’s personal stall satisfaction number multiplied by the fraction of a whole pizza she eats there.
Her total satisfaction is the sum of satisfactions of every stall she visits. Can you help Margriet plot a route between the pizza stalls that satisfies her the most?

输入
• Two integers 1 ≤ n ≤ 5 · 105 and 0 ≤ m ≤ 5 · 105, the number of pizza stalls and the number of one way connections.
• The second line has n integers c0, . . . , cn−1, 0 ≤ ci ≤ 109, the amount of enjoyment Margriet gets from eating one pizza at stall i.
• The next m lines each contain 2 integers 0 ≤ s < n and 0 ≤ t < n, ndicating a one way path from stall s to stall t. No connection will appear twice in the input.

输出
Print the maximal amount of enjoyment Margriet can reach at the pizza fair. Your answer will be considered correct if it differs from the actual answer by at most 10−6 absolutely or relatively.

样例输入
5 5
1 4 6 2 100
0 1
1 2
0 3
2 4
3 4

样例输出
100

思路
从入度为0的点入图,根据题意推得答案即可

代码实现

#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>

using namespace std;
const int N=1000005;
typedef long long ll;

struct node
{
    int to,index;
}G[N];
int n,m;
double dp[N],ans;
int head[N],cnt;
int c[N],deg[N];
void add(int u,int v)
{
    G[++cnt].to=v;
    G[cnt].index=head[u];
    head[u]=cnt;
}

void dfs(int x)
{
    if(dp[x]>0) return ;
    dp[x]=c[x];
    for(int i=head[x];i;i=G[i].index)
    {
        int v=G[i].to;
        dfs(v);
        dp[x]=max(dp[x],max(dp[v],dp[v]/2+c[x]));
    }
    ans=max(ans,dp[x]);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++) scanf("%d",&c[i]);
    for(int i=0;i<m;i++)
    {
        int u,v;
        scanf("%d%d",&u,&v);
        add(u,v);
        deg[v]++;
    }
    for(int i=0;i<n;i++) if(!deg[i]) dfs(i);
    printf("%f\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值