[2016校赛]两个简单的小题

本文分享了作者在校内竞赛中解决两道题目的过程,包括测量药物剂量的背包问题和森林路径选择问题,并附上了C语言实现的源代码。

又是一年的校赛的季节,今年的校赛的题目感觉上还是不是那么的难,我和队友一起4个小时完成了所有的题目。总之感谢他们,在做题的时候提出了很多有益的建议和测试数据。虽然最后因为罚时只是位居第二,但是,我为你们感到骄傲!!

第一题

题目描述

Now you are asked to measure a dose of medicine with a balance and a number of weights. Certainly it is not always achievable. So you should find out the qualities which cannot be measured from the range [1,S]. S is the total quality of all the weights.

解题报告

这道题实际上就是背包问题的一个翻版,在天平上,砝码既可以放在左边,也可以放在右边,就是一个两次做循环的双肩背包。只要按照要求写就可以了, 算法复杂度为O(n*sum).

source code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define MAXN 105
#define MAXM 10005

int weight[MAXN];
int n;
int dp[MAXM];
int sum;

void Input()
{
    sum = 0;
    int i;
    for(i = 1; i <= n; i++){
        scanf("%d",&weight[i]);
        sum += weight[i];
    }
}

void Solve()
{
    int i,j;
    int ans = 0;
    memset(dp,0,sizeof(dp));
    dp[0] = 1;
    for(i=1;i<=n;i++){
        for(j=sum;j>=weight[i];j--){
            if(dp[j-weight[i]]){
                dp[j] = 1;
            }
        }
        for(j=0;j<=sum;j++){
            if(dp[j]){
                dp[abs(j-weight[i])] = 1;
            }
        }
    }

    for(i=1;i<=sum;i++){
        if(!dp[i])ans++;
    }
    printf("%d\n",ans);
    if(ans){
        for(i=1;i<=sum;i++){
            if(!dp[i]){
                printf("%d",i);
                if(--ans)printf(" ");
            }
        }
        printf("\n");
    }
    return;
}

int main()
{
    while(scanf("%d",&n)!=EOF){
        Input();
        Solve();
    }
    return 0;
}

第二题

题目描述

Jimmy experiences a lot of stress at work these days, especially since his accident made working difficult. To relax after a hard day, he likes to walk home. To make things even nicer, his office is on one side of a forest, and his house is on the other. A nice walk through the forest, seeing the birds and chipmunks is quite enjoyable.
The forest is beautiful, and Jimmy wants to take a different route everyday. He also wants to get home before dark, so he always takes a path to make progress towards his house. He considers taking a path from A to B to be progress if there exists a route from B to his home that is shorter than any possible route from A. Calculate how many different routes through the forest Jimmy might take.

解题报告

本题的一大重要的条件是,Jimmy只会从A到B当且仅当B存在一条回家的路比所有从A出发回家的路都要短。所以我们去求从家出发的单源最短路径,然后使用记忆化搜索就好了。

source code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<stdbool.h>

#define MAXN 1005
#define INF 0x3f3f3f3f

int graph[MAXN][MAXN];
int n,m;
int dist[MAXN];
int total[MAXN];

void Input()
{
    int i,j,k,t;
    memset(graph,INF,sizeof(graph));
    memset(total,-1,sizeof(total));
    total[2] = 1;
    for(i=1;i<=n;i++)
        graph[i][i] = 0;

    for(i=1;i<=m;i++){
        scanf("%d %d %d",&j,&k,&t);
        graph[j][k] = graph[k][j] = t;
    }
}

void Minpath()
{
    bool visited[MAXN];
    int i,j;
    for(i=1;i<=n;i++)dist[i] = graph[2][i];

    memset(visited,false,sizeof(visited));
    visited[2] = true;

    while(1){
        int temp1 = 2;
        int temp2 = INF;
        for(j=1;j<=n;j++){
            if(!visited[j] && dist[j]<temp2){
                temp2 = dist[j];
                temp1 = j;
            }
        }
        if(visited[temp1])break;
        visited[temp1] = true;
        for(j=1;j<=n;j++){
            if(temp2+graph[temp1][j]<dist[j]){
                dist[j] = graph[temp1][j]+temp2;
            }
        }
    }
    //for(i=1;i<=n;i++)printf("%d\n",dist[i]);
}

int Dfs(int index)
{
    int i,sum=0;
    if(total[index]>=0)return total[index];
    for(i=1;i<=n;i++){
        if(graph[index][i]<INF&&dist[index]>dist[i]&&i!=index)sum+=Dfs(i);
    }
    return total[index]=sum;
}

int main()
{
    freopen("input","r",stdin);
    while(1){
        scanf("%d",&n);
        if(!n)break;
        scanf("%d",&m);
        Input();
        Minpath();
        printf("%d\n",Dfs(1));
    }
}

结语

这两天,ACM/ICPC世界总决赛在泰国清迈进行,想来看算法也快一年了。而今年又有一所中国的大学第一次进入了world final。我想,好好努力吧,未来谁知道呢?不说了,要看直播去了。再次感谢我的队友,因为你们我不再羡慕任何人,任何事。

内容概要:本文介绍了一个基于冠豪猪优化算法(CPO)的无人机三维路径规划项目,利用Python实现了在复杂三维环境中为无人机规划安全、高效、低能耗飞行路径的完整解决方案。项目涵盖空间环境建模、无人机动力学约束、路径编码、多目标代价函数设计以及CPO算法的核心实现。通过体素网格建模、动态障碍物处理、路径平滑技术和多约束融合机制,系统能够在高维、密集障碍环境下快速搜索出满足飞行可行性、安全性与能效最优的路径,并支持在线重规划以适应动态环境变化。文中还提供了关键模块的代码示例,包括环境建模、路径评估和CPO优化流程。; 适合人群:具备一定Python编程基础和优化算法基础知识,从事无人机、智能机器人、路径规划或智能优化算法研究的相关科研人员与工程技术人员,尤其适合研究生及有一定工作经验的研发工程师。; 使用场景及目标:①应用于复杂三维环境下的无人机自主导航与避障;②研究智能优化算法(如CPO)在路径规划中的实际部署与性能优化;③实现多目标(路径最短、能耗最低、安全性最高)耦合条件下的工程化路径求解;④构建可扩展的智能无人系统决策框架。; 阅读建议:建议结合文中模型架构与代码示例进行实践运行,重点关注目标函数设计、CPO算法改进策略与约束处理机制,宜在仿真环境中测试不同场景以深入理解算法行为与系统鲁棒性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值