Caves - UVa 1407 树形dp

科学家们最近发现了一个未知的Menggol族部落,其部落成员居住在相互连接的洞穴中。为了探索这个复杂而危险的地形,他们派出了机器人进行考察。本文将介绍如何通过给定的地图和机器人能量限制,计算机器人最多能探索多少个洞穴。

It is said that the people of Menggol lived in caves. A tribe's caves were connected to each other with paths. The paths were so designed that there was one and only one path to each cave. So the caves and the paths formed a tree. There was a main cave, which connected the world outside. The Menggolian always carved a map of the tribe's caves on the wall of the main cave.

Scientists have just discovered Menggolian's tribe. What a heart-stirring discovery! They are eager to explore it. Since the terrain is very complex and dangerous, they decide to send out a robot.

The robot will be landed into the main cave, where he will begin his adventure. It doesn't have to return to the main cave, because the messages of his exploration will be sent immediately to the scientists while he is on the way.

A robot can only walk x meters before it runs out of energy. So the problem arises: given the map of the tribe's caves and a set of x , how many caves can be explored at most?

Input 

There are multiple test cases in the input file. Each test case starts with a single number n (0$ \le$n$ \le$500) , which is the number of caves, followed by n - 1 lines describing the map. Each of the n - 1 lines contains three integers separated by blanks: i , j , and d (1$ \le$d$ \le$10000) . It means that the i -th cave's parent caveis the j -th cave and the distance is d meters. A parent cave of cave i is the first cave to enter on the path from i to the main cave. Caves are numbered from 0 to n - 1 . Then there is an integer q (1$ \le$q$ \le$1000) , which is the number of queries, followed by q lines. For one query, there is one integer x (0$ \le$x$ \le$5000000) , the maximum distance that the robot can travel. n = 0 indicates the end of input file.

Output 

For each test case, output q lines in the format as indicated in the sample output, each line contains one integer, the maximum number of caves the robot is able to visit.

Sample Input 

3 
1 0 5 
2 0 3 
3 
3 
10 
11 
0

Sample Output 

Case 1: 
2 
2 
3

题意:给你一个n个节点的树,以及每个节点之间的距离,问从根节点开始,走不超过多少的路程,最多可以经过多少节点。

思路:dp[u][j][0]表示从u节点往下经过j个节点并且最后回到u节点的最少路程,dp[u][j][1]表示从u节点往下经过j个节点不要求回到u的最少路程,

               dp[u][j][0]=min(dp[u][j][0],dp[u][j-k][0]+dp[v][k][0]+2*dis[v]);
               dp[u][j][1]=min(dp[u][j][1],dp[u][j-k][0]+dp[v][k][1]+dis[v]);
               dp[u][j][1]=min(dp[u][j][1],dp[u][j-k][1]+dp[v][k][0]+2*dis[v]);

AC代码如下:

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
int n,dp[510][510][2],dis[510],INF=1e9,tot[510];
vector<int> vc[510];
void dfs(int u)
{
    int i,j,k,v,len=vc[u].size();
    tot[u]=1;
    for(i=0;i<len;i++)
    {
        v=vc[u][i];
        dfs(v);
        tot[u]+=tot[v];
    }
    dp[u][1][0]=dp[u][1][1]=0;
    for(i=2;i<=tot[u];i++)
       dp[u][i][0]=dp[u][i][1]=INF;
    for(i=0;i<len;i++)
    {
        v=vc[u][i];
        for(j=tot[u];j>=2;j--)
           for(k=1;k<=tot[v];k++)
           {
               dp[u][j][0]=min(dp[u][j][0],dp[u][j-k][0]+dp[v][k][0]+2*dis[v]);
               dp[u][j][1]=min(dp[u][j][1],dp[u][j-k][0]+dp[v][k][1]+dis[v]);
               dp[u][j][1]=min(dp[u][j][1],dp[u][j-k][1]+dp[v][k][0]+2*dis[v]);
           }
    }
}
int main()
{
    int t=0,i,j,k,q,u,v,ans;
    while(~scanf("%d",&n) && n>0)
    {
        printf("Case %d:\n",++t);
        for(i=0;i<=n;i++)
           vc[i].clear();
        memset(tot,0,sizeof(tot));
        for(i=1;i<n;i++)
        {
            scanf("%d%d%d",&v,&u,&k);
            vc[u].push_back(v);
            dis[v]=k;
        }
        dfs(0);
        scanf("%d",&q);
        for(j=1;j<=q;j++)
        {
            scanf("%d",&k);
            ans=0;
            for(i=n;i>=1;i--)
               if(k>=dp[0][i][1])
               {
                   ans=i;break;
               }
            printf("%d\n",ans);
        }
    }
}



【直流微电网】径向直流微电网的状态空间建模与线性化:一种耦合DC-DC变换器状态空间平均模型的方法 (Matlab代码实现)内容概要:本文介绍了径向直流微电网的状态空间建模与线性化方法,重点提出了一种基于耦合DC-DC变换器状态空间平均模型的建模策略。该方法通过对系统中多个相互耦合的DC-DC变换器进行统一建模,构建出整个微电网的集中状态空间模型,并在此基础上实施线性化处理,便于后续的小信号分析与稳定性研究。文中详细阐述了建模过程中的关键步骤,包括电路拓扑分析、状态变量选取、平均化处理以及雅可比矩阵的推导,最终通过Matlab代码实现模型仿真验证,展示了该方法在动态响应分析和控制器设计中的有效性。; 适合人群:具备电力电子、自动控制理论基础,熟悉Matlab/Simulink仿真工具,从事微电网、新能源系统建模与控制研究的研究生、科研人员及工程技术人员。; 使用场景及目标:①掌握直流微电网中多变换器系统的统一建模方法;②理解状态空间平均法在非线性电力电子系统中的应用;③实现系统线性化并用于稳定性分析与控制器设计;④通过Matlab代码复现和扩展模型,服务于科研仿真与教学实践。; 阅读建议:建议读者结合Matlab代码逐步理解建模流程,重点关注状态变量的选择与平均化处理的数学推导,同时可尝试修改系统参数或拓扑结构以加深对模型通用性和适应性的理解。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值