The Preliminary Contest for ICPC Asia Shenyang 2019 D. Fish eating fruit

树形DP与点分治算法详解
本文详细解析了树形DP与点分治两种算法在解决特定路径问题上的应用,通过具体实例展示了算法的设计思路与实现过程,为读者提供了深入理解与实践的参考。

题意:设f(x,y)=dis(x,y)%3,现在要求f(x,y)=i(0<=i<=2)的路径

思路:

两种解法,一种树形dp,一种点分治。

树形dp:二次扫描换根,第一次扫描,搜索出每个节点到其子树的答案贡献,第二次扫描,搜出这个点到非子树节点的答案贡献

最后统计答案的时候加上两次扫描每个点的贡献

代码:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=1e4+9;
const int mod=1e9+7;
ll dp[maxn][3],ans[3],head[maxn],vis[maxn],fdp[maxn][3],ct[maxn][3],fct[maxn][3];
struct Edge{
    int next,to,val;
}edge[maxn*2];
int cnt=0;
void add(int u,int v,int val){
    edge[cnt].next=head[u];
    edge[cnt].to=v;
    edge[cnt].val=val;
    head[u]=cnt++;
}
void dfs1(int u){
    ct[u][0]=1;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
//            cout<<u<<' '<<v<<endl;
            vis[v]=1;
            dfs1(v);
            int dis=edge[i].val;
            for(int j=0;j<3;j++){
//                if(dp[v][(j-(dis%3)+3)%3]>=0){
                    ct[u][j]=(ct[u][j]+ct[v][(j-(dis%3)+3)%3])%mod;
                    dp[u][j]=(dp[u][j]+dp[v][(j-(dis%3)+3)%3]+dis*ct[v][(j-(dis%3)+3)%3])%mod;
                }
//            else
//                if(j==dis%3){
//                    ct[u][j]=max(ct[u][j],0)+1;
//                    dp[u][j]=max(dp[u][j],0)+dis;
//                }
//            }
        }
    }
}
void dfs2(int u){
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
            vis[v]=1;
            int dis=edge[i].val;
            for(int j=0;j<3;j++){
                fct[v][j]=(fct[u][((j-dis)%3+3)%3]+ct[u][((j-dis)%3+3)%3]-ct[v][((j-2*dis)%3+3)%3])%mod;
                fdp[v][j]=(fdp[u][((j-dis)%3+3)%3]+(dp[u][((j-dis)%3+3)%3]-dp[v][((j-2*dis)%3+3)%3]-dis*ct[v][((j-2*dis)%3+3)%3]%mod+mod)%mod+dis*fct[v][(j)%3]%mod)%mod;

            }
            dfs2(v);
        }
    }
}
int main(){
    int i,j,k,n;
    while(cin>>n){
        cnt=0;
        memset(head,-1,sizeof(head));
        memset(dp,0,sizeof(dp));
        memset(fdp,0,sizeof(fdp));
        memset(ct,0,sizeof(ct));
        memset(fct,0,sizeof(fct));
        for(i=1;i<n;i++){
            int x,y,z;cin>>x>>y>>z;
            add(x,y,z);add(y,x,z);
        }
        for(i=0;i<n;i++)vis[i]=0;
        vis[0]=1;
        dfs1(0);
        for(i=0;i<n;i++)vis[i]=0;
        vis[0]=1;
        dfs2(0);
        memset(ans,0,sizeof(ans));
        for(i=0;i<n;i++){
//            for(j=0;j<3;j++)cout<<i<<' '<<dp[i][j]<<endl;
            for(j=0;j<3;j++)(ans[j]+=fdp[i][j]+dp[i][j])%=mod;
        }
        for(i=0;i<3;i++){
            if(i!=2)cout<<ans[i]<<' ';
            else cout<<ans[i]<<endl;
        }
    }
}

点分治:点分治板子一套,稍微改改就行

代码:

#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn=1e4+9;
const int mod=1e9+7;
struct Edge{
    int next,to,val;
}edge[maxn*2];
int cnt=0,vis[maxn],root,k,mx=inf,Size,head[maxn],sz[maxn],r=0;
ll d[maxn],tmp[3],ans[3],id_path[3],id_num[3];
inline void add(int u,int v,int val){
    edge[cnt].next=head[u];
    edge[cnt].to=v;
    edge[cnt].val=val;
    head[u]=cnt++;
}
void getroot(int u,int fa){
    sz[u]=1;int num=0;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(v!=fa&&!vis[v]){
            getroot(v,u);
            sz[u]+=sz[v];
            num=max(num,sz[v]);
        }
    }
    num=max(num,Size-sz[u]);
    if(num<mx)mx=num,root=u;
}
void getdis(int u,int fa){
    id_path[d[u]%3]++;
    (id_num[d[u]%3]+=d[u])%=mod;
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(v!=fa&&!vis[v]){
            d[v]=(edge[i].val+d[u])%mod;
            getdis(v,u);
        }
    }
}
void cal(int u,int val){
    d[u]=val;
    for(int i=0;i<3;i++){
        id_path[i]=0;id_num[i]=0;
        tmp[i]=0;
    }
    getdis(u,0);
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            tmp[(i+j)%3]=(tmp[(i+j)%3]+id_num[i]*id_path[j]+id_num[j]*id_path[i])%mod;
        }
    }
}
void dfs(int u){
    vis[u]=1;
    cal(u,0);
    for(int i=0;i<3;i++){
        ans[i]=(ans[i]+tmp[i])%mod;
    }
    for(int i=head[u];i!=-1;i=edge[i].next){
        int v=edge[i].to;
        if(!vis[v]){
            cal(v,edge[i].val);
            for(int j=0;j<3;j++){
                ans[j]=(ans[j]-tmp[j]+mod)%mod;
            }
            Size=sz[v];
            mx=inf;
            getroot(v,0);
            dfs(root);
        }
    }
}
int main(){
    int i,j,n;
    while(cin>>n){
        cnt=0;
        for(i=0;i<n;i++){
            head[i]=-1;
            vis[i]=0;
    
        }
        memset(ans,0,sizeof(ans));
        for(i=0;i<n-1;i++){
            int x,y,z;cin>>x>>y>>z;
            add(x,y,z);add(y,x,z);
        }
        Size=n;
        mx=inf;
        getroot(0,0);
        dfs(0);
        for(i=0;i<3;i++){
            if(i!=2)
            cout<<ans[i]<<' ';
            else{
                cout<<ans[i]<<endl;
            }
        }
    }
}

 

【四轴飞行器】非线性三自由度四轴飞行器模拟器研究(Matlab代码实现)内容概要:本文围绕非线性三自由度四轴飞行器模拟器的研究展开,重点介绍基于Matlab代码实现的四轴飞行器动力学建模与仿真方法。研究构建了考虑非线性特性的飞行器数学模型,涵盖姿态动力学与运动学方程,实现了三自由度(滚转、俯仰、偏航)的精确模拟。文中详细阐述了系统建模过程、控制算法设计思路及仿真结果分析,帮助读者深入理解四轴飞行器的飞行动力学特性与控制机制;同时,该模拟器可用于算法验证、控制器设计与教学实验。; 适合人群:具备一定自动控制理论基础和Matlab编程能力的高校学生、科研人员及无人机相关领域的工程技术人员,尤其适合从事飞行器建模、控制算法开发的研究生和初级研究人员。; 使用场景及目标:①用于四轴飞行器非线性动力学特性的学习与仿真验证;②作为控制器(如PID、LQR、MPC等)设计与测试的仿真平台;③支持无人机控制系统教学与科研项目开发,提升对姿态控制与系统仿真的理解。; 阅读建议:建议读者结合Matlab代码逐模块分析,重点关注动力学方程的推导与实现方式,动手运行并调试仿真程序,以加深对飞行器姿态控制过程的理解。同时可扩展为六自由度模型或加入外部干扰以增强仿真真实性。
基于分布式模型预测控制DMPC的多智能体点对点过渡轨迹生成研究(Matlab代码实现)内容概要:本文围绕“基于分布式模型预测控制(DMPC)的多智能体点对点过渡轨迹生成研究”展开,重点介绍如何利用DMPC方法实现多智能体系统在复杂环境下的协同轨迹规划与控制。文中结合Matlab代码实现,详细阐述了DMPC的基本原理、数学建模过程以及在多智能体系统中的具体应用,涵盖点对点转移、避障处理、状态约束与通信拓扑等关键技术环节。研究强调算法的分布式特性,提升系统的可扩展性与鲁棒性,适用于多无人机、无人车编队等场景。同时,文档列举了大量相关科研方向与代码资源,展示了DMPC在路径规划、协同控制、电力系统、信号处理等多领域的广泛应用。; 适合人群:具备一定自动化、控制理论或机器人学基础的研究生、科研人员及从事智能系统开发的工程技术人员;熟悉Matlab/Simulink仿真环境,对多智能体协同控制、优化算法有一定兴趣或研究需求的人员。; 使用场景及目标:①用于多智能体系统的轨迹生成与协同控制研究,如无人机集群、无人驾驶车队等;②作为DMPC算法学习与仿真实践的参考资料,帮助理解分布式优化与模型预测控制的结合机制;③支撑科研论文复现、毕业设计或项目开发中的算法验证与性能对比。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注DMPC的优化建模、约束处理与信息交互机制;按文档结构逐步学习,同时参考文中提及的路径规划、协同控制等相关案例,加深对分布式控制系统的整体理解。
Sure, I'd be happy to give you some ideas for organizing a speech contest. Here are some suggestions: 1. Determine the theme and rules: Decide on the theme of the contest and the rules for participants. Will it be an open topic, or will there be a specific theme? What is the maximum length of the speech? Will there be any specific guidelines for language or content? 2. Decide on the judging criteria: Determine how the speeches will be evaluated. Will judges be looking for content, delivery, or both? Will there be a score sheet or rubric that judges will use to score the speeches? 3. Recruit judges: Find people who are qualified to judge the speeches. Ideally, they should have experience in public speaking or have a background in the theme of the contest. 4. Promote the contest: Advertise the contest to potential participants, such as students, professionals, or members of a specific community. Use social media, flyers, and other methods to get the word out. 5. Registration and selection: Set a deadline for registration and selection of participants. Consider having a preliminary round to narrow down the field before the final competition. 6. Prepare the venue: Ensure that the venue is suitable for the contest. Make sure that there is adequate seating, sound equipment, and lighting for the speakers. 7. Hold the contest: Set a date and time for the contest, and make sure that all participants and judges are aware of the schedule. Encourage audience participation and provide refreshments. 8. Award ceremony: After the contest, hold an award ceremony to recognize the winners and participants. Provide certificates or other prizes to the top performers. I hope these ideas help you in organizing a successful speech contest!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值