Hdu 2196 Computer【树型Dp】经典题

本文介绍了一种算法,用于解决给定一棵树后求每个节点到树中其它节点的最大距离的问题。通过两次深度优先搜索(DFS)实现,第一次确定每个子树中离根节点最远的节点,第二次确定非子树方向上的最远距离。

Computer

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 7193    Accepted Submission(s): 3566


Problem Description
A school bought the first computer some time ago(so this computer's id is 1). During the recent years the school bought N-1 new computers. Each new computer was connected to one of settled earlier. Managers of school are anxious about slow functioning of the net and want to know the maximum distance Si for which i-th computer needs to send signal (i.e. length of cable to the most distant computer). You need to provide this information. 


Hint: the example input is corresponding to this graph. And from the graph, you can see that the computer 4 is farthest one from 1, so S1 = 3. Computer 4 and 5 are the farthest ones from 2, so S2 = 2. Computer 5 is the farthest one from 3, so S3 = 3. we also get S4 = 4, S5 = 4.
 

Input
Input file contains multiple test cases.In each case there is natural number N (N<=10000) in the first line, followed by (N-1) lines with descriptions of computers. i-th line contains two natural numbers - number of computer, to which i-th computer is connected and length of cable used for connection. Total length of cable does not exceed 10^9. Numbers in lines of input are separated by a space.
 

Output
For each case output N lines. i-th line must contain number Si for i-th computer (1<=i<=N).
 

Sample Input
5 1 1 2 1 3 1 1 1
 

Sample Output
3 2 3 4 4

题目大意:


给你N个点的一棵树,问距离每一个点最远的点的距离。


思路:


类似:http://blog.youkuaiyun.com/mengxiang000000/article/details/75578275


①我们设定Dp【u】表示以u为根的子树中,距离点u最远距离长度。

那么有:Dp【u】=max(Dp【v】+W(u,v));


②现在对于一个点u来讲,有两个方向,一个是子树方向,一个是非子树方向。

我们搞定了子树方向,就剩下非子树方向了。

那么设定F【u】表示非子树方向到点u节点最远的长度。

对于一个点来说,如果是非子树方向就有两种递推状态的方案:

1.从父亲节点来(从根节点到当前节点这条路径的长度):F【u】=F【from】+W(from,u);

2.从当前节点的兄弟节点而来;F【u】=max(Dp【brother】+W(from,u)+W(from,v));


如果存在一个点有多个儿子的情况,从2状态转移的时间复杂度会达到O(n^2),很危险,所以我们再维护一个数组Id【u】表示以u为根的子节点中Dp【v】值最大的节点编号。同时维护从这个点u到这个最大值点v直接路径的长度(W(u,v));


过程维护一下即可。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
    int from;
    int to;
    int w;
    int next;
}e[35000];
int head[15000];
int fa[15000];
int maxn[15000];
int Id[15000];
int F[15000];
int dp[15000];
int n,cont;
void add(int from,int to,int w)
{
    e[cont].to=to;
    e[cont].w=w;
    e[cont].next=head[from];
    head[from]=cont++;
}
void dfs(int u,int from)
{
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(v==from)continue;
        else
        {
            dfs(v,u);
            if(dp[v]+w>dp[u])
            {
                dp[u]=dp[v]+w;
                Id[u]=v;
            }
        }
    }
}
void Dfs(int u,int from,int prew)
{
    fa[u]=from;
    if(from!=-1)F[u]=max(F[from]+prew,F[u]);
    if(from!=-1)
    {
        if(u==Id[from])
        {
            for(int i=head[from];i!=-1;i=e[i].next)
            {
                int v=e[i].to;
                int w=e[i].w;
                if(v==u)continue;
                if(from!=-1&&v==fa[from])continue;
                if(v==from)continue;
                F[u]=max(dp[v]+prew+w,F[u]);
            }
        }
        else
        {
            F[u]=max(F[u],dp[from]+prew);
        }
    }
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        int w=e[i].w;
        if(v==from)continue;
        Dfs(v,u,w);
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        cont=0;
        memset(head,-1,sizeof(head));
        memset(F,0,sizeof(F));
        memset(dp,0,sizeof(dp));
        for(int i=2;i<=n;i++)
        {
            int faa,w;
            scanf("%d%d",&faa,&w);
            add(i,faa,w);
            add(faa,i,w);
        }
        dfs(1,-1);
        Dfs(1,-1,0);
        /*
        for(int i=1;i<=n;i++)
        {
            printf("%d ",dp[i]);
        }
        printf("\n");
        for(int i=1;i<=n;i++)
        {
            printf("%d ",F[i]);
        }
        printf("\n");*/
        for(int i=1;i<=n;i++)
        {
            printf("%d\n",max(F[i],dp[i]));
        }
    }
}



















带开环升压转换器和逆变器的太阳能光伏系统 太阳能光伏系统驱动开环升压转换器和SPWM逆变器提供波形稳定、设计简单的交流电的模型 Simulink模型展示了一个完整的基于太阳能光伏的直流到交流电力转换系统,该系统由简单、透明、易于理解的模块构建而成。该系统从配置为提供真实直流输出电压的光伏阵列开始,然后由开环DC-DC升压转换器进行处理。升压转换器将光伏电压提高到适合为单相全桥逆变器供电的稳定直流链路电平。 逆变器使用正弦PWM(SPWM)开关来产生干净的交流输出波形,使该模型成为研究直流-交流转换基本操作的理想选择。该设计避免了闭环和MPPT的复杂性,使用户能够专注于光伏接口、升压转换和逆变器开关的核心概念。 此模型包含的主要功能: •太阳能光伏阵列在标准条件下产生~200V电压 •具有固定占空比操作的开环升压转换器 •直流链路电容器,用于平滑和稳定转换器输出 •单相全桥SPWM逆变器 •交流负载,用于观察实际输出行为 •显示光伏电压、升压输出、直流链路电压、逆变器交流波形和负载电流的组织良好的范围 •完全可编辑的结构,适合分析、实验和扩展 该模型旨在为太阳能直流-交流转换提供一个干净高效的仿真框架。布局简单明了,允许用户快速了解信号流,检查各个阶段,并根据需要修改参数。 系统架构有意保持模块化,因此可以轻松扩展,例如通过添加MPPT、动态负载行为、闭环升压控制或并网逆变器概念。该模型为进一步开发或整合到更大的可再生能源模拟中奠定了坚实的基础。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值