hdu1054 树形dp&&二分图

本文介绍了一道关于在树形结构中部署最少数量的士兵以观察所有边的算法问题。采用树形动态规划的方法求解,确保任意路径都有士兵驻守。通过递归遍历节点并更新状态,实现最小化士兵数量的目标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B - Strategic Game
Time Limit:10000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

Bob enjoys playing computer games, especially strategic games, but sometimes he cannot find the solution fast enough and then he is very sad. Now he has the following problem. He must defend a medieval city, the roads of which form a tree. He has to put the minimum number of soldiers on the nodes so that they can observe all the edges. Can you help him?

Your program should find the minimum number of soldiers that Bob has to put for a given tree.

The input file contains several data sets in text format. Each data set represents a tree with the following description:

the number of nodes
the description of each node in the following format
node_identifier:(number_of_roads) node_identifier1 node_identifier2 ... node_identifier
or
node_identifier:(0)

The node identifiers are integer numbers between 0 and n-1, for n nodes (0 < n <= 1500). Every edge appears only once in the input data.

For example for the tree:



the solution is one soldier ( at the node 1).

The output should be printed on the standard output. For each given input data set, print one integer number in a single line that gives the result (the minimum number of soldiers). An example is given in the following table:
4
0:(1) 1
1:(2) 2 3
2:(0)
3:(0)
5
3:(3) 1 4 2
1:(1) 0
2:(0)
0:(0)
4:(0)

Sample Output

1 2
前几天刚做过这题,二分图A的,树形dp同样可以解决
注意    此题要求任意一条路径上都应当有人看守,所有dp[root][0]+=dp[k][1],,,此处0的时候只能选着儿子节点的1
 
dp[root][1]=min(dp[k][0],dp[k][1])
题很水不多说了
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1505;
struct node{
    int to,net;
}edge[maxn<<1];

int head[maxn];
bool  vis[maxn];
int dp[maxn][2];
int tot;

void addedge(int u,int v){

     edge[tot].to=v;
     edge[tot].net=head[u];
     head[u]=tot++;

     edge[tot].to=u;
     edge[tot].net=head[v];
     head[v]=tot++;

}

void dfs(int root){
    vis[root]=true;
    
    for(int i=head[root];i!=-1;i=edge[i].net){
        int v=edge[i].to;
        if(!vis[v]){
            dfs(v);
            dp[root][0]+=dp[v][1];
            dp[root][1]+=min(dp[v][1],dp[v][0]);

        }
    }
}

int main(){
    int t;
    while(scanf("%d",&t)!=EOF){
         memset(dp,0,sizeof(dp));
         memset(vis,false,sizeof(vis));
         tot=0;
         memset(head,-1,sizeof(head));
         for(int i=1;i<=t;i++){
                int n,x,y;
            scanf("%d:(%d)",&x,&n);
            for(int j=1;j<=n;j++){
                scanf("%d",&y);
                addedge(x,y);
            }
         }
         int root=0;
           for(int i=0;i<t;i++)
              dp[i][1]=1;
         dfs(root);
         int ans=min(dp[root][0],dp[root][1]);
        printf("%d\n",ans);
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/13224ACMer/p/5317167.html

内容概要:本文详细探讨了基于阻尼连续可调减振器(CDC)的半主动悬架系统的控制策略。首先建立了CDC减振器的动力学模型,验证了其阻尼特性,并通过实验确认了模型的准确性。接着,搭建了1/4车辆悬架模型,分析了不同阻尼系数对悬架性能的影响。随后,引入了PID、自适应模糊PID模糊-PID并联三种控制策略,通过仿真比较它们的性能提升效果。研究表明,模糊-PID并联控制能最优地提升悬架综合性能,在平顺性稳定性间取得最佳平衡。此外,还深入分析了CDC减振器的特性,优化了控制策略,并进行了系统级验证。 适用人群:从事汽车工程、机械工程及相关领域的研究人员技术人员,尤其是对车辆悬架系统控制策略感兴趣的读者。 使用场景及目标:①适用于研究开发基于CDC减振器的半主动悬架系统的工程师;②帮助理解不同控制策略(如PID、模糊PID、模糊-PID并联)在悬架系统中的应用及其性能差异;③为优化车辆行驶舒适性稳定性提供理论依据技术支持。 其他说明:本文不仅提供了详细的数学模型仿真代码,还通过实验数据验证了模型的准确性。对于希望深入了解CDC减振器工作原理及其控制策略的读者来说,本文是一份极具价值的参考资料。同时,文中还介绍了多种控制策略的具体实现方法及其优缺点,为后续的研究实际应用提供了有益的借鉴。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值